. * */ namespace Ampache\Repository; use Ampache\Module\Database\DatabaseConnectionInterface; use Ampache\Repository\Model\MetadataField; use PDO; use PDOStatement; use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; use Psr\Log\LoggerInterface; class MetadataFieldRepositoryTest extends TestCase { private DatabaseConnectionInterface&MockObject $connection; private LoggerInterface&MockObject $logger; private MetadataFieldRepository $subject; public function testCollectGarbageExecutesQuery(): void { $this->connection->expects(static::once()) ->method('query') ->with('DELETE FROM `metadata_field` USING `metadata_field` LEFT JOIN `metadata` ON `metadata`.`field` = `metadata_field`.`id` WHERE `metadata`.`id` IS NULL;'); $this->subject->collectGarbage(); } public function testFindByIdReturnsFoundItem(): void { $id = 666; $result = $this->createMock(PDOStatement::class); $item = $this->createMock(MetadataField::class); $this->connection->expects(static::once()) ->method('query') ->with( "SELECT `id`, IFNULL(`name`, '') AS `name`, `public` FROM `metadata_field` WHERE `id` = ?", [ $id ], ) ->willReturn($result); $result->expects(static::once()) ->method('setFetchMode') ->with(PDO::FETCH_CLASS, MetadataField::class, [$this->subject]); $result->expects(static::once()) ->method('fetch') ->willReturn($item); self::assertSame( $item, $this->subject->findById($id) ); } public function testFindByIdReturnsNullIfNoItemWasFound(): void { $id = 666; $result = $this->createMock(PDOStatement::class); $this->connection->expects(static::once()) ->method('query') ->with( "SELECT `id`, IFNULL(`name`, '') AS `name`, `public` FROM `metadata_field` WHERE `id` = ?", [ $id ], ) ->willReturn($result); $result->expects(static::once()) ->method('setFetchMode') ->with(PDO::FETCH_CLASS, MetadataField::class, [$this->subject]); $result->expects(static::once()) ->method('fetch') ->willReturn(false); self::assertNull( $this->subject->findById($id) ); } public function testFindByNameReturnsFoundItem(): void { $name = 'some-name'; $result = $this->createMock(PDOStatement::class); $item = $this->createMock(MetadataField::class); $this->connection->expects(static::once()) ->method('query') ->with( "SELECT `id`, IFNULL(`name`, '') AS `name`, `public` FROM `metadata_field` WHERE `name` = ? LIMIT 1", [ $name ], ) ->willReturn($result); $result->expects(static::once()) ->method('setFetchMode') ->with(PDO::FETCH_CLASS, MetadataField::class, [$this->subject]); $result->expects(static::once()) ->method('fetch') ->willReturn($item); self::assertSame( $item, $this->subject->findByName($name) ); } public function testFindByNameReturnsNullIfNoItemWasFound(): void { $name = 'some-name'; $result = $this->createMock(PDOStatement::class); $this->connection->expects(static::once()) ->method('query') ->with( "SELECT `id`, IFNULL(`name`, '') AS `name`, `public` FROM `metadata_field` WHERE `name` = ? LIMIT 1", [ $name ], ) ->willReturn($result); $result->expects(static::once()) ->method('setFetchMode') ->with(PDO::FETCH_CLASS, MetadataField::class, [$this->subject]); $result->expects(static::once()) ->method('fetch') ->willReturn(false); self::assertNull( $this->subject->findByName($name) ); } public function testGetPropertyListReturnsData(): void { $id = 666; $name = 'some-name'; $result = $this->createMock(PDOStatement::class); $this->connection->expects(static::once()) ->method('query') ->with('SELECT `id`, `name` FROM `metadata_field`') ->willReturn($result); $result->expects(static::exactly(2)) ->method('fetch') ->with(PDO::FETCH_ASSOC) ->willReturn(['id' => (string) $id, 'name' => $name], false); self::assertSame( [$id => $name], iterator_to_array($this->subject->getPropertyList()) ); } public function testPersistInsertsNewItemIfNew(): void { $field = $this->createMock(MetadataField::class); $name = 'some-name'; $public = true; $result = 666; $field->expects(static::once()) ->method('isNew') ->willReturn(true); $field->expects(static::once()) ->method('getName') ->willReturn($name); $field->expects(static::once()) ->method('isPublic') ->willReturn($public); $this->connection->expects(static::once()) ->method('query') ->with( 'INSERT INTO `metadata_field` (`name`, `public`) VALUES (?, ?)', [ $name, $public ] ); $this->connection->expects(static::once()) ->method('getLastInsertedId') ->willReturn($result); self::assertSame( $result, $this->subject->persist($field) ); } public function testPersistUpdatesExistingItem(): void { $field = $this->createMock(MetadataField::class); $fieldId = 42; $name = 'some-name'; $public = true; $field->expects(static::once()) ->method('isNew') ->willReturn(false); $field->expects(static::once()) ->method('getName') ->willReturn($name); $field->expects(static::once()) ->method('isPublic') ->willReturn($public); $field->expects(static::once()) ->method('getId') ->willReturn($fieldId); $this->connection->expects(static::once()) ->method('query') ->with( 'UPDATE `metadata_field` SET `name` = ?, `public` = ? WHERE `id` = ?', [ $name, $public, $fieldId ] ); self::assertNull( $this->subject->persist($field) ); } public function testPrototypeReturnsNewItem(): void { self::assertInstanceOf( MetadataField::class, $this->subject->prototype() ); } protected function setUp(): void { $this->connection = $this->createMock(DatabaseConnectionInterface::class); $this->logger = $this->createMock(LoggerInterface::class); $this->subject = new MetadataFieldRepository( $this->connection, $this->logger, ); } }