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
104 changes: 104 additions & 0 deletions tests/Command/ListProcessCommandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
<?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\Command;

use CleverAge\ProcessBundle\Command\ListProcessCommand;
use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Registry\ProcessConfigurationRegistry;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;

#[CoversClass(ListProcessCommand::class)]
#[UsesClass(ProcessConfiguration::class)]
#[UsesClass(TaskConfiguration::class)]
#[UsesClass(ProcessConfigurationRegistry::class)]
class ListProcessCommandTest extends TestCase
{
public function testThePublicProcessesAreListedSortedByCode(): void
{
$tester = $this->createTester();

self::assertSame(0, $tester->execute([]));
self::assertSame(
[
'There are 2 process configurations defined (and 1 private) :',
' - test.a with 1 tasks First process',
' - test.b with 0 tasks',
],
$this->lines($tester->getDisplay())
);
}

public function testAllTheProcessesAreListedWithTheAllOption(): void
{
$tester = $this->createTester();

self::assertSame(0, $tester->execute(['--all' => true]));
self::assertSame(
[
'There are 2 process configurations defined (and 1 private) :',
' - test.a with 1 tasks First process',
' - test.b with 0 tasks',
' - test.private with 0 tasks (private) Private process',
],
$this->lines($tester->getDisplay())
);
}

/**
* @return list<string>
*/
private function lines(string $display): array
{
return array_values(array_filter(array_map(rtrim(...), explode("\n", $display))));
}

private function createTester(): CommandTester
{
$process = static fn (string $description, bool $public, array $tasks = []): array => [
'options' => [],
'entry_point' => null,
'end_point' => null,
'description' => $description,
'help' => '',
'public' => $public,
'tasks' => $tasks,
];
$registry = new ProcessConfigurationRegistry(
[
'test.private' => $process('Private process', false),
'test.b' => $process('', true),
'test.a' => $process('First process', true, [
'task' => [
'service' => '@app.task',
'options' => [],
'description' => '',
'help' => '',
'outputs' => [],
'errors' => [],
'error_outputs' => [],
'error_strategy' => null,
'log_level' => null,
],
]),
],
'stop'
);

return new CommandTester(new ListProcessCommand($registry));
}
}
36 changes: 36 additions & 0 deletions tests/Event/ConsoleProcessEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?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\Event;

use CleverAge\ProcessBundle\Event\ConsoleProcessEvent;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;

#[CoversClass(ConsoleProcessEvent::class)]
class ConsoleProcessEventTest extends TestCase
{
public function testGetters(): void
{
$input = new ArrayInput([]);
$output = new NullOutput();
$event = new ConsoleProcessEvent($input, $output, 'data', ['key' => 'value']);

self::assertSame($input, $event->getConsoleInput());
self::assertSame($output, $event->getConsoleOutput());
self::assertSame('data', $event->getProcessInput());
self::assertSame(['key' => 'value'], $event->getProcessContext());
}
}
33 changes: 33 additions & 0 deletions tests/ExpressionLanguage/PhpFunctionProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?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\ExpressionLanguage;

use CleverAge\ProcessBundle\ExpressionLanguage\PhpFunctionProvider;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\ExpressionLanguage\ExpressionLanguage;

#[CoversClass(PhpFunctionProvider::class)]
class PhpFunctionProviderTest extends TestCase
{
public function testThePhpFunctionsCanBeUsedInExpressions(): void
{
$provider = new PhpFunctionProvider(['strtoupper', 'str_contains']);
$expressionLanguage = new ExpressionLanguage(null, [$provider]);

self::assertSame(['strtoupper', 'str_contains'], array_map(static fn ($function): string => $function->getName(), $provider->getFunctions()));
self::assertSame('FOO', $expressionLanguage->evaluate('strtoupper(value)', ['value' => 'foo']));
self::assertTrue($expressionLanguage->evaluate('str_contains(value, "o")', ['value' => 'foo']));
}
}
119 changes: 119 additions & 0 deletions tests/Logger/ProcessorsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<?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\Logger;

use CleverAge\ProcessBundle\Configuration\ProcessConfiguration;
use CleverAge\ProcessBundle\Configuration\TaskConfiguration;
use CleverAge\ProcessBundle\Logger\AbstractProcessor;
use CleverAge\ProcessBundle\Logger\ProcessProcessor;
use CleverAge\ProcessBundle\Logger\TaskProcessor;
use CleverAge\ProcessBundle\Logger\TransformerProcessor;
use CleverAge\ProcessBundle\Manager\ProcessManager;
use CleverAge\ProcessBundle\Model\ProcessHistory;
use CleverAge\ProcessBundle\Model\ProcessState;
use Monolog\Level;
use Monolog\LogRecord;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;

/**
* The Monolog processors add the current process and task to the log records.
*/
#[CoversClass(AbstractProcessor::class)]
#[CoversClass(ProcessProcessor::class)]
#[CoversClass(TaskProcessor::class)]
#[CoversClass(TransformerProcessor::class)]
#[UsesClass(ProcessConfiguration::class)]
#[UsesClass(TaskConfiguration::class)]
#[UsesClass(ProcessHistory::class)]
#[UsesClass(ProcessState::class)]
class ProcessorsTest extends TestCase
{
public function testNothingIsAddedOutsideOfAProcess(): void
{
$processManager = $this->createStub(ProcessManager::class);

foreach ([ProcessProcessor::class, TaskProcessor::class, TransformerProcessor::class] as $class) {
$record = (new $class($processManager))($this->createRecord(['foo' => 'bar']));

self::assertSame([], $record->extra, $class);
self::assertSame(['foo' => 'bar'], $record->context, $class);
}
}

public function testTheProcessProcessorAddsTheProcess(): void
{
[$processManager, $history] = $this->createProcessManager();

$record = (new ProcessProcessor($processManager))($this->createRecord());

self::assertSame(
['process_id' => $history->getId(), 'process_code' => 'test.process', 'process_context' => ['key' => 'value']],
$record->extra
);
}

public function testTheTaskAndTransformerProcessorsAddTheTaskAndItsError(): void
{
[$processManager, $history, $state] = $this->createProcessManager();
$exception = new \RuntimeException('Something went wrong');
$state->setErrorOutput(['invalid' => 'item']);
$state->setException($exception);

foreach ([TaskProcessor::class, TransformerProcessor::class] as $class) {
$record = (new $class($processManager))($this->createRecord());

self::assertSame(
[
'process_id' => $history->getId(),
'process_code' => 'test.process',
'process_context' => ['key' => 'value'],
'task_code' => 'my_task',
'task_service' => '@app.task',
'error' => ['invalid' => 'item'],
'exception' => $exception,
],
$record->extra,
$class
);
}
}

/**
* @return array{ProcessManager, ProcessHistory, ProcessState}
*/
private function createProcessManager(): array
{
$processConfiguration = new ProcessConfiguration('test.process', []);
$history = new ProcessHistory($processConfiguration, ['key' => 'value']);
$taskConfiguration = new TaskConfiguration('my_task', '@app.task', []);
$state = new ProcessState($processConfiguration, $history);
$taskConfiguration->setState($state);

$processManager = $this->createStub(ProcessManager::class);
$processManager->method('getProcessHistory')->willReturn($history);
$processManager->method('getTaskConfiguration')->willReturn($taskConfiguration);

return [$processManager, $history, $state];
}

/**
* @param array<string, mixed> $context
*/
private function createRecord(array $context = []): LogRecord
{
return new LogRecord(new \DateTimeImmutable(), 'cleverage_process', Level::Info, 'message', $context);
}
}
Loading