Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
193 changes: 193 additions & 0 deletions tests/Configuration/ProcessConfigurationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
<?php

declare(strict_types=1);

/*
* This file is part of the CleverAge/ProcessBundle package.
*
* Copyright (c) Clever-Age
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace CleverAge\ProcessBundle\Tests\Configuration;

use CleverAge\ProcessBundle\Configuration\DeprecatedCodeAliases;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Exception\CircularProcessException;
use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\DataProvider;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

/**
* The dependency groups of a process (getDependencyGroups(), getMainTaskGroup()): the process manager resolves the
* tasks of the main group in this order.
*/
#[CoversClass(ProcessConfiguration::class)]
#[UsesClass(TaskConfiguration::class)]
#[UsesClass(ProcessConfigurationRegistry::class)]
#[UsesClass(DeprecatedCodeAliases::class)]
#[UsesClass(CircularProcessException::class)]
class ProcessConfigurationTest extends TestCase
{
/**
* @return iterable<string, array{int}>
*/
public static function provideSeeds(): iterable
{
foreach (range(1, 30) as $seed) {
yield 'graph '.$seed => [$seed];
}
}

/**
* Random acyclic graphs (fixed seeds): each group contains the connected tasks once, each task after its
* ancestors.
*/
#[DataProvider('provideSeeds')]
public function testDependencyGroupsAreSorted(int $seed): void
{
mt_srand($seed);
$count = mt_rand(2, 14);
// Tasks declared in a random order, edges from a lower to a higher rank: no cycle
$ranks = range(0, $count - 1);
shuffle($ranks);
$tasks = [];
foreach ($ranks as $rank) {
$tasks['t'.$rank] = ['outputs' => [], 'error_outputs' => []];
}
for ($from = 0; $from < $count; ++$from) {
for ($to = $from + 1; $to < $count; ++$to) {
$random = mt_rand(0, 9);
if ($random < 2) {
$tasks['t'.$from]['outputs'][] = 't'.$to;
} elseif (2 === $random) {
$tasks['t'.$from]['error_outputs'][] = 't'.$to;
}
}
}
$processConfiguration = $this->createProcessConfiguration($tasks);

$groups = $processConfiguration->getDependencyGroups();

$grouped = array_merge(...$groups);
sort($grouped);
$codes = array_keys($tasks);
sort($codes);
self::assertSame($codes, $grouped, 'Each task in a single group');
foreach ($groups as $group) {
$positions = array_flip($group);
foreach ($group as $code) {
$task = $processConfiguration->getTaskConfiguration($code);
foreach ($group as $otherCode) {
if ($task->hasAncestor($processConfiguration->getTaskConfiguration($otherCode))) {
self::assertLessThan($positions[$code], $positions[$otherCode], "{$otherCode} before {$code}");
}
}
// Connected tasks are in the same group
foreach ([...$task->getNextTasksConfigurations(), ...$task->getErrorTasksConfigurations()] as $next) {
self::assertArrayHasKey($next->getCode(), $positions);
}
}
}
}

public function testChainDeclaredInReverseOrder(): void
{
$processConfiguration = $this->createProcessConfiguration([
'e' => [],
'd' => ['outputs' => ['e']],
'c' => ['outputs' => ['d']],
'b' => ['outputs' => ['c']],
'a' => ['outputs' => ['b']],
]);

self::assertSame([['a', 'b', 'c', 'd', 'e']], $processConfiguration->getDependencyGroups());
}

/**
* The main group is the one of the entry point, else of the end point, else of the first task.
*/
public function testMainTaskGroup(): void
{
$tasks = [
'a1' => ['outputs' => ['a2']],
'a2' => [],
'b1' => ['outputs' => ['b2']],
'b2' => [],
'c1' => [],
];

self::assertSame(['a1', 'a2'], $this->createProcessConfiguration($tasks)->getMainTaskGroup());
self::assertSame('a1', $this->createProcessConfiguration($tasks)->getMainTask()?->getCode());
self::assertSame(['b1', 'b2'], $this->createProcessConfiguration($tasks, ['end_point' => 'b2'])->getMainTaskGroup());
self::assertSame(['c1'], $this->createProcessConfiguration($tasks, ['entry_point' => 'c1', 'end_point' => 'b2'])->getMainTaskGroup());
self::assertSame(
[['a1', 'a2'], ['b1', 'b2'], ['c1']],
$this->createProcessConfiguration($tasks)->getDependencyGroups()
);
}

public function testWithoutTask(): void
{
$processConfiguration = new ProcessConfiguration('test.process', []);

self::assertNull($processConfiguration->getMainTask());
self::assertSame([], $processConfiguration->getMainTaskGroup());
self::assertSame([], $processConfiguration->getDependencyGroups());
self::assertTrue($processConfiguration->isPublic(), 'Public by default');
}

/**
* With a cycle, the tasks are not sorted (the cycle is reported when the process is executed).
*/
public function testCycleIsNotSorted(): void
{
$processConfiguration = $this->createProcessConfiguration([
'c' => ['outputs' => ['a']],
'b' => ['outputs' => ['c']],
'a' => ['outputs' => ['b']],
'd' => ['outputs' => ['a']],
]);

self::assertSame([['c', 'b', 'a', 'd']], $processConfiguration->getDependencyGroups());

$this->expectException(CircularProcessException::class);
$processConfiguration->checkCircularDependencies();
}

/**
* @param array<string, array<string, mixed>> $tasks
* @param array<string, mixed> $processConfiguration
*/
private function createProcessConfiguration(array $tasks, array $processConfiguration = []): ProcessConfiguration
{
$registry = new ProcessConfigurationRegistry([
'test.process' => $processConfiguration + [
'options' => [],
'entry_point' => null,
'end_point' => null,
'description' => '',
'help' => '',
'public' => true,
'tasks' => array_map(static fn (array $task): array => $task + [
'service' => '@app.task',
'options' => [],
'description' => '',
'help' => '',
'outputs' => [],
'errors' => [],
'error_outputs' => [],
'error_strategy' => null,
'log_level' => null,
], $tasks),
],
], 'stop');

return $registry->getProcessConfiguration('test.process');
}
}
56 changes: 56 additions & 0 deletions tests/Filesystem/XmlFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,62 @@ public function testReadRestoresLibxmlErrorHandling(): void
}
}

