Skip to content

Commit 58e2c5a

Browse files
committed
feat: Implements additionalFailureDescription with relevant information.
1 parent ef26266 commit 58e2c5a

File tree

2 files changed

+49
-12
lines changed

2 files changed

+49
-12
lines changed

src/Constraint/IsIdenticalIterable.php

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Iterator;
1313
use loophp\iterators\IterableIteratorAggregate;
1414
use loophp\iterators\MultipleIterableAggregate;
15+
use loophp\iterators\PackIterableAggregate;
1516
use MultipleIterator;
1617
use PHPUnit\Framework\Constraint\Constraint;
1718

@@ -42,6 +43,45 @@ public function toString(): string
4243
return 'has exactly the same keys and values';
4344
}
4445

46+
protected function additionalFailureDescription($other): string
47+
{
48+
[$subject, $other] = array_map(
49+
static fn (iterable $iterable): Iterator => (new IterableIteratorAggregate($iterable))->getIterator(),
50+
[$this->subject, $other]
51+
);
52+
53+
$mi = new PackIterableAggregate(
54+
new MultipleIterableAggregate([$subject, $other], MultipleIterator::MIT_NEED_ALL)
55+
);
56+
57+
foreach ($mi as $index => [$key, $value]) {
58+
if (0 !== $this->limit && $index >= $this->limit) {
59+
break;
60+
}
61+
62+
if ($key[0] !== $key[1]) {
63+
return sprintf('Expected iterable key is different from subject key at index %s', $index);
64+
}
65+
66+
if ($value[0] !== $value[1]) {
67+
return sprintf('Expected iterable value is different from subject value at index %s', $index);
68+
}
69+
}
70+
71+
$subjectValid = $subject->valid();
72+
$otherValid = $other->valid();
73+
74+
if ($subjectValid !== $otherValid) {
75+
if (true === $subjectValid) {
76+
return 'Expected iterable has more items than subject.';
77+
}
78+
79+
return 'Expected iterable has lesser items than subject.';
80+
}
81+
82+
return '';
83+
}
84+
4585
/**
4686
* @param iterable<TKey, T> $other
4787
*/
@@ -52,11 +92,11 @@ protected function matches($other): bool
5292
[$this->subject, $other]
5393
);
5494

55-
$mi = new MultipleIterableAggregate([$subject, $other], MultipleIterator::MIT_NEED_ALL);
56-
57-
$index = 0;
95+
$mi = new PackIterableAggregate(
96+
new MultipleIterableAggregate([$subject, $other], MultipleIterator::MIT_NEED_ALL)
97+
);
5898

59-
foreach ($mi as $key => $value) {
99+
foreach ($mi as $index => [$key, $value]) {
60100
if (0 !== $this->limit && $index >= $this->limit) {
61101
break;
62102
}
@@ -68,8 +108,6 @@ protected function matches($other): bool
68108
if ($value[0] !== $value[1]) {
69109
return false;
70110
}
71-
72-
++$index;
73111
}
74112

75113
if (0 === $this->limit) {

src/Constraint/IsNotIdenticalIterable.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Iterator;
1313
use loophp\iterators\IterableIteratorAggregate;
1414
use loophp\iterators\MultipleIterableAggregate;
15+
use loophp\iterators\PackIterableAggregate;
1516
use MultipleIterator;
1617
use PHPUnit\Framework\Constraint\Constraint;
1718

@@ -52,11 +53,11 @@ protected function matches($other): bool
5253
[$this->subject, $other]
5354
);
5455

55-
$mi = new MultipleIterableAggregate([$subject, $other], MultipleIterator::MIT_NEED_ALL);
56-
57-
$index = 0;
56+
$mi = new PackIterableAggregate(
57+
new MultipleIterableAggregate([$subject, $other], MultipleIterator::MIT_NEED_ALL)
58+
);
5859

59-
foreach ($mi as $key => $value) {
60+
foreach ($mi as $index => [$key, $value]) {
6061
if (0 !== $this->limit && $index >= $this->limit) {
6162
break;
6263
}
@@ -68,8 +69,6 @@ protected function matches($other): bool
6869
if ($value[0] !== $value[1]) {
6970
return true;
7071
}
71-
72-
++$index;
7372
}
7473

7574
if (0 === $this->limit) {

0 commit comments

Comments
 (0)