Skip to content

Commit ca80a66

Browse files
committed
Refactored generator factory
1 parent 8850859 commit ca80a66

File tree

8 files changed

+21
-73
lines changed

8 files changed

+21
-73
lines changed

lib/Core/BuiltPipeline.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ public function run(array $data = []): array
3232

3333
public function generator(array $data = []): Generator
3434
{
35-
$configuredGenerator = $this->factory->generatorFor('pipeline', [
35+
$configuredGenerator = $this->factory->generatorFor(['pipeline', [
3636
'stages' => $this->stages,
37-
]);
37+
]]);
3838

3939
$return = $data;
4040
$generator = $configuredGenerator->generator();

lib/Core/GeneratorFactory.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
namespace PhpBench\Pipeline\Core;
44

55
use PhpBench\Pipeline\Core\Exception\InvalidStage;
6+
use PhpBench\Pipeline\Core\Exception\InvalidYieldedValue;
7+
use Generator;
68

79
class GeneratorFactory
810
{
@@ -22,7 +24,7 @@ public function generatorFor($stage, array $config = []): ConfiguredGenerator
2224
$generator = $stage();
2325

2426
if (false === $generator instanceof Generator) {
25-
throw new InvalidStage(sprintf(
27+
throw new InvalidYieldedValue(sprintf(
2628
'Callable stages must return Generators, got "%s"',
2729
is_object($generator) ? get_class($generator) : gettype($generator)
2830
));
@@ -31,9 +33,13 @@ public function generatorFor($stage, array $config = []): ConfiguredGenerator
3133
return new ConfiguredGenerator($generator, []);
3234
}
3335

36+
if (is_string($stage)) {
37+
$stage = (array) $stage;
38+
}
39+
3440
if (false === is_array($stage)) {
3541
throw new InvalidStage(sprintf(
36-
'Stage must either be an stage config element or a callable, got "%s"',
42+
'Stage must either be a stage config element or a callable, got "%s"',
3743
is_object($stage) ? get_class($stage) : gettype($stage)
3844
));
3945
}

lib/Core/PipelineBuilder.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public function build(): BuiltPipeline
5151
public function load(array $stages): self
5252
{
5353
foreach ($stages as $stage) {
54+
$this->stages[] = $stage;
5455
}
5556

5657
return $this;

lib/Extension/Core/Stage/Distribution/Fork.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function __invoke(): Generator
2121
while (true) {
2222
foreach ($config['stages'] as $stage) {
2323
$generatorFactory->generatorFor($stage);
24+
yield;
2425
}
2526
}
2627

tests/Unit/Core/BuiltPipelineTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,9 @@ public function setUp()
2222
public function testItShouldProvideAGenerator()
2323
{
2424
$pipeline = new BuiltPipeline([], $this->factory->reveal());
25-
$this->factory->generatorFor('pipeline', [
25+
$this->factory->generatorFor(['pipeline', [
2626
'stages' => [],
27-
])->will(function () {
27+
]])->will(function () {
2828
return new ConfiguredGenerator((function () {
2929
yield;
3030
yield 'hello';

tests/Unit/Core/GeneratorFactoryTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PhpBench\Pipeline\Core\ConfiguredGenerator;
1414
use PhpBench\Pipeline\Core\Exception\InvalidStage;
1515
use stdClass;
16+
use PhpBench\Pipeline\Core\Exception\InvalidYieldedValue;
1617

1718
class GeneratorFactoryTest extends TestCase
1819
{
@@ -79,7 +80,7 @@ public function testResolvesConfig()
7980

8081
public function testThrowsExceptionIfCallableDoesntReturnAGenerator()
8182
{
82-
$this->expectException(InvalidStage::class);
83+
$this->expectException(InvalidYieldedValue::class);
8384
$this->expectExceptionMessage('Callable stages must return Generators, got "stdClass"');
8485

8586
$this->factory->generatorFor(function () {
@@ -90,7 +91,7 @@ public function testThrowsExceptionIfCallableDoesntReturnAGenerator()
9091
public function testThrowsExceptionIfStageNotStageOrCallable()
9192
{
9293
$this->expectException(InvalidStage::class);
93-
$this->expectExceptionMessage('Stage must either be an stage config element or a callable, got "stdClass"');
94+
$this->expectExceptionMessage('Stage must either be a stage config element or a callable, got "stdClass"');
9495

9596
$this->factory->generatorFor(new stdClass());
9697
}

tests/Unit/Core/PipelineBuilderTest.php

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -38,41 +38,6 @@ public function testBuildsAnEmptyPipeline()
3838
$this->assertInstanceOf(BuiltPipeline::class, $pipeline);
3939
}
4040

41-
public function testBuildsPipelineWithCallable()
42-
{
43-
$builder = PipelineBuilder::create();
44-
$builder->stage(function () {
45-
yield;
46-
yield ['Hello'];
47-
});
48-
$pipeline = $builder->build();
49-
50-
$this->assertInstanceOf(BuiltPipeline::class, $pipeline);
51-
$result = $pipeline->run();
52-
$this->assertEquals(['Hello'], $result);
53-
}
54-
55-
public function testBuildsPipelineWithStageAlias()
56-
{
57-
$this->extension1->stageAliases()->willReturn(['test/foobar']);
58-
$this->extension1->stage('test/foobar')->willReturn($this->stage1->reveal());
59-
$this->stage1->configure(Argument::type(Schema::class))->will(function () {});
60-
$this->stage1->__invoke()->will(function () {
61-
yield;
62-
yield ['Test'];
63-
});
64-
65-
$builder = PipelineBuilder::create();
66-
$builder->addExtension($this->extension1->reveal());
67-
68-
$builder->stage('test/foobar');
69-
$pipeline = $builder->build();
70-
71-
$this->assertInstanceOf(BuiltPipeline::class, $pipeline);
72-
$result = $pipeline->run();
73-
$this->assertEquals(['Test'], $result);
74-
}
75-
7641
/**
7742
* @dataProvider provideBuildsPipelinesFromStages
7843
*/
@@ -153,36 +118,6 @@ function () {
153118
],
154119
[ 'Value1' ],
155120
];
156-
yield 'but throws exception if stage has more than 2 elements ' => [
157-
[
158-
[ 'test/stage1', [], [] ]
159-
],
160-
[],
161-
[
162-
InvalidStage::class,
163-
'Stage config element cannot have more than 2 elements, got 3'
164-
]
165-
];
166-
yield 'but throws exception if stage was neither an array or a callable ' => [
167-
[
168-
new stdClass,
169-
],
170-
[],
171-
[
172-
InvalidStage::class,
173-
'Stage must either be an array config element or a callable, got "stdClass"'
174-
]
175-
];
176-
yield 'but throws exception stage was a indexes are not numerical' => [
177-
[
178-
[ 'test/stage1' => [] ]
179-
],
180-
[],
181-
[
182-
InvalidStage::class,
183-
'Stage config element must be a 1 to 2 element tuple (e.g. ["stage\/alias",{"config1":"value1"}]), got "{"test\/stage1":[]}"'
184-
]
185-
];
186121
}
187122

188123
public function testBuildsPipelineWithMultipleStages()

tests/Unit/Core/PipelineTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ public function provideSubstitutesConfigTokensWithDataValues()
145145

146146
public function testThrowsAnExceptionIfStageDoesNotYieldAnArray()
147147
{
148+
$this->factory->generatorFor(Argument::type('callable'))->will(function ($args) {
149+
return new ConfiguredGenerator($args[0](), []);
150+
});
151+
148152
$this->expectException(InvalidYieldedValue::class);
149153

150154
$this->runPipeline([

0 commit comments

Comments
 (0)