/**
* The errors left by another code (libxml_use_internal_errors(true)) are neither reported nor cleared by a valid
* file, and none is left by an invalid one.
*/
public function testReadWithPreviousLibxmlErrors(): void
{
$previous = libxml_use_internal_errors(true);
try {
(new \DOMDocument())->loadXML('<other>');
file_put_contents($this->path, '<root/>');
(new XmlFile($this->path))->read();

file_put_contents($this->path, '<root>');
try {
(new XmlFile($this->path))->read();
self::fail('An exception should have been thrown');
} catch (\UnexpectedValueException $e) {
// Only the error of the file, trimmed, with its position
self::assertSame(
\sprintf('Invalid XML in file "%s": Premature end of data in tag root line 1 (line 1, column 7)', $this->path),
$e->getMessage()
);
}
self::assertSame([], libxml_get_errors());
self::assertTrue(libxml_use_internal_errors(), 'Restored');
} finally {
libxml_clear_errors();
libxml_use_internal_errors($previous);
}
}

/**
* A warning (e.g. a relative namespace URI) does not prevent the reading.
*/
public function testReadToleratesWarnings(): void
{
file_put_contents($this->path, '<root xmlns:a="relative"><a:b>ok</a:b></root>');

$dom = (new XmlFile($this->path))->read();

self::assertSame('ok', $dom->documentElement?->textContent);
}

public function testReadTwice(): void
{
file_put_contents($this->path, '<root>ok</root>');
$file = new XmlFile($this->path);

$first = $file->read();
$second = $file->read();

self::assertNotSame($first, $second);
self::assertSame('ok', $first->documentElement?->textContent);
self::assertSame('ok', $second->documentElement?->textContent);
}

public function testWriteThrowsWhenTheXmlCannotBeGenerated(): void
{
$dom = new class extends \DOMDocument {
Expand Down
Loading
Loading