Skip to content

Commit 0648e67

Browse files
committed
Updated assertions
1 parent ee940f2 commit 0648e67

File tree

7 files changed

+112
-17
lines changed

7 files changed

+112
-17
lines changed

src/BuilderAssertTrait.php

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use PHPUnit\Framework\Constraint\IsIdentical;
1515
use PHPUnit\Framework\Constraint\IsInstanceOf;
1616
use PHPUnit\Framework\Constraint\LogicalNot;
17-
use PHPUnit\Framework\Constraint\UnaryOperator;
1817

1918
trait BuilderAssertTrait
2019
{
@@ -55,15 +54,31 @@ protected function assertBuilderProducesExtractorNotIteratesAs(array $expected,
5554
protected function assertBuilderProducesPipelineExtractingLike(iterable $expected, DefaultBuilder $builder, string $message = '')
5655
{
5756
$this->assertThat($builder, new BuilderProducesCodeThat(
58-
new IteratesLike($expected)
57+
new IteratesLike($expected, fn ($item) => new IsEqual($item))
58+
), $message);
59+
}
60+
61+
protected function assertBuilderProducesPipelineExtractingExactly(iterable $expected, DefaultBuilder $builder, string $message = '')
62+
{
63+
$this->assertThat($builder, new BuilderProducesCodeThat(
64+
new IteratesLike($expected, fn ($item) => new IsIdentical($item))
5965
), $message);
6066
}
6167

6268
protected function assertBuilderProducesPipelineNotExtractingLike(iterable $expected, DefaultBuilder $builder, string $message = '')
6369
{
6470
$this->assertThat($builder, new LogicalNot(
6571
new BuilderProducesCodeThat(
66-
new IteratesLike($expected)
72+
new IteratesLike($expected, fn ($item) => new IsEqual($item))
73+
),
74+
), $message);
75+
}
76+
77+
protected function assertBuilderProducesPipelineNotExtractingExactly(iterable $expected, DefaultBuilder $builder, string $message = '')
78+
{
79+
$this->assertThat($builder, new LogicalNot(
80+
new BuilderProducesCodeThat(
81+
new IteratesLike($expected, fn ($item) => new IsIdentical($item))
6782
),
6883
), $message);
6984
}

src/Constraint/Builder/BuilderProducesCodeThat.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<?php
22

3-
43
namespace Kiboko\Component\PHPUnitExtension\Constraint\Builder;
54

65
use PhpParser\Builder;

src/Constraint/Pipeline/IteratesLike.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,17 @@
33
namespace Kiboko\Component\PHPUnitExtension\Constraint\Pipeline;
44

55
use PHPUnit\Framework\Constraint\Constraint;
6-
use PHPUnit\Framework\Constraint\IsIdentical;
76

87
final class IteratesLike extends Constraint
98
{
10-
public function __construct(private iterable $expected)
11-
{
9+
/** @var callable */
10+
private $itemConstraintFactory;
11+
12+
public function __construct(
13+
private iterable $expected,
14+
callable $itemConstraintFactory
15+
) {
16+
$this->itemConstraintFactory = $itemConstraintFactory;
1217
}
1318

1419
private function asIterator(iterable $iterable): \Iterator
@@ -36,7 +41,7 @@ public function matches($other): bool
3641
$index = 0;
3742
foreach ($both as [$expectedItem, $actualItem]) {
3843
++$index;
39-
$constraint = new IsIdentical($expectedItem);
44+
$constraint = ($this->itemConstraintFactory)($expectedItem);
4045
$constraint->evaluate($actualItem, sprintf("Values of Iteration #%d", $index)) !== true;
4146
}
4247

src/Constraint/Pipeline/PipelineExtractsLike.php

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
namespace Kiboko\Component\PHPUnitExtension\Constraint\Pipeline;
44

5+
use Kiboko\Component\Pipeline\PipelineRunner;
6+
use Kiboko\Contract\Pipeline\ExtractorInterface;
7+
use Kiboko\Contract\Pipeline\NullRejection;
8+
use Kiboko\Contract\Pipeline\NullState;
59
use PHPUnit\Framework\Constraint\Constraint;
610

711
final class PipelineExtractsLike extends Constraint
@@ -31,12 +35,52 @@ private function asIterator(iterable $iterable): \Iterator
3135
throw new \InvalidArgumentException();
3236
}
3337

38+
private function passThroughCoroutine(): \Generator
39+
{
40+
$line = yield;
41+
while ($line = yield $line);
42+
}
43+
3444
public function matches($other): bool
3545
{
3646
$both = new \MultipleIterator(\MultipleIterator::MIT_NEED_ANY);
3747

3848
$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);
4084

4185
$index = 0;
4286
foreach ($both as [$expectedItem, $actualItem]) {

src/Constraint/Pipeline/PipelineLoadsLike.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public function matches($other): bool
5555
$iterator = new \AppendIterator();
5656

5757
$iterator->append(
58-
$runner->run($this->asIterator($this->source), $other->load(), new NullRejection(), new NullState())
58+
$runner->run(
59+
$this->asIterator($this->source),
60+
$other->load(),
61+
new NullRejection(),
62+
new NullState(),
63+
)
5964
);
6065
$iterator->append(
6166
$runner->run(
@@ -65,11 +70,16 @@ public function matches($other): bool
6570
yield $other->flush();
6671
})(),
6772
new NullRejection(),
68-
new NullState()
73+
new NullState(),
6974
)
7075
);
7176
} else {
72-
$iterator = $runner->run($this->asIterator($this->source), $other->load(), new NullRejection(), new NullState());
77+
$iterator = $runner->run(
78+
$this->asIterator($this->source),
79+
$other->load(),
80+
new NullRejection(),
81+
new NullState(),
82+
);
7383
}
7484
$both->attachIterator($iterator);
7585

src/Constraint/Pipeline/PipelineTransformsLike.php

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ public function matches($other): bool
5555
$iterator = new \AppendIterator();
5656

5757
$iterator->append(
58-
$runner->run($this->asIterator($this->source), $other->transform(), new NullRejection(), new NullState())
58+
$runner->run(
59+
$this->asIterator($this->source),
60+
$other->transform(),
61+
new NullRejection(),
62+
new NullState(),
63+
)
5964
);
6065
$iterator->append(
6166
$runner->run(
@@ -65,11 +70,16 @@ public function matches($other): bool
6570
yield $other->flush();
6671
})(),
6772
new NullRejection(),
68-
new NullState()
73+
new NullState(),
6974
)
7075
);
7176
} else {
72-
$iterator = $runner->run($this->asIterator($this->source), $other->transform(), new NullRejection(), new NullState());
77+
$iterator = $runner->run(
78+
$this->asIterator($this->source),
79+
$other->transform(),
80+
new NullRejection(),
81+
new NullState(),
82+
);
7383
}
7484
$both->attachIterator($iterator);
7585

