. * */ namespace Ampache\Repository; use Ampache\Module\Database\DatabaseConnectionInterface; use PDO; use PDOStatement; use PHPUnit\Framework\TestCase; class ImageRepositoryTest extends TestCase { private DatabaseConnectionInterface $connection; private ImageRepository $subject; public function testDeleteImageDeletes(): void { $imageId = 666; $this->connection->expects(static::once()) ->method('query') ->with( 'UPDATE `image` SET `image` = NULL WHERE `id` = ?', [ $imageId ] ); $this->subject->deleteImage($imageId); } public function testFindAllImage(): void { $result = $this->createMock(PDOStatement::class); $rowId = 666; $objectId = 42; $objectType = 'some-type'; $size = 'some-size'; $mimeType = 'some-mimetype'; $this->connection->expects(static::once()) ->method('query') ->with('SELECT `id`, `object_id`, `object_type`, `size`, `mime` FROM `image` WHERE `image` IS NOT NULL') ->willReturn($result); $result->expects(static::exactly(2)) ->method('fetch') ->with(PDO::FETCH_ASSOC) ->willReturn( [ 'id' => (string) $rowId, 'object_id' => (string) $objectId, 'object_type' => $objectType, 'size' => $size, 'mime' => $mimeType, ], false ); self::assertSame( [[ 'id' => $rowId, 'object_id' => $objectId, 'object_type' => $objectType, 'size' => $size, 'mime' => $mimeType, ]], iterator_to_array($this->subject->findAllImage()) ); } public function testGetRawImageReturnsImage(): void { $objectId = 666; $objectType = 'some-type'; $size = 'some-size'; $mimeType = 'some-mimetype'; $result = 'some-result'; $this->connection->expects(static::once()) ->method('fetchOne') ->with( 'SELECT `image` FROM `image` WHERE `object_id` = ? AND `object_type` = ? AND `size` = ? AND `mime` = ?', [ $objectId, $objectType, $size, $mimeType ] ) ->willReturn($result); self::assertSame( $result, $this->subject->getRawImage($objectId, $objectType, $size, $mimeType) ); } public function testGetRawImageReturnsNullIfNotExisting(): void { $objectId = 666; $objectType = 'some-type'; $size = 'some-size'; $mimeType = 'some-mimetype'; $this->connection->expects(static::once()) ->method('fetchOne') ->with( 'SELECT `image` FROM `image` WHERE `object_id` = ? AND `object_type` = ? AND `size` = ? AND `mime` = ?', [ $objectId, $objectType, $size, $mimeType ] ) ->willReturn(false); self::assertNull( $this->subject->getRawImage($objectId, $objectType, $size, $mimeType) ); } protected function setUp(): void { $this->connection = $this->createMock(DatabaseConnectionInterface::class); $this->subject = new ImageRepository( $this->connection, ); } }