-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add missing tests for MediaUrlLoader
- Loading branch information
Showing
3 changed files
with
238 additions
and
2 deletions.
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
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
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,236 @@ | ||
<?php declare(strict_types=1); | ||
|
||
namespace Frosh\ThumbnailProcessor\Tests\Unit\Core\Media; | ||
|
||
use Frosh\ThumbnailProcessor\Core\Media\MediaUrlGenerator; | ||
use Frosh\ThumbnailProcessor\Core\Media\MediaUrlLoader; | ||
use PHPUnit\Framework\TestCase; | ||
use Shopware\Core\Framework\DataAbstractionLayer\PartialEntity; | ||
use Shopware\Core\Framework\Uuid\Uuid; | ||
|
||
class MediaUrlLoaderTest extends TestCase | ||
{ | ||
public function testLoaded() { | ||
$id = Uuid::randomHex(); | ||
|
||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([$id => 'https://example.com/a0/image.jpg']); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => $id, | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertTrue($entity->has('url')); | ||
static::assertSame('https://example.com/a0/image.jpg', $entity->get('url')); | ||
} | ||
|
||
public function testLoadedWithMissingPrivate() { | ||
$id = Uuid::randomHex(); | ||
|
||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::never()) | ||
->method('generate') | ||
->willReturn([$id => 'https://example.com/a0/image.jpg']); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => $id, | ||
'path' => 'a0/image.txt', | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
} | ||
|
||
public function testLoadedWithMissingUrlResult() { | ||
$id = Uuid::randomHex(); | ||
|
||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([]); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => $id, | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
} | ||
|
||
public function testLoadedWithMissingPathResult() { | ||
$id = Uuid::randomHex(); | ||
|
||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::never()) | ||
->method('generate'); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => $id, | ||
'private' => false, | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
} | ||
|
||
public function testLoadedWithThumbnailHavingUrls() { | ||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([ | ||
'1' => 'https://example.com/a0/image.jpg?width=100', | ||
'2' => 'https://example.com/a0/thumbnailimage.jpg?width=100', | ||
]); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => '1', | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
'width' => 100, | ||
'thumbnails' => [ | ||
(new PartialEntity())->assign([ | ||
'id' => '2', | ||
'path' => 'a0/thumbnailimage.jpg', | ||
'width' => 100, | ||
]), | ||
], | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
static::assertFalse($entity->get('thumbnails')[0]->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertTrue($entity->has('url')); | ||
static::assertTrue($entity->get('thumbnails')[0]->has('url')); | ||
static::assertSame('https://example.com/a0/thumbnailimage.jpg?width=100', $entity->get('thumbnails')[0]->get('url')); | ||
} | ||
|
||
public function testLoadedWithThumbnailHavingNoUrlsWithMissingWidth() { | ||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([ | ||
'1' => 'https://example.com/a0/image.jpg?width=100', | ||
]); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => '1', | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
'thumbnails' => [ | ||
(new PartialEntity())->assign([ | ||
'id' => '2', | ||
'path' => 'a0/thumbnailimage.jpg', | ||
]), | ||
], | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
static::assertFalse($entity->get('thumbnails')[0]->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertTrue($entity->has('url')); | ||
static::assertFalse($entity->get('thumbnails')[0]->has('url')); | ||
} | ||
|
||
public function testLoadedWithThumbnailHavingNoUrl() { | ||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([ | ||
'1' => 'https://example.com/a0/image.jpg', | ||
]); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => '1', | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
'width' => 100, | ||
'thumbnails' => [ | ||
(new PartialEntity())->assign([ | ||
'id' => '2', | ||
]), | ||
], | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
static::assertFalse($entity->get('thumbnails')[0]->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertTrue($entity->has('url')); | ||
static::assertFalse($entity->get('thumbnails')[0]->has('url')); | ||
} | ||
|
||
/** | ||
* @dataProvider provideInvalidThumbnailData | ||
*/ | ||
public function testLoadedWithNotIterableThumbnail(mixed $thumbnail) { | ||
$mediaUrlGenerator = $this->createMock(MediaUrlGenerator::class); | ||
$mediaUrlGenerator->expects(static::once()) | ||
->method('generate') | ||
->willReturn([ | ||
'1' => 'https://example.com/a0/image.jpg', | ||
]); | ||
|
||
$mediaUrlLoader = new MediaUrlLoader($mediaUrlGenerator); | ||
$entity = new PartialEntity(); | ||
$entity->assign([ | ||
'id' => '1', | ||
'path' => 'a0/image.txt', | ||
'private' => false, | ||
'width' => 100, | ||
'thumbnails' => $thumbnail, | ||
]); | ||
|
||
static::assertFalse($entity->has('url')); | ||
|
||
$mediaUrlLoader->loaded([$entity]); | ||
|
||
static::assertTrue($entity->has('url')); | ||
} | ||
|
||
public function provideInvalidThumbnailData(): iterable | ||
{ | ||
yield [[]]; | ||
yield [[null]]; | ||
yield ['']; | ||
yield [['']]; | ||
yield [new PartialEntity()]; | ||
} | ||
} |