From defe803e7963d813726720baed814277de9d6759 Mon Sep 17 00:00:00 2001 From: reliq Date: Wed, 22 May 2024 10:08:36 +0200 Subject: [PATCH] fix tests --- .github/workflows/unit-tests.yml | 2 +- composer.json | 2 +- phpunit.xml | 3 - src/Demand/Resize.php | 4 +- tests/Unit/Demand/DummyTest.php | 52 ++--------------- tests/Unit/Demand/ExistingImageTest.php | 53 ++++++++--------- tests/Unit/Demand/ImageTest.php | 73 +++++------------------- tests/Unit/Demand/ResizeTest.php | 55 ++++++------------ tests/Unit/Demand/ThumbnailTest.php | 18 +++--- tests/Unit/Service/ImageUploaderTest.php | 70 ++++------------------- 10 files changed, 83 insertions(+), 249 deletions(-) diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index a1a1059..fc4680e 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -8,7 +8,7 @@ jobs: strategy: max-parallel: 10 matrix: - laravel-version: ['^11.0', '^11.1'] + laravel-version: ['^11.1'] preference: ['stable'] php-version: ['8.2', '8.3'] name: Laravel ${{ matrix.laravel-version }} (${{ matrix.preference }}) on PHP ${{ matrix.php-version }} diff --git a/composer.json b/composer.json index f52beda..ab92ffb 100644 --- a/composer.json +++ b/composer.json @@ -24,7 +24,7 @@ "require": { "php": "^8.2", "illuminate/support": "^11.1", - "intervention/image": "^3.5", + "intervention/image": "^3.6", "reliqarts/laravel-common": "^8.0", "ext-json": "*", "ext-fileinfo": "*", diff --git a/phpunit.xml b/phpunit.xml index 7330a50..634a364 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -7,9 +7,6 @@ - - tests - tests/Feature diff --git a/src/Demand/Resize.php b/src/Demand/Resize.php index 2549e1a..45515e9 100644 --- a/src/Demand/Resize.php +++ b/src/Demand/Resize.php @@ -18,7 +18,7 @@ public function __construct( mixed $height, private readonly mixed $maintainAspectRatio = true, private readonly mixed $allowUpSizing = null, - private readonly bool $returnObject = false + private readonly mixed $returnObject = false ) { parent::__construct($request, $guidedImage, $width, $height); } @@ -35,6 +35,6 @@ public function allowUpSizing(): bool public function returnObject(): bool { - return $this->returnObject; + return ! $this->isValueConsideredNull($this->returnObject); } } diff --git a/tests/Unit/Demand/DummyTest.php b/tests/Unit/Demand/DummyTest.php index 10dc984..a8a9e64 100644 --- a/tests/Unit/Demand/DummyTest.php +++ b/tests/Unit/Demand/DummyTest.php @@ -5,29 +5,21 @@ namespace ReliqArts\GuidedImage\Tests\Unit\Demand; use Exception; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use ReliqArts\GuidedImage\Demand\Dummy; /** - * Class DummyTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Demand\Dummy - * * @internal */ +#[CoversClass(Dummy::class)] final class DummyTest extends TestCase { /** - * @dataProvider colorDataProvider - * - * @covers ::__construct - * @covers ::getColor - * @covers ::isValueConsideredNull - * - * @param mixed $color - * * @throws Exception */ - public function testGetColor($color, string $expectedResult): void + #[DataProvider('colorDataProvider')] + public function testGetColor(mixed $color, string $expectedResult): void { $demand = new Dummy( self::DIMENSION, @@ -38,27 +30,6 @@ public function testGetColor($color, string $expectedResult): void self::assertSame($expectedResult, $demand->getColor()); } - /** - * @dataProvider fillDataProvider - * - * @covers ::__construct - * @covers ::fill - * @covers ::isValueConsideredNull - * - * @param mixed $fill - */ - public function testFill($fill, ?string $expectedResult): void - { - $demand = new Dummy( - self::DIMENSION, - self::DIMENSION, - null, - $fill - ); - - self::assertSame($expectedResult, $demand->fill()); - } - public static function colorDataProvider(): array { return [ @@ -71,17 +42,4 @@ public static function colorDataProvider(): array [null, Dummy::DEFAULT_COLOR], ]; } - - public static function fillDataProvider(): array - { - return [ - ['0f0', '0f0'], - ['n', null], - ['_', null], - ['false', null], - ['null', null], - [false, null], - [null, null], - ]; - } } diff --git a/tests/Unit/Demand/ExistingImageTest.php b/tests/Unit/Demand/ExistingImageTest.php index 6ea852c..c757532 100644 --- a/tests/Unit/Demand/ExistingImageTest.php +++ b/tests/Unit/Demand/ExistingImageTest.php @@ -4,54 +4,51 @@ namespace ReliqArts\GuidedImage\Tests\Unit\Demand; -use PHPUnit\Framework\MockObject\MockObject; +use Exception; +use PHPUnit\Framework\Attributes\CoversClass; use ReliqArts\GuidedImage\Demand\ExistingImage; +use ReliqArts\GuidedImage\Demand\Thumbnail; /** - * Class ExistingImageTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Demand\ExistingImage - * * @internal */ +#[CoversClass(ExistingImage::class)] final class ExistingImageTest extends TestCase { /** - * @covers ::__construct - * @covers ::getRequest + * @throws Exception */ public function testGetRequest(): void { - $demand = $this->getExistingImageDemand(self::DIMENSION, self::DIMENSION, null); - - self::assertSame($this->request->reveal(), $demand->getRequest()); + self::assertSame( + $this->request->reveal(), + $this->getExistingImageDemand()->getRequest() + ); } /** - * @covers ::__construct - * @covers ::getGuidedImage + * @throws Exception */ public function testGetGuidedImage(): void { - $demand = $this->getExistingImageDemand(self::DIMENSION, self::DIMENSION, null); - - self::assertSame($this->guidedImage->reveal(), $demand->getGuidedImage()); + self::assertSame( + $this->guidedImage->reveal(), + $this->getExistingImageDemand() + ->getGuidedImage() + ); } /** - * @param $width - * @param $height - * @param null $returnObject - * - * @return ExistingImage|MockObject + * @throws Exception */ - private function getExistingImageDemand( - $width, - $height, - $returnObject = null - ): MockObject { - return $this->getMockBuilder(ExistingImage::class) - ->setConstructorArgs([$this->request->reveal(), $this->guidedImage->reveal(), $width, $height, $returnObject]) - ->getMockForAbstractClass(); + private function getExistingImageDemand(): ExistingImage + { + return new Thumbnail( + $this->request->reveal(), + $this->guidedImage->reveal(), + 'crop', + self::DIMENSION, + self::DIMENSION + ); } } diff --git a/tests/Unit/Demand/ImageTest.php b/tests/Unit/Demand/ImageTest.php index 4f58b17..dca6cc5 100644 --- a/tests/Unit/Demand/ImageTest.php +++ b/tests/Unit/Demand/ImageTest.php @@ -4,64 +4,41 @@ namespace ReliqArts\GuidedImage\Tests\Unit\Demand; -use PHPUnit\Framework\MockObject\MockObject; +use Exception; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; +use ReliqArts\GuidedImage\Demand\Dummy; use ReliqArts\GuidedImage\Demand\Image; /** - * Class ImageTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Demand\Image - * * @internal */ +#[CoversClass(Image::class)] final class ImageTest extends TestCase { /** - * @dataProvider widthAndHeightDataProvider - * @covers ::__construct - * @covers ::getWidth - * @covers ::isValueConsideredNull - * - * @param mixed $width + * @throws Exception */ + #[DataProvider('widthAndHeightDataProvider')] public function testGetWidth($width, ?int $expectedResult): void { - $demand = $this->getImageDemand($width, self::DIMENSION, null); + $demand = $this->getImageDemand($width, self::DIMENSION); self::assertSame($expectedResult, $demand->getWidth()); } /** - * @dataProvider widthAndHeightDataProvider - * @covers ::__construct - * @covers ::getHeight - * @covers ::isValueConsideredNull - * - * @param mixed $height + * @throws Exception */ + #[DataProvider('widthAndHeightDataProvider')] public function testGetHeight($height, ?int $expectedResult): void { - $demand = $this->getImageDemand(self::DIMENSION, $height, null); + $demand = $this->getImageDemand(self::DIMENSION, $height); self::assertSame($expectedResult, $demand->getHeight()); } - /** - * @dataProvider imageFlagDataProvider - * @covers ::__construct - * @covers ::isValueConsideredNull - * @covers ::returnObject - * - * @param mixed $returnObject - */ - public function testReturnObject($returnObject, bool $expectedResult): void - { - $demand = $this->getImageDemand(self::DIMENSION, self::DIMENSION, $returnObject); - - self::assertSame($expectedResult, $demand->returnObject()); - } - - public function widthAndHeightDataProvider(): array + public static function widthAndHeightDataProvider(): array { return [ [200, 200], @@ -75,30 +52,8 @@ public function widthAndHeightDataProvider(): array ]; } - public function imageFlagDataProvider(): array - { - return [ - [true, true], - ['n', false], - ['_', false], - ['false', false], - ['null', false], - [false, false], - [null, false], - ]; - } - - /** - * @param $width - * @param $height - * @param null $returnObject - * - * @return Image|MockObject - */ - private function getImageDemand($width, $height, $returnObject = null): MockObject + private function getImageDemand($width, $height): Image { - return $this->getMockBuilder(Image::class) - ->setConstructorArgs([$width, $height, $returnObject]) - ->getMockForAbstractClass(); + return new Dummy($width, $height); } } diff --git a/tests/Unit/Demand/ResizeTest.php b/tests/Unit/Demand/ResizeTest.php index 1c96fed..e7329a1 100644 --- a/tests/Unit/Demand/ResizeTest.php +++ b/tests/Unit/Demand/ResizeTest.php @@ -6,26 +6,21 @@ namespace ReliqArts\GuidedImage\Tests\Unit\Demand; +use Exception; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use ReliqArts\GuidedImage\Demand\Resize; /** - * Class ImageTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Demand\Resize - * * @internal */ +#[CoversClass(Resize::class)] final class ResizeTest extends TestCase { /** - * @dataProvider resizeDimensionDataProvider - * @covers ::__construct - * @covers ::getWidth - * @covers ::isValueConsideredNull - * @covers \ReliqArts\GuidedImage\Demand\Image::__construct - * - * @param mixed $width + * @throws Exception */ + #[DataProvider('resizeDimensionDataProvider')] public function testGetWidth($width, ?int $expectedResult): void { $demand = new Resize( @@ -39,14 +34,9 @@ public function testGetWidth($width, ?int $expectedResult): void } /** - * @dataProvider resizeDimensionDataProvider - * @covers ::__construct - * @covers ::getHeight - * @covers ::isValueConsideredNull - * @covers \ReliqArts\GuidedImage\Demand\Image::__construct - * - * @param mixed $height + * @throws Exception */ + #[DataProvider('resizeDimensionDataProvider')] public function testGetHeight($height, ?int $expectedResult): void { $demand = new Resize( @@ -60,14 +50,9 @@ public function testGetHeight($height, ?int $expectedResult): void } /** - * @dataProvider resizeFlagDataProvider - * @covers ::__construct - * @covers ::isValueConsideredNull - * @covers ::maintainAspectRatio - * @covers \ReliqArts\GuidedImage\Demand\Image::__construct - * - * @param mixed $maintainAspectRatio + * @throws Exception */ + #[DataProvider('resizeFlagDataProvider')] public function testMaintainAspectRatio($maintainAspectRatio, bool $expectedResult): void { $demand = new Resize( @@ -82,13 +67,9 @@ public function testMaintainAspectRatio($maintainAspectRatio, bool $expectedResu } /** - * @dataProvider resizeFlagDataProvider - * @covers ::__construct - * @covers ::allowUpSizing - * @covers \ReliqArts\GuidedImage\Demand\Image::__construct - * - * @param mixed $upSize + * @throws Exception */ + #[DataProvider('resizeFlagDataProvider')] public function testAllowUpSizing($upSize, bool $expectedResult): void { $demand = new Resize( @@ -104,13 +85,9 @@ public function testAllowUpSizing($upSize, bool $expectedResult): void } /** - * @dataProvider resizeFlagDataProvider - * @covers ::__construct - * @covers ::returnObject - * @covers \ReliqArts\GuidedImage\Demand\Image::__construct - * - * @param mixed $returnObject + * @throws Exception */ + #[DataProvider('resizeFlagDataProvider')] public function testReturnObject($returnObject, bool $expectedResult): void { $demand = new Resize( @@ -126,7 +103,7 @@ public function testReturnObject($returnObject, bool $expectedResult): void self::assertSame($expectedResult, $demand->returnObject()); } - public function resizeDimensionDataProvider(): array + public static function resizeDimensionDataProvider(): array { return [ [self::DIMENSION, self::DIMENSION], @@ -139,7 +116,7 @@ public function resizeDimensionDataProvider(): array ]; } - public function resizeFlagDataProvider(): array + public static function resizeFlagDataProvider(): array { return [ [true, true], diff --git a/tests/Unit/Demand/ThumbnailTest.php b/tests/Unit/Demand/ThumbnailTest.php index e511674..072fae9 100644 --- a/tests/Unit/Demand/ThumbnailTest.php +++ b/tests/Unit/Demand/ThumbnailTest.php @@ -4,22 +4,21 @@ namespace ReliqArts\GuidedImage\Tests\Unit\Demand; +use Exception; +use PHPUnit\Framework\Attributes\CoversClass; +use PHPUnit\Framework\Attributes\DataProvider; use ReliqArts\GuidedImage\Demand\Thumbnail; /** - * Class ThumbnailTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Demand\Thumbnail - * * @internal */ +#[CoversClass(Thumbnail::class)] final class ThumbnailTest extends TestCase { /** - * @dataProvider isValidDataProvider - * @covers ::__construct - * @covers ::isValid + * @throws Exception */ + #[DataProvider('isValidDataProvider')] public function testIsValid(string $method, bool $expectedResult): void { $demand = new Thumbnail( @@ -33,11 +32,12 @@ public function testIsValid(string $method, bool $expectedResult): void self::assertSame($expectedResult, $demand->isValid()); } - public function isValidDataProvider(): array + public static function isValidDataProvider(): array { return [ - ['fit', true], ['crop', true], + ['cover', true], + ['fit', true], ['grab', false], ['spook', false], ]; diff --git a/tests/Unit/Service/ImageUploaderTest.php b/tests/Unit/Service/ImageUploaderTest.php index 260dc5d..b494d4b 100644 --- a/tests/Unit/Service/ImageUploaderTest.php +++ b/tests/Unit/Service/ImageUploaderTest.php @@ -20,24 +20,22 @@ use Illuminate\Http\UploadedFile; use Mockery; use Mockery\MockInterface; +use PHPUnit\Framework\Attributes\CoversClass; use Prophecy\Argument; use Prophecy\Prophecy\ObjectProphecy; +use ReliqArts\Contract\Logger; use ReliqArts\GuidedImage\Contract\ConfigProvider; use ReliqArts\GuidedImage\Contract\FileHelper; use ReliqArts\GuidedImage\Contract\ImageUploader as ImageUploaderContract; -use ReliqArts\GuidedImage\Contract\Logger; use ReliqArts\GuidedImage\Result; use ReliqArts\GuidedImage\Service\ImageUploader; use ReliqArts\GuidedImage\Tests\Fixtures\Model\GuidedImage; use ReliqArts\GuidedImage\Tests\Unit\TestCase; /** - * Class ImageUploaderTest. - * - * @coversDefaultClass \ReliqArts\GuidedImage\Service\ImageUploader - * * @internal */ +#[CoversClass(ImageUploader::class)] final class ImageUploaderTest extends TestCase { private const ALLOWED_EXTENSIONS = ['jpg']; @@ -164,17 +162,6 @@ protected function setUp(): void } /** - * @covers ::__construct - * @covers ::getUploadDestination - * @covers ::upload - * @covers ::validate - * @covers ::validateFileExtension - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::toArray - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getDestination - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFile - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFilename - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getSize - * * @throws Exception */ public function testUpload(): void @@ -200,16 +187,6 @@ function ($argument) { } /** - * @covers ::__construct - * @covers ::getUploadDestination - * @covers ::upload - * @covers ::validate - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::toArray - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getDestination - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFile - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFilename - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getSize - * * @throws Exception */ public function testUploadWhenFileShouldBeReused(): void @@ -249,10 +226,6 @@ public function testUploadWhenFileShouldBeReused(): void } /** - * @covers ::__construct - * @covers ::upload - * @covers ::validate - * * @throws Exception */ public function testUploadWhenValidationFails(): void @@ -305,15 +278,6 @@ public function testUploadWhenValidationFails(): void } /** - * @covers ::__construct - * @covers ::upload - * @covers ::validate - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::toArray - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getDestination - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFile - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFilename - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getSize - * * @throws Exception */ public function testUploadWhenFileUploadFails(): void @@ -344,18 +308,6 @@ public function testUploadWhenFileUploadFails(): void } /** - * @covers ::__construct - * @covers ::uploadFromUrl - * @covers ::getUploadDestination - * @covers ::upload - * @covers ::validate - * @covers ::validateFileExtension - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::toArray - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getDestination - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFile - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getFilename - * @covers \ReliqArts\GuidedImage\Model\UploadedImage::getSize - * * @throws Exception */ public function testUploadFromUrl(): void @@ -431,20 +383,18 @@ function ($argument) { self::assertTrue($result->isSuccess()); } - private function getUploadedFileMock( - string $filename = 'myimage', - string $mimeType = 'image/jpeg', - string $extension = 'jpg', - int $size = 80000 - ): UploadedFile { + private function getUploadedFileMock(): UploadedFile + { + $filename = 'my-image'; + return Mockery::mock( UploadedFile::class, [ 'getFilename' => $filename, 'getClientOriginalName' => $filename, - 'getClientOriginalExtension' => $extension, - 'getMimeType' => $mimeType, - 'getSize' => $size, + 'getClientOriginalExtension' => 'jpg', + 'getMimeType' => 'image/jpeg', + 'getSize' => 80000, 'getRealPath' => $filename, 'move' => null, ]