Skip to content

Commit

Permalink
Merge pull request #71 from manuelmeister/feature/add-tests
Browse files Browse the repository at this point in the history
FEATURE: Add basic test for scalable sources
  • Loading branch information
mficzel committed Oct 13, 2023
2 parents c245864 + 1118315 commit c3703e3
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 1 deletion.
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

0 comments on commit c3703e3

Please sign in to comment.