|
9 | 9 | use PhpBench\Pipeline\Core\Stage;
|
10 | 10 | use Prophecy\Argument;
|
11 | 11 | use PhpBench\Pipeline\Core\Schema;
|
| 12 | +use PhpBench\Pipeline\Core\Exception\InvalidStage; |
| 13 | +use stdClass; |
12 | 14 |
|
13 | 15 | class PipelineBuilderTest extends TestCase
|
14 | 16 | {
|
@@ -70,6 +72,92 @@ public function testBuildsPipelineWithStageAlias()
|
70 | 72 | $this->assertEquals(['Test'], $result);
|
71 | 73 | }
|
72 | 74 |
|
| 75 | + /** |
| 76 | + * @dataProvider provideBuildsPipelinesFromStages |
| 77 | + */ |
| 78 | + public function testBuildsPipelineFromStages(array $stages, array $expected, array $exception = []) |
| 79 | + { |
| 80 | + if ($exception) { |
| 81 | + list($class, $message) = $exception; |
| 82 | + $this->expectException($class); |
| 83 | + $this->expectExceptionMessage($message); |
| 84 | + } |
| 85 | + |
| 86 | + $this->extension1->stageAliases()->willReturn(['test/foobar']); |
| 87 | + $this->extension1->stage('test/foobar')->willReturn($this->stage1->reveal()); |
| 88 | + |
| 89 | + $this->stage1->configure(Argument::type(Schema::class))->will(function ($args) { |
| 90 | + $schema = $args[0]; |
| 91 | + $schema->setDefaults([ 'foo' => 'bar' ]); |
| 92 | + }); |
| 93 | + $this->stage1->__invoke()->will(function () { |
| 94 | + yield; |
| 95 | + yield ['Test']; |
| 96 | + }); |
| 97 | + |
| 98 | + $builder = PipelineBuilder::create(); |
| 99 | + $builder->addExtension($this->extension1->reveal()); |
| 100 | + $builder->load($stages); |
| 101 | + |
| 102 | + $pipeline = $builder->build(); |
| 103 | + |
| 104 | + $this->assertInstanceOf(BuiltPipeline::class, $pipeline); |
| 105 | + $result = $pipeline->run(); |
| 106 | + $this->assertEquals($expected, $result); |
| 107 | + } |
| 108 | + |
| 109 | + public function provideBuildsPipelinesFromStages() |
| 110 | + { |
| 111 | + yield 'with string alias' => [ |
| 112 | + [ |
| 113 | + 'test/foobar' |
| 114 | + ], |
| 115 | + [ 'Test' ], |
| 116 | + ]; |
| 117 | + yield 'with callable' => [ |
| 118 | + [ |
| 119 | + function () { |
| 120 | + yield; |
| 121 | + yield [ 'Hai!' ]; |
| 122 | + }, |
| 123 | + ], |
| 124 | + [ 'Hai!' ], |
| 125 | + |
| 126 | + ]; |
| 127 | + yield 'with alias in an array' => [ |
| 128 | + [ |
| 129 | + [ 'test/foobar' ] |
| 130 | + ], |
| 131 | + [ 'Test' ], |
| 132 | + ]; |
| 133 | + yield 'with alias and config' => [ |
| 134 | + [ |
| 135 | + [ 'test/foobar', ['foo' => 'bar'] ] |
| 136 | + ], |
| 137 | + [ 'Test' ], |
| 138 | + ]; |
| 139 | + yield 'but throws exception if stage has more than 2 elements ' => [ |
| 140 | + [ |
| 141 | + [ 'test/foobar', [], [] ] |
| 142 | + ], |
| 143 | + [], |
| 144 | + [ |
| 145 | + InvalidStage::class, |
| 146 | + 'Stage config element cannot have more than 2 elements, got 3' |
| 147 | + ] |
| 148 | + ]; |
| 149 | + yield 'but throws exception if stage was neither an array or a callable ' => [ |
| 150 | + [ |
| 151 | + new stdClass, |
| 152 | + ], |
| 153 | + [], |
| 154 | + [ |
| 155 | + InvalidStage::class, |
| 156 | + 'Stage must either be an array config element or a callable, got "stdClass"' |
| 157 | + ] |
| 158 | + ]; |
| 159 | + } |
| 160 | + |
73 | 161 | public function testBuildsPipelineWithMultipleStages()
|
74 | 162 | {
|
75 | 163 | $builder = PipelineBuilder::create();
|
|
0 commit comments