From 11183158ad901a6c810921c50b1166c0bc7c09c1 Mon Sep 17 00:00:00 2001 From: Manuel Meister Date: Thu, 5 Oct 2023 17:16:03 +0200 Subject: [PATCH] FEATURE: Add basic test for scalable sources --- Tests/Unit/BaseTestCase.php | 46 ++++++++++ .../AbstractScalableImageSourceTest.php | 86 +++++++++++++++++++ composer.json | 9 +- 3 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 Tests/Unit/BaseTestCase.php create mode 100644 Tests/Unit/Domain/AbstractScalableImageSourceTest.php diff --git a/Tests/Unit/BaseTestCase.php b/Tests/Unit/BaseTestCase.php new file mode 100644 index 0000000..520a14c --- /dev/null +++ b/Tests/Unit/BaseTestCase.php @@ -0,0 +1,46 @@ +hasMethod('set' . $methodNamePart)) { + $methodName = 'set' . $methodNamePart; + $target->$methodName($dependency); + } elseif ($objectReflection->hasMethod('inject' . $methodNamePart)) { + $methodName = 'inject' . $methodNamePart; + $target->$methodName($dependency); + } elseif ($objectReflection->hasProperty($name)) { + $property = $objectReflection->getProperty($name); + $property->setAccessible(true); + $property->setValue($target, $dependency); + } else { + throw new \RuntimeException('Could not inject ' . $name . ' into object of type ' . get_class($target)); + } + } +} diff --git a/Tests/Unit/Domain/AbstractScalableImageSourceTest.php b/Tests/Unit/Domain/AbstractScalableImageSourceTest.php new file mode 100644 index 0000000..4f3ec86 --- /dev/null +++ b/Tests/Unit/Domain/AbstractScalableImageSourceTest.php @@ -0,0 +1,86 @@ +withWidth(200, true); + $this->assertEquals(200, $copy->height()); + } + + /** + * @test + */ + public function srcsetIsGenerated() + { + $dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test'); + $this->assertEquals( + 'https://example.com?w=200&h=200&bg=999&fg=fff&t=Test 200w, https://example.com?w=400&h=400&bg=999&fg=fff&t=Test 400w', + $dummy->srcset('200w, 400w') + ); + } + + /** + * @test + */ + public function srcsetWithWidthAdheresToDefinition() + { + $dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test'); + $this->assertEquals( + 'https://example.com?w=200&h=200&bg=999&fg=fff&t=Test 200w, https://example.com?w=400&h=400&bg=999&fg=fff&t=Test 400w, https://example.com?w=600&h=600&bg=999&fg=fff&t=Test 600w', + $dummy->srcset('200w, 400w, 600w') + ); + } + + /** + * If the actual image is smaller than the requested size, then the image should be returned in its original size. + * @test + * @todo + */ + #public function srcsetWithWidthShouldOutputOnlyAvailableSources() + #{ + # $dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 500, 500, '999', 'fff', 'Test'); + # $this->assertEquals( + # 'https://example.com?w=200&h=200&bg=999&fg=fff&t=Test 200w, https://example.com?w=400&h=400&bg=999&fg=fff&t=Test 400w, https://example.com?w=500&h=500&bg=999&fg=fff&t=Test 500w', + # $dummy->srcset('200w, 400w, 600w') + # ); + #} + + /** + * @test + */ + public function srcsetWithRatioAdheresToDefinition() + { + $dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 200, '999', 'fff', 'Test'); + $copy = $dummy->withHeight(50, true); + $this->assertEquals( + 'https://example.com?w=100&h=50&bg=999&fg=fff&t=Test 1x, https://example.com?w=200&h=100&bg=999&fg=fff&t=Test 2x, https://example.com?w=300&h=150&bg=999&fg=fff&t=Test 3x', + $copy->srcset('1x, 2x, 3x') + ); + } + + /** + * If the actual image is smaller than the requested size, then the image should be returned in its original size. + * @test + * @todo + */ + #public function srcsetWithRatioShouldOutputOnlyAvailableSources() + #{ + # $dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 30, 12, '999', 'fff', 'Test'); + # $copy = $dummy->withWidth(20, true); + # $this->assertEquals( + # 'https://example.com?w=20&h=8&bg=999&fg=fff&t=Test 1x, https://example.com?w=30&h=12&bg=999&fg=fff&t=Test 1.5x', + # $copy->srcset('1x, 2x') + # ); + #} +} \ No newline at end of file diff --git a/composer.json b/composer.json index 9533e02..09c223c 100644 --- a/composer.json +++ b/composer.json @@ -10,6 +10,7 @@ "neos/imagine": "*" }, "require-dev": { + "phpunit/phpunit": "^9.5", "phpstan/phpstan": "^1.8", "squizlabs/php_codesniffer": "^3.7" }, @@ -18,6 +19,11 @@ "Sitegeist\\Kaleidoscope\\": "Classes/" } }, + "autoload-dev": { + "psr-4": { + "Sitegeist\\Kaleidoscope\\Tests\\": "Tests/" + } + }, "extra": { "neos": { "package-key": "Sitegeist.Kaleidoscope" @@ -27,8 +33,9 @@ "fix:style": "phpcbf --colors --standard=PSR12 Classes", "test:style": "phpcs --colors -n --standard=PSR12 Classes", "test:stan": "phpstan analyse Classes", + "test:unit": "phpunit Tests/Unit", "cc": "phpstan clear cache", - "test": ["composer install", "composer test:style" , "composer test:stan"] + "test": ["composer install", "composer test:style" , "composer test:stan", "composer test:unit"] }, "config": { "allow-plugins": {