Skip to content

Commit

Permalink
FEATURE: Test mixed and invalid descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
manuelmeister committed Oct 26, 2023
1 parent 70a7fca commit 2b39b81
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Classes/Domain/AbstractScalableImageSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ public function srcset($mediaDescriptors, bool $allowUpScaling = false): string
$maxScaleFactor = min($this->baseWidth / $this->width(), $this->baseHeight / $this->height());

foreach ($descriptors as $descriptor) {
$hasDescriptor = preg_match('/^(?<width>[0-9]+)w$|^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches);
$hasDescriptor = preg_match('/^(?<width>[0-9]+)w$|^(?<factor>[0-9\\.]+)x$/u', $descriptor, $matches, PREG_UNMATCHED_AS_NULL);

if (!$hasDescriptor) {
$this->logger->warning(sprintf('Invalid media descriptor "%s". Missing type "x" or "w"', $descriptor), LogEnvironment::fromMethodName(__METHOD__));
Expand All @@ -275,7 +275,7 @@ public function srcset($mediaDescriptors, bool $allowUpScaling = false): string
$srcsetType = isset($matches['width']) ? 'width' : 'factor';
} elseif (($srcsetType === 'width' && isset($matches['factor'])) || ($srcsetType === 'factor' && isset($matches['width']))) {
$this->logger->warning(sprintf('Mixed media descriptors are not valid: [%s]', implode(', ', is_array($descriptors) ? $descriptors : iterator_to_array($descriptors))), LogEnvironment::fromMethodName(__METHOD__));
break;
continue;
}

if ($srcsetType === 'width') {
Expand Down
77 changes: 71 additions & 6 deletions Tests/Unit/Domain/AbstractScalableImageSourceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,20 @@

class AbstractScalableImageSourceTest extends BaseTestCase
{
protected $logger = null;

protected function setUp(): void
{
parent::setUp();
$this->logger = $this->createMock(\Psr\Log\LoggerInterface::class);
}

/**
* @test
*/
public function aspectRatioIsHonored()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(400, 400);
$copy = $dummy->withWidth(200, true);
$this->assertEquals(200, $copy->height());
}
Expand All @@ -23,7 +31,7 @@ public function aspectRatioIsHonored()
*/
public function srcsetIsGenerated()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(400, 400);
$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')
Expand All @@ -35,7 +43,7 @@ public function srcsetIsGenerated()
*/
public function srcsetWithWidthAdheresToDefinition()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 400, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(400, 400);
$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', allowUpScaling: true)
Expand All @@ -48,7 +56,7 @@ public function srcsetWithWidthAdheresToDefinition()
*/
public function srcsetWithWidthShouldOutputOnlyAvailableSources()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 500, 500, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(500, 500);
$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')
Expand All @@ -60,7 +68,7 @@ public function srcsetWithWidthShouldOutputOnlyAvailableSources()
*/
public function srcsetWithRatioAdheresToDefinition()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 400, 200, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(400, 200);
$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',
Expand All @@ -74,11 +82,68 @@ public function srcsetWithRatioAdheresToDefinition()
*/
public function srcsetWithRatioShouldOutputOnlyAvailableSources()
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', 30, 12, '999', 'fff', 'Test');
$dummy = $this->getDummyImageSource(30, 12);
$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')
);
}

/**
* Log a warning if the descriptors are mixed between width and factor
* @test
*/
public function srcsetShouldWarnIfMixedDescriptors()
{
$dummy = $this->getDummyImageSource(650, 320);
$this->logger->expects($this->once())->method('warning')->with($this->equalTo('Mixed media descriptors are not valid: [1x, 100w]'));

$dummy->srcset('1x, 100w');
}

/**
* Skip srcset descriptor if it does not match the first matched descriptor
* @test
*/
public function srcsetShouldSkipMixedDescriptors()
{
$dummy = $this->getDummyImageSource(500, 300);
$this->assertEquals(
'https://example.com?w=200&h=120&bg=999&fg=fff&t=Test 200w, https://example.com?w=440&h=264&bg=999&fg=fff&t=Test 440w',
$dummy->srcset('200w, 1x, 440w')
);
}

/**
* Log a warning if the descriptor is invalid
* @test
*/
public function srcsetShouldWarnIfMissingDescriptor()
{
$dummy = $this->getDummyImageSource(30, 12);
$this->logger->expects($this->once())->method('warning')->with($this->equalTo('Invalid media descriptor "1a". Missing type "x" or "w"'));

$dummy->srcset('1a, 10w');
}

/**
* Should skip srcset descriptor if either width w or factor x is missing
* @test
*/
public function srcsetShouldSkipMissingDescriptors()
{
$dummy = $this->getDummyImageSource(200, 400);
$this->assertEquals(
'https://example.com?w=100&h=200&bg=999&fg=fff&t=Test 100w, https://example.com?w=200&h=400&bg=999&fg=fff&t=Test 200w',
$dummy->srcset('100w, 150, 200w')
);
}

protected function getDummyImageSource($width, $height)
{
$dummy = new DummyImageSource('https://example.com', 'Test', 'Test', $width, $height, '999', 'fff', 'Test');
$this->inject($dummy, 'logger', $this->logger);
return $dummy;
}
}

0 comments on commit 2b39b81

Please sign in to comment.