Valinor/tests/Integration/IntegrationTest.php
Romain Canon a805ba0442 feat!: wrap node messages in proper class
A new class `NodeMessage` is used to wrap messages added to a node
during the mapping. This class will allow further features by giving
access to useful data related to the bound node.

BREAKING CHANGE: as of now every message is wrapped into a `NodeMessage`
it is therefore not possible to check whether the message is an instance
of `Throwable` — a new method `$message->isError()` is now to be used
for such cases.
2022-01-06 14:11:42 +01:00

82 lines
2.0 KiB
PHP

<?php
declare(strict_types=1);
namespace CuyZ\Valinor\Tests\Integration;
use CuyZ\Valinor\Mapper\MappingError;
use CuyZ\Valinor\Mapper\Tree\Node;
use CuyZ\Valinor\MapperBuilder;
use FilesystemIterator;
use PHPUnit\Framework\TestCase;
use function implode;
use function is_dir;
use function iterator_to_array;
abstract class IntegrationTest extends TestCase
{
protected MapperBuilder $mapperBuilder;
private string $cacheDir;
protected function setUp(): void
{
parent::setUp();
$this->cacheDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'valinor-integration-test' . DIRECTORY_SEPARATOR . uniqid();
$this->mapperBuilder = (new MapperBuilder())->withCacheDir($this->cacheDir);
}
protected function tearDown(): void
{
parent::tearDown();
if (! is_dir($this->cacheDir)) {
return;
}
/** @var FilesystemIterator $file */
foreach (new FilesystemIterator($this->cacheDir) as $file) {
/** @var string $path */
$path = $file->getRealPath();
unlink($path);
}
rmdir($this->cacheDir);
}
/**
* @return never-return
*/
protected function mappingFail(MappingError $error)
{
$errorFinder = static function (Node $node, callable $errorFinder) {
if ($node->isValid()) {
return;
}
$errors = [];
foreach ($node->messages() as $message) {
if ($message->isError()) {
$errors[] = (string)$message;
}
}
if (count($errors) > 0) {
yield $node->path() => "{$node->path()}: " . implode(' / ', $errors);
}
foreach ($node->children() as $child) {
yield from $errorFinder($child, $errorFinder);
}
};
$list = iterator_to_array($errorFinder($error->node(), $errorFinder));
self::fail(implode(' — ', $list));
}
}