|
2 | 2 |
|
3 | 3 | namespace Kiboko\Component\PHPUnitExtension\Constraint\Pipeline; |
4 | 4 |
|
| 5 | +use Kiboko\Component\Pipeline\PipelineRunner; |
| 6 | +use Kiboko\Contract\Pipeline\ExtractorInterface; |
| 7 | +use Kiboko\Contract\Pipeline\NullRejection; |
| 8 | +use Kiboko\Contract\Pipeline\NullState; |
5 | 9 | use PHPUnit\Framework\Constraint\Constraint; |
6 | 10 |
|
7 | 11 | final class PipelineExtractsLike extends Constraint |
@@ -31,12 +35,52 @@ private function asIterator(iterable $iterable): \Iterator |
31 | 35 | throw new \InvalidArgumentException(); |
32 | 36 | } |
33 | 37 |
|
| 38 | + private function passThroughCoroutine(): \Generator |
| 39 | + { |
| 40 | + $line = yield; |
| 41 | + while ($line = yield $line); |
| 42 | + } |
| 43 | + |
34 | 44 | public function matches($other): bool |
35 | 45 | { |
36 | 46 | $both = new \MultipleIterator(\MultipleIterator::MIT_NEED_ANY); |
37 | 47 |
|
38 | 48 | $both->attachIterator($this->asIterator($this->expected)); |
39 | | - $both->attachIterator($this->asIterator($other->extract())); |
| 49 | + |
| 50 | + if (!$other instanceof ExtractorInterface) { |
| 51 | + $this->fail($other, strtr('Expected an instance of %expected%, but got %actual%.', [ |
| 52 | + '%expected%' => ExtractorInterface::class, |
| 53 | + '%actual%' => get_debug_type($other), |
| 54 | + ])); |
| 55 | + } |
| 56 | + |
| 57 | + $runner = new PipelineRunner(null); |
| 58 | + $extract = $other->extract(); |
| 59 | + if (is_array($extract)) { |
| 60 | + $iterator = $runner->run( |
| 61 | + new \ArrayIterator($extract), |
| 62 | + $this->passThroughCoroutine(), |
| 63 | + new NullRejection(), |
| 64 | + new NullState(), |
| 65 | + ); |
| 66 | + } elseif ($extract instanceof \Iterator) { |
| 67 | + $iterator = $runner->run( |
| 68 | + $extract, |
| 69 | + $this->passThroughCoroutine(), |
| 70 | + new NullRejection(), |
| 71 | + new NullState(), |
| 72 | + ); |
| 73 | + } elseif ($extract instanceof \Traversable) { |
| 74 | + $iterator = $runner->run( |
| 75 | + new \IteratorIterator($extract), |
| 76 | + $this->passThroughCoroutine(), |
| 77 | + new NullRejection(), |
| 78 | + new NullState(), |
| 79 | + ); |
| 80 | + } else { |
| 81 | + throw new \RuntimeException('Invalid data source, expecting array or Traversable.'); |
| 82 | + } |
| 83 | + $both->attachIterator($iterator); |
40 | 84 |
|
41 | 85 | $index = 0; |
42 | 86 | foreach ($both as [$expectedItem, $actualItem]) { |
|
0 commit comments