Skip to content

Commit 299bfab

Browse files
committed
ArrayHasItemWith: Allow passing a static value as constraint
1 parent afc8c02 commit 299bfab

File tree

5 files changed

+68
-59
lines changed

5 files changed

+68
-59
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ $this->assertSequentialArray($data, 4, null, $this->is(IsType::TYPE_STRING));
229229

230230
The [`ArrayHasItemWith` constraint](https://github.com/PhrozenByte/phpunit-array-asserts/blob/master/src/Constraint/ArrayHasItemWith.php) asserts that an array has a item at a given index and that its value passes another constraint.
231231

232-
Accepts both native arrays and `Traversable` objects. The constraint will fail if the array has less items than required. The index of the item to check (parameter `$index`), and the constraint its value must pass (parameter `$constraint`) are passed in the constructor.
232+
Accepts both native arrays and `Traversable` objects. The constraint will fail if the array has less items than required. The index of the item to check (parameter `$index`), and the constraint its value must pass (parameter `$constraint`) are passed in the constructor. The constraint can either be an arbitrary `Constraint` instance (e.g. `PHPUnit\Framework\Constraint\StringContains`), or any static value, requiring an exact match of the value.
233233

234234
This constraint will fully traverse any `Traversable` object given. This also means that any `Generator` will be fully exhausted. It doesn't restore an `Iterator`'s pointer to its previous state.
235235

@@ -240,16 +240,16 @@ The `ArrayAssertsTrait` trait exposes two public methods for the `ArrayHasItemWi
240240
```php
241241
// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
242242
ArrayAssertsTrait::assertArrayHasItemWith(
243-
int $index, // the index of the item to check
244-
Constraint $constraint, // the constraint the item's value is applied to
245-
array|Traversable $array, // the array to check
246-
string $message = '' // additional information about the test
243+
int $index, // the index of the item to check
244+
Constraint|mixed $constraint, // the constraint the item's value is applied to
245+
array|Traversable $array, // the array to check
246+
string $message = '' // additional information about the test
247247
);
248248

249249
// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\ArrayHasItemWith`
250250
new ArrayHasItemWith(
251251
int $index,
252-
Constraint $constraint
252+
Constraint|mixed $constraint
253253
);
254254
```
255255

@@ -266,20 +266,20 @@ $data = [
266266
];
267267

268268
// asserts that `$data` contains "Life, the Universe and Everything" as third item (i.e. at index 2)
269-
$this->assertArrayHasItemWith(2, $this->identicalTo("Life, the Universe and Everything"));
269+
$this->assertArrayHasItemWith(2, "Life, the Universe and Everything");
270270
```
271271

272272
**Debugging:**
273273

274274
```php
275275
$data = [];
276276

277-
$this->assertArrayHasItemWith(2, $this->identicalTo('Arthur Dent'), $data);
277+
$this->assertArrayHasItemWith(2, 'Arthur Dent', $data);
278278

279279
// Will fail with the following message:
280280
//
281281
// Failed asserting that Array &0 () is an array that
282-
// has a value at index 2 which is identical to 'Arthur Dent'.
282+
// has a value at index 2 which is equal to 'Arthur Dent'.
283283
```
284284

285285
Example

src/ArrayAssertsTrait.php

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -190,37 +190,32 @@ public static function sequentialArray(int $minItems, int $maxItems = null, $con
190190
* passes another constraint.
191191
*
192192
* @param int $index the index of the item to check
193-
* @param Constraint $constraint the constraint the item's value is applied to
193+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
194194
* @param array|Traversable $array the array to check
195195
* @param string $message additional information about the test
196196
*
197197
* @throws ExpectationFailedException
198198
* @throws InvalidArgumentException
199199
* @throws Exception
200200
*/
201-
public static function assertArrayHasItemWith(
202-
int $index,
203-
Constraint $constraint,
204-
$array,
205-
string $message = ''
206-
): void {
201+
public static function assertArrayHasItemWith(int $index, $constraint, $array, string $message = ''): void
202+
{
207203
if (!(is_array($array) || ($array instanceof Traversable))) {
208204
throw InvalidArgumentException::create(3, 'array or Traversable');
209205
}
210-
211206
$constraint = new ArrayHasItemWith($index, $constraint);
212207
PHPUnitAssert::assertThat($array, $constraint, $message);
213208
}
214209

215210
/**
216211
* Returns a new instance of the ArrayHasItemWith constraint.
217212
*
218-
* @param int $index the index of the item to check
219-
* @param Constraint $constraint the constraint the item's value is applied to
213+
* @param int $index the index of the item to check
214+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
220215
*
221216
* @return ArrayHasItemWith
222217
*/
223-
public static function arrayHasItemWith(int $index, Constraint $constraint): ArrayHasItemWith
218+
public static function arrayHasItemWith(int $index, $constraint): ArrayHasItemWith
224219
{
225220
return new ArrayHasItemWith($index, $constraint);
226221
}

src/Constraint/ArrayHasItemWith.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
namespace PhrozenByte\PHPUnitArrayAsserts\Constraint;
2121

2222
use PHPUnit\Framework\Constraint\Constraint;
23+
use PHPUnit\Framework\Constraint\IsEqual;
2324
use Traversable;
2425

2526
/**
@@ -34,7 +35,9 @@
3435
* Iterator's pointer to its previous state.
3536
*
3637
* The index of the item to check, and the constraint its value must pass are
37-
* passed in the constructor.
38+
* passed in the constructor. The constraint can either be an arbitrary
39+
* `Constraint` instance (e.g. `PHPUnit\Framework\Constraint\StringContains`),
40+
* or any static value, requiring an exact match of the value.
3841
*/
3942
class ArrayHasItemWith extends Constraint
4043
{
@@ -47,13 +50,13 @@ class ArrayHasItemWith extends Constraint
4750
/**
4851
* ArrayHasItemWith constructor.
4952
*
50-
* @param int $index the index of the item to check
51-
* @param Constraint $constraint the constraint the item's value is applied to
53+
* @param int $index the index of the item to check
54+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
5255
*/
53-
public function __construct(int $index, Constraint $constraint)
56+
public function __construct(int $index, $constraint)
5457
{
5558
$this->index = $index;
56-
$this->constraint = $constraint;
59+
$this->constraint = !($constraint instanceof Constraint) ? new IsEqual($constraint) : $constraint;
5760
}
5861

5962
/**

tests/Unit/Constraint/ArrayHasItemWithTest.php

Lines changed: 26 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ class ArrayHasItemWithTest extends TestCase
3737
/**
3838
* @dataProvider dataProviderSelfDescribing
3939
*
40-
* @param int $index
41-
* @param Constraint $constraint
42-
* @param string $expectedDescription
40+
* @param int $index
41+
* @param Constraint|mixed $constraint
42+
* @param string $expectedDescription
4343
*/
44-
public function testSelfDescribing(int $index, Constraint $constraint, string $expectedDescription): void
44+
public function testSelfDescribing(int $index, $constraint, string $expectedDescription): void
4545
{
4646
$mockedConstraint = $this->mockConstraint($constraint, [ 'toString' => $this->once() ]);
4747

@@ -60,17 +60,13 @@ public function dataProviderSelfDescribing(): array
6060
/**
6161
* @dataProvider dataProviderEvaluate
6262
*
63-
* @param int $index
64-
* @param Constraint $constraint
65-
* @param mixed $other
66-
* @param mixed $expectedEvaluationValue
63+
* @param int $index
64+
* @param Constraint|mixed $constraint
65+
* @param mixed $other
66+
* @param mixed $expectedEvaluationValue
6767
*/
68-
public function testEvaluate(
69-
int $index,
70-
Constraint $constraint,
71-
$other,
72-
$expectedEvaluationValue
73-
): void {
68+
public function testEvaluate(int $index, $constraint, $other, $expectedEvaluationValue): void
69+
{
7470
$mockedConstraint = $this->mockConstraint(
7571
$constraint,
7672
[ 'evaluate' => $this->once() ],
@@ -96,15 +92,15 @@ public function dataProviderEvaluate(): array
9692
/**
9793
* @dataProvider dataProviderEvaluateFail
9894
*
99-
* @param int $index
100-
* @param Constraint $constraint
101-
* @param mixed $other
102-
* @param mixed $expectedEvaluationValue
103-
* @param string $expectedExceptionMessage
95+
* @param int $index
96+
* @param Constraint|mixed $constraint
97+
* @param mixed $other
98+
* @param mixed $expectedEvaluationValue
99+
* @param string $expectedExceptionMessage
104100
*/
105101
public function testEvaluateFail(
106102
int $index,
107-
Constraint $constraint,
103+
$constraint,
108104
$other,
109105
$expectedEvaluationValue,
110106
string $expectedExceptionMessage
@@ -135,17 +131,13 @@ public function dataProviderEvaluateFail(): array
135131
/**
136132
* @dataProvider dataProviderPreEvaluateFail
137133
*
138-
* @param int $index
139-
* @param Constraint $constraint
140-
* @param mixed $other
141-
* @param string $expectedExceptionMessage
134+
* @param int $index
135+
* @param Constraint|mixed $constraint
136+
* @param mixed $other
137+
* @param string $expectedExceptionMessage
142138
*/
143-
public function testPreEvaluateFail(
144-
int $index,
145-
Constraint $constraint,
146-
$other,
147-
string $expectedExceptionMessage
148-
): void {
139+
public function testPreEvaluateFail(int $index, $constraint, $other, string $expectedExceptionMessage): void
140+
{
149141
$mockedConstraint = $this->mockConstraint($constraint);
150142

151143
$itemConstraint = new ArrayHasItemWith($index, $mockedConstraint);
@@ -168,11 +160,11 @@ public function dataProviderPreEvaluateFail(): array
168160
/**
169161
* @dataProvider dataProviderCountable
170162
*
171-
* @param int $index
172-
* @param Constraint $constraint
173-
* @param int $expectedCount
163+
* @param int $index
164+
* @param Constraint|mixed $constraint
165+
* @param int $expectedCount
174166
*/
175-
public function testCountable(int $index, Constraint $constraint, int $expectedCount): void
167+
public function testCountable(int $index, $constraint, int $expectedCount): void
176168
{
177169
$mockedConstraint = $this->mockConstraint($constraint, [ 'count' => $this->once() ]);
178170

tests/data/ArrayHasItemWithTest.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@
3737
toString: matches something
3838
matches: false
3939
count: 2
40+
paramsIndexStatic: &paramsIndexStatic
41+
index: 0
42+
constraint: static value
4043
constraintMatches: &constraintMatches
4144
constraint: { options: { matches: true } }
4245

@@ -47,6 +50,8 @@ testSelfDescribing:
4750
expectedDescription: is an array that has a value at index 1 which is funny
4851
- <<: *paramsIndexZero
4952
expectedDescription: is an array that has a value at index 0 which matches something
53+
- <<: *paramsIndexStatic
54+
expectedDescription: is an array that has a value at index 0 which is equal to 'static value'
5055

5156
testEvaluate:
5257
- <<: *paramsIndexTwo
@@ -66,6 +71,11 @@ testEvaluate:
6671
~object: ArrayIterator
6772
array: { 2: "foo", 1: "bar", 0: "baz" }
6873
expectedEvaluationValue: foo
74+
- <<: *paramsIndexStatic
75+
other:
76+
item: static value
77+
other: 42
78+
expectedEvaluationValue: static value
6979
- index: 10
7080
constraint:
7181
~object: PhrozenByte\PHPUnitArrayAsserts\Tests\Utils\TestConstraint
@@ -89,6 +99,13 @@ testEvaluateFail:
8999
expectedEvaluationValue: foo
90100
expectedExceptionMessage: |-
91101
Failed asserting that %s is an array that has a value at index 0 which matches something.
102+
- <<: *paramsIndexStatic
103+
other:
104+
item: other value
105+
other: 42
106+
expectedEvaluationValue: other value
107+
expectedExceptionMessage: |-
108+
Failed asserting that %s is an array that has a value at index 0 which is equal to 'static value'.
92109
93110
testPreEvaluateFail:
94111
- <<: *paramsIndexTwo
@@ -133,3 +150,5 @@ testCountable:
133150
expectedCount: 2
134151
- <<: *paramsIndexZero
135152
expectedCount: 3
153+
- <<: *paramsIndexStatic
154+
expectedCount: 2

0 commit comments

Comments
 (0)