-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from manuelmeister/feature/add-tests
FEATURE: Add basic test for scalable sources
- Loading branch information
Showing
3 changed files
with
140 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
# ); | ||
#} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters