-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Replace atoum with PHPUnit for unit tests
- Loading branch information
1 parent
3d8c83b
commit 49f315f
Showing
58 changed files
with
1,681 additions
and
2,157 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
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
28 changes: 28 additions & 0 deletions
28
lib/Documents/src/Test/MediaFinders/SimpleVimeoEmbedFinder.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\Documents\Test\MediaFinders; | ||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use RZ\Roadiz\Documents\MediaFinders\AbstractVimeoEmbedFinder; | ||
use RZ\Roadiz\Documents\Models\DocumentInterface; | ||
|
||
final class SimpleVimeoEmbedFinder extends AbstractVimeoEmbedFinder | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function documentExists(ObjectManager $objectManager, string $embedId, ?string $embedPlatform): bool | ||
{ | ||
throw new \RuntimeException('Not implemented'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function injectMetaInDocument(ObjectManager $objectManager, DocumentInterface $document): DocumentInterface | ||
{ | ||
throw new \RuntimeException('Not implemented'); | ||
} | ||
} |
28 changes: 28 additions & 0 deletions
28
lib/Documents/src/Test/MediaFinders/SimpleYoutubeEmbedFinder.php
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\Documents\Test\MediaFinders; | ||
|
||
use Doctrine\Persistence\ObjectManager; | ||
use RZ\Roadiz\Documents\MediaFinders\AbstractYoutubeEmbedFinder; | ||
use RZ\Roadiz\Documents\Models\DocumentInterface; | ||
|
||
final class SimpleYoutubeEmbedFinder extends AbstractYoutubeEmbedFinder | ||
{ | ||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function documentExists(ObjectManager $objectManager, string $embedId, ?string $embedPlatform): bool | ||
{ | ||
throw new \RuntimeException('Not implemented'); | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
protected function injectMetaInDocument(ObjectManager $objectManager, DocumentInterface $document): DocumentInterface | ||
{ | ||
throw new \RuntimeException('Not implemented'); | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
lib/Documents/src/Test/Renderer/AbstractRendererTestCase.php
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,94 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\Documents\Test\Renderer; | ||
|
||
use League\Flysystem\Config; | ||
use League\Flysystem\Filesystem; | ||
use League\Flysystem\FilesystemOperator; | ||
use League\Flysystem\Local\LocalFilesystemAdapter; | ||
use League\Flysystem\MountManager; | ||
use League\Flysystem\UrlGeneration\PublicUrlGenerator; | ||
use PHPUnit\Framework\TestCase; | ||
use RZ\Roadiz\Documents\MediaFinders\EmbedFinderFactory; | ||
use RZ\Roadiz\Documents\Test\MediaFinders\SimpleVimeoEmbedFinder; | ||
use RZ\Roadiz\Documents\Test\MediaFinders\SimpleYoutubeEmbedFinder; | ||
use RZ\Roadiz\Documents\UrlGenerators\DocumentUrlGeneratorInterface; | ||
use RZ\Roadiz\Documents\UrlGenerators\DummyDocumentUrlGenerator; | ||
use Twig\Environment; | ||
use Twig\Loader\FilesystemLoader; | ||
|
||
abstract class AbstractRendererTestCase extends TestCase | ||
{ | ||
protected function htmlTidy(string $body): string | ||
{ | ||
$body = preg_replace('#[\n\r\t\s]{2,}#', ' ', $body); | ||
$body = str_replace("/", '/', $body); | ||
$body = html_entity_decode($body); | ||
return preg_replace('#\>[\n\r\t\s]+\<#', '><', $body); | ||
} | ||
|
||
protected function getUrlGenerator(): DocumentUrlGeneratorInterface | ||
{ | ||
return new DummyDocumentUrlGenerator(); | ||
} | ||
|
||
protected function getFilesystemOperator(): FilesystemOperator | ||
{ | ||
return new MountManager([ | ||
'public' => new Filesystem( | ||
new LocalFilesystemAdapter(dirname(__DIR__) . '/../../files/'), | ||
publicUrlGenerator: new class () implements PublicUrlGenerator | ||
{ | ||
public function publicUrl(string $path, Config $config): string | ||
{ | ||
return '/files/' . $path; | ||
} | ||
} | ||
), | ||
'private' => new Filesystem( | ||
new LocalFilesystemAdapter(dirname(__DIR__) . '/../../files/'), | ||
publicUrlGenerator: new class () implements PublicUrlGenerator | ||
{ | ||
public function publicUrl(string $path, Config $config): string | ||
{ | ||
return '/files/' . $path; | ||
} | ||
} | ||
) | ||
]); | ||
} | ||
|
||
protected function getEnvironment(): Environment | ||
{ | ||
$loader = new FilesystemLoader([ | ||
dirname(__DIR__) . '/../Resources/views' | ||
]); | ||
return new Environment($loader, [ | ||
'autoescape' => false | ||
]); | ||
} | ||
|
||
|
||
|
||
/** | ||
* @return EmbedFinderFactory | ||
*/ | ||
protected function getEmbedFinderFactory(): EmbedFinderFactory | ||
{ | ||
return new EmbedFinderFactory([ | ||
'youtube' => SimpleYoutubeEmbedFinder::class, | ||
'vimeo' => SimpleVimeoEmbedFinder::class, | ||
]); | ||
} | ||
|
||
public function assertHtmlTidyEquals(string $expected, string $actual, string $message = ''): void | ||
{ | ||
$this->assertEquals( | ||
$this->htmlTidy($expected), | ||
$this->htmlTidy($actual), | ||
$message | ||
); | ||
} | ||
} |
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,139 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace RZ\Roadiz\Documents\Test\Renderer; | ||
|
||
use RZ\Roadiz\Documents\ArrayDocumentFinder; | ||
use RZ\Roadiz\Documents\DocumentFinderInterface; | ||
use RZ\Roadiz\Documents\Models\DocumentInterface; | ||
use RZ\Roadiz\Documents\Models\SimpleDocument; | ||
use RZ\Roadiz\Documents\Renderer\AudioRenderer; | ||
|
||
class AudioRendererTest extends AbstractRendererTestCase | ||
{ | ||
protected function getRenderer(): AudioRenderer | ||
{ | ||
return new AudioRenderer( | ||
$this->getFilesystemOperator(), | ||
$this->getDocumentFinder(), | ||
$this->getEnvironment(), | ||
$this->getUrlGenerator() | ||
); | ||
} | ||
|
||
public function testSupports(): void | ||
{ | ||
/** @var DocumentInterface $mockValidDocument */ | ||
$mockValidDocument = new SimpleDocument(); | ||
$mockValidDocument->setFilename('file.mp3'); | ||
$mockValidDocument->setMimeType('audio/mpeg'); | ||
|
||
/** @var DocumentInterface $mockInvalidDocument */ | ||
$mockInvalidDocument = new SimpleDocument(); | ||
$mockInvalidDocument->setFilename('file.jpg'); | ||
$mockInvalidDocument->setMimeType('image/jpeg'); | ||
|
||
$renderer = $this->getRenderer(); | ||
|
||
$this->assertIsString($mockValidDocument->getMimeType()); | ||
$this->assertEquals('audio/mpeg', $mockValidDocument->getMimeType()); | ||
$this->assertIsBool($renderer->supports($mockValidDocument, [])); | ||
$this->assertTrue($renderer->supports($mockValidDocument, [])); | ||
|
||
$this->assertIsString($mockInvalidDocument->getMimeType()); | ||
$this->assertEquals('image/jpeg', $mockInvalidDocument->getMimeType()); | ||
$this->assertIsBool($renderer->supports($mockInvalidDocument, [])); | ||
$this->assertFalse($renderer->supports($mockInvalidDocument, [])); | ||
} | ||
|
||
public function testRender(): void | ||
{ | ||
/** @var DocumentInterface $mockDocument */ | ||
$mockDocument = new SimpleDocument(); | ||
$mockDocument->setFilename('file.mp3'); | ||
$mockDocument->setFolder('folder'); | ||
$mockDocument->setMimeType('audio/mpeg'); | ||
|
||
/** @var DocumentInterface $mockDocument2 */ | ||
$mockDocument2 = new SimpleDocument(); | ||
$mockDocument2->setFilename('file2.mp3'); | ||
$mockDocument2->setFolder('folder'); | ||
$mockDocument2->setMimeType('audio/mpeg'); | ||
|
||
|
||
$renderer = $this->getRenderer(); | ||
$this->assertHtmlTidyEquals((<<<EOT | ||
<audio controls> | ||
<source type="audio/ogg" src="/files/folder/file.ogg"> | ||
<source type="audio/mpeg" src="/files/folder/file.mp3"> | ||
<p>Your browser does not support native audio.</p> | ||
</audio> | ||
EOT | ||
), ($renderer->render($mockDocument, []))); | ||
|
||
|
||
$this->assertHtmlTidyEquals((<<<EOT | ||
<audio controls> | ||
<source type="audio/mpeg" src="/files/folder/file2.mp3"> | ||
<p>Your browser does not support native audio.</p> | ||
</audio> | ||
EOT | ||
), ($renderer->render($mockDocument2, []))); | ||
|
||
|
||
$this->assertHtmlTidyEquals((<<<EOT | ||
<audio controls autoplay loop> | ||
<source type="audio/ogg" src="/files/folder/file.ogg"> | ||
<source type="audio/mpeg" src="/files/folder/file.mp3"> | ||
<p>Your browser does not support native audio.</p> | ||
</audio> | ||
EOT | ||
), ($renderer->render($mockDocument, [ | ||
'controls' => true, | ||
'loop' => true, | ||
'autoplay' => true, | ||
]))); | ||
|
||
|
||
$this->assertHtmlTidyEquals((<<<EOT | ||
<audio> | ||
<source type="audio/ogg" src="/files/folder/file.ogg"> | ||
<source type="audio/mpeg" src="/files/folder/file.mp3"> | ||
<p>Your browser does not support native audio.</p> | ||
</audio> | ||
EOT | ||
), ($renderer->render($mockDocument, [ | ||
'controls' => false | ||
]))); | ||
} | ||
|
||
/** | ||
* @return DocumentFinderInterface | ||
*/ | ||
private function getDocumentFinder(): DocumentFinderInterface | ||
{ | ||
$finder = new ArrayDocumentFinder(); | ||
|
||
$finder->addDocument( | ||
(new SimpleDocument()) | ||
->setFilename('file.mp3') | ||
->setFolder('folder') | ||
->setMimeType('audio/mpeg') | ||
); | ||
$finder->addDocument( | ||
(new SimpleDocument()) | ||
->setFilename('file.ogg') | ||
->setFolder('folder') | ||
->setMimeType('audio/ogg') | ||
); | ||
$finder->addDocument( | ||
(new SimpleDocument()) | ||
->setFilename('file2.mp3') | ||
->setFolder('folder') | ||
->setMimeType('audio/mpeg') | ||
); | ||
|
||
return $finder; | ||
} | ||
} |
Oops, something went wrong.