-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TesterBundle] EventDispatcherTesterTrait
- Loading branch information
Showing
6 changed files
with
420 additions
and
15 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
88 changes: 88 additions & 0 deletions
88
packages/tester-bundle/EventDispatcher/EventDispatcherTesterTrait.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,88 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\EventDispatcher; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
|
||
trait EventDispatcherTesterTrait | ||
{ | ||
protected static array $eventDispatcherFileCleaners = [ | ||
[EventDispatcherTesterTrait::class, 'cleanEventDispatcherFileContainerReference'], | ||
]; | ||
|
||
protected static function cleanEventDispatcherFile(string $filePath): void | ||
{ | ||
foreach (static::$eventDispatcherFileCleaners as $cleaner) { | ||
\call_user_func($cleaner, $filePath); | ||
} | ||
} | ||
|
||
/** | ||
* Clean reference in file that looks like this. | ||
* | ||
* <callable type="function" name="onKernelControllerArguments" class="ContainerPKL8sSi\RequestPayloadValueResolverGhostB42fd45" priority="0"/> | ||
* | ||
* By Removing the unique string pattern in the class attribute using DomDocument. | ||
* | ||
* The result after cleaning will be: | ||
* | ||
* <callable type="function" name="onKernelControllerArguments" class="Container\RequestPayloadValueResolverGhost" priority="0"/> | ||
* | ||
* Clean when the class name contains Container and Ghost in its name. | ||
*/ | ||
public static function cleanEventDispatcherFileContainerReference(string $filePath): void | ||
{ | ||
$dom = new \DOMDocument(); | ||
$dom->load($filePath); | ||
|
||
$xpath = new \DOMXPath($dom); | ||
|
||
$nodes = $xpath->query('//callable[@class]'); | ||
|
||
foreach ($nodes as $node) { | ||
\assert($node instanceof \DOMElement); | ||
$class = $node->getAttribute('class'); | ||
if (preg_match('/Container.*Ghost/', $class)) { | ||
// Use regex to match only the alphanumeric patterns that follow "Container" and "Ghost" | ||
$class = preg_replace('/(?<=Container)[A-Za-z0-9]+|(?<=Ghost)[A-Za-z0-9]+/', '__cleaned__', $class); | ||
$node->setAttribute('class', $class); | ||
} | ||
} | ||
|
||
$dom->save($filePath); | ||
} | ||
|
||
public static function assertEventDispatcherConfiguration(string $expectedFilePath, string $dispatcherName = 'event_dispatcher'): void | ||
{ | ||
$commandTester = new CommandTester( | ||
static::getContainer()->get('console.command.event_dispatcher_debug') | ||
); | ||
|
||
$commandTester->execute([ | ||
'--dispatcher' => $dispatcherName, | ||
'--format' => 'xml', | ||
]); | ||
|
||
$display = $commandTester->getDisplay(); | ||
|
||
$resultFilePath = tempnam(sys_get_temp_dir(), 'event_dispatcher_configuration'); | ||
|
||
file_put_contents($resultFilePath, $display); | ||
|
||
static::cleanEventDispatcherFile($resultFilePath); | ||
|
||
register_shutdown_function('unlink', $resultFilePath); | ||
|
||
if (!file_exists($expectedFilePath)) { | ||
copy($resultFilePath, $expectedFilePath); | ||
|
||
TestCase::fail($dispatcherName.' configuration file created at '.$expectedFilePath.'. Please review it and commit it.'); | ||
} | ||
|
||
TestCase::assertXmlFileEqualsXmlFile( | ||
$expectedFilePath, | ||
$resultFilePath, | ||
); | ||
} | ||
} |
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,18 @@ | ||
<?php | ||
|
||
namespace App\Tests; | ||
|
||
use Draw\Bundle\TesterBundle\EventDispatcher\EventDispatcherTesterTrait; | ||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
|
||
class AppKernelTest extends KernelTestCase | ||
{ | ||
use EventDispatcherTesterTrait; | ||
|
||
public function testEventDispatcherConfiguration(): void | ||
{ | ||
$this->assertEventDispatcherConfiguration( | ||
__DIR__.'/fixtures/AppKernelTest/testEventDispatcherConfiguration/event_dispatcher.xml' | ||
); | ||
} | ||
} |
Oops, something went wrong.