src/PipelineAssertTrait.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,25 @@ abstract public static function assertThat($value, Constraint $constraint, strin
2222

2323
protected function assertDoesIterateLike(iterable $expected, iterable $actual, $message = '')
2424
{
25-
$this->assertThat($actual, new IteratesLike($expected), $message);
25+
$this->assertThat($actual, new IteratesLike($expected, fn ($item) => new IsEqual($item)), $message);
26+
}
27+
28+
protected function assertDoesIterateExactly(iterable $expected, iterable $actual, $message = '')
29+
{
30+
$this->assertThat($actual, new IteratesLike($expected, fn ($item) => new IsIdentical($item)), $message);
2631
}
2732

2833
protected function assertDoesNotIterateLike(iterable $expected, iterable $actual, $message = '')
2934
{
3035
$this->assertThat($actual, new LogicalNot(
31-
new IteratesLike($expected),
36+
new IteratesLike($expected, fn ($item) => new IsEqual($item)),
37+
), $message);
38+
}
39+
40+
protected function assertDoesNotIterateExactly(iterable $expected, iterable $actual, $message = '')
41+
{
42+
$this->assertThat($actual, new LogicalNot(
43+
new IteratesLike($expected, fn ($item) => new IsIdentical($item)),
3244
), $message);
3345
}
3446

0 commit comments

Comments
 (0)