Skip to content

Commit

Permalink
chore(ci): refactor some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jean-Beru committed May 2, 2024
1 parent e3afc62 commit 3260810
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 35 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@
"phpstan/phpstan-phpunit": "^1.0",
"phpstan/phpstan-symfony": "^1.0",
"phpstan/extension-installer": "^1.1",
"friendsofphp/php-cs-fixer": "^3.5"
"friendsofphp/php-cs-fixer": "^3.5",
"symfony/yaml": "^7.0"
},
"suggest": {
"symfony/event-dispatcher": "Allows event subscription on the stage execution"
Expand Down
13 changes: 13 additions & 0 deletions tests/DependencyInjection/Fixtures/configuration/multiple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
jeanberu_pipeline:
pipelines:
pipeline_one:
stages:
- 'stage_1.1'
- 'stage_1.2'
- 'stage_1.3'
- 'stage_1.4'
pipeline_two:
stages:
- 'stage_2.1'
- 'stage_2.2'
- 'stage_2.3'
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
jeanberu_pipeline:
pipelines:
custom_pipeline:
processor: 'custom_processor'
stages:
- 'stage_1.1'
- 'stage_1.2'
54 changes: 20 additions & 34 deletions tests/DependencyInjection/PipelineExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,40 +7,26 @@
use JeanBeru\PipelineBundle\DependencyInjection\PipelineExtension;
use League\Pipeline\PipelineInterface;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\DependencyInjection\Reference;

class PipelineExtensionTest extends TestCase
{
public function testLoadProcessor(): void
public function testItLoadsProcessor(): void
{
$container = $this->createContainer([
'pipelines' => [
'custom_pipeline' => [
'processor' => 'custom_processor',
'stages' => ['custom_stage_1', 'custom_stage_2'],
],
],
]);
$container = $this->createContainerFromFile('processor');

$definitionProcessor = $container->getDefinition('jeanberu_pipeline.pipeline.custom_pipeline')->getArgument(1);

self::assertInstanceOf(Reference::class, $definitionProcessor);
self::assertSame('custom_processor', (string) $definitionProcessor);
$this->assertInstanceOf(Reference::class, $definitionProcessor);
$this->assertSame('custom_processor', (string) $definitionProcessor);
}

public function testLoadStages(): void
public function testItLoadsMultiplePipelines(): void
{
$container = $this->createContainer([
'pipelines' => [
'pipeline_one' => [
'stages' => ['stage_1.1', 'stage_1.2', 'stage_1.3', 'stage_1.4'],
],
'pipeline_two' => [
'stages' => ['stage_2.1', 'stage_2.2', 'stage_2.3'],
],
],
]);
$container = $this->createContainerFromFile('multiple');

$this->assertPipelineDefinition($container, 'jeanberu_pipeline.pipeline.pipeline_one', PipelineInterface::class. ' $pipelineOnePipeline', [
'stage_1.1',
Expand All @@ -60,30 +46,30 @@ public function testLoadStages(): void
*/
private function assertPipelineDefinition(ContainerBuilder $container, string $id, string $alias, array $stages): void
{
self::assertTrue($container->hasDefinition($id), "Definition \"$id\" not found.");
self::assertTrue($container->hasAlias($alias), "Alias \"$alias\" not found.");
self::assertSame($id, (string) $container->getAlias($alias));
$this->assertTrue($container->hasDefinition($id), "Definition \"$id\" not found.");
$this->assertTrue($container->hasAlias($alias), "Alias \"$alias\" not found.");
$this->assertSame($id, (string) $container->getAlias($alias));

$definitionStages = $container->getDefinition($id)->getArgument(0);
self::assertIsIterable($definitionStages);
$this->assertIsIterable($definitionStages);

$argumentCount = 0;
foreach ($definitionStages as $definitionStage) {
self::assertInstanceOf(Reference::class, $definitionStage);
self::assertSame($stages[$argumentCount], (string) $definitionStage);
$this->assertInstanceOf(Reference::class, $definitionStage);
$this->assertSame($stages[$argumentCount], (string) $definitionStage);
++$argumentCount;
}
self::assertSame(count($stages), $argumentCount);
$this->assertSame(count($stages), $argumentCount);
}

/**
* @param array<mixed> $config
*/
private function createContainer(array $config): ContainerBuilder
private function createContainerFromFile(string $file): ContainerBuilder
{

$container = new ContainerBuilder();
$container->registerExtension(new PipelineExtension());
$container->loadFromExtension('jeanberu_pipeline', $config);

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/Fixtures/configuration'));
$loader->load($file.'.yaml');

$container->getCompilerPassConfig()->setOptimizationPasses([]);
$container->getCompilerPassConfig()->setRemovingPasses([]);
Expand Down

0 comments on commit 3260810

Please sign in to comment.