Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEATURE: Add basic test for scalable sources #71

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Tests/Unit/BaseTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php

declare(strict_types=1);

namespace Sitegeist\Kaleidoscope\Tests\Unit;

use PHPUnit\Framework\TestCase;

class BaseTestCase extends TestCase
{
/**
* Injects $dependency into property $name of $target
*
* This is a convenience method for setting a protected or private property in
* a test subject for the purpose of injecting a dependency.
*
* @param object $target The instance which needs the dependency
* @param string $name Name of the property to be injected
* @param mixed $dependency The dependency to inject – usually an object but can also be any other type
* @return void
* @throws \RuntimeException
* @throws \InvalidArgumentException
*/
protected function inject($target, $name, $dependency)
{
if (!is_object($target)) {
throw new \InvalidArgumentException('Wrong type for argument $target, must be object.');
}

$objectReflection = new \ReflectionObject($target);
$methodNamePart = strtoupper($name[0]) . substr($name, 1);
if ($objectReflection->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));
}
}
}
86 changes: 86 additions & 0 deletions Tests/Unit/Domain/AbstractScalableImageSourceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
declare(strict_types=1);

namespace Sitegeist\Kaleidoscope\Tests\Unit\Domain;

use Sitegeist\Kaleidoscope\Domain\DummyImageSource;
use Sitegeist\Kaleidoscope\Tests\Unit\BaseTestCase;

class AbstractScalableImageSourceTest extends BaseTestCase
{
/**
* @test
*/
public function aspectRatioIsHonored()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test');
$copy = $dummy->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')
# );
#}
}
9 changes: 8 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"neos/imagine": "*"
},
"require-dev": {
"phpunit/phpunit": "^9.5",
"phpstan/phpstan": "^1.8",
"squizlabs/php_codesniffer": "^3.7"
},
Expand All @@ -18,6 +19,11 @@
"Sitegeist\\Kaleidoscope\\": "Classes/"
}
},
"autoload-dev": {
"psr-4": {
"Sitegeist\\Kaleidoscope\\Tests\\": "Tests/"
}
},
"extra": {
"neos": {
"package-key": "Sitegeist.Kaleidoscope"
Expand All @@ -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": {
Expand Down
Loading