Skip to content

Commit ca93758

Browse files
committed
ArrayHasKeyWith: Allow passing a static value as constraint
1 parent 57b960d commit ca93758

File tree

5 files changed

+72
-55
lines changed

5 files changed

+72
-55
lines changed

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ $this->assertAssociativeArray([
118118

119119
The [`ArrayHasKeyWith` constraint](https://github.com/PhrozenByte/phpunit-array-asserts/blob/master/src/Constraint/ArrayHasKeyWith.php) asserts that an array has a given key and that its value passes another constraint.
120120

121-
Accepts both native arrays and `ArrayAccess` objects. The constraint (parameter `$constraint`) will fail if the key (parameter `$key`) doesn't exist in the array. The item's key and the constraint the value must pass are passed in the constructor.
121+
Accepts both native arrays and `ArrayAccess` objects. The constraint (parameter `$constraint`) will fail if the key (parameter `$key`) doesn't exist in the array. The item's key, and the constraint the value must pass 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.
122122

123123
The `ArrayAssertsTrait` trait exposes two public methods for the `ArrayHasKeyWith` constraint: Use `ArrayAssertsTrait::assertArrayHasKeyWith()` to perform an assertion, and `ArrayAssertsTrait::arrayHasKeyWith()` to create a new instance of the `ArrayHasKeyWith` constraint.
124124

@@ -127,16 +127,16 @@ The `ArrayAssertsTrait` trait exposes two public methods for the `ArrayHasKeyWit
127127
```php
128128
// using `\PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait` trait
129129
ArrayAssertsTrait::assertArrayHasKeyWith(
130-
string|int $key, // the key of the item to check
131-
Constraint $constraint, // the constraint the item's value is applied to
132-
array|ArrayAccess $array, // the array to check
133-
string $message = '' // additional information about the test
130+
string|int $key, // the key of the item to check
131+
Constraint|mixed $constraint, // the constraint the item's value is applied to
132+
array|ArrayAccess $array, // the array to check
133+
string $message = '' // additional information about the test
134134
);
135135

136136
// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\ArrayHasKeyWith`
137137
new ArrayHasKeyWith(
138138
string|int $key,
139-
Constraint $constraint
139+
Constraint|mixed $constraint
140140
);
141141
```
142142

@@ -150,20 +150,20 @@ $data = [
150150
];
151151

152152
// asserts that $data has the item `name` with the value "Arthur Dent"
153-
$this->assertArrayHasKeyWith('name', $this->identicalTo('Arthur Dent'), $data);
153+
$this->assertArrayHasKeyWith('name', 'Arthur Dent', $data);
154154
```
155155

156156
**Debugging:**
157157

158158
```php
159159
$data = [];
160160

161-
$this->assertArrayHasKeyWith('answer', $this->identicalTo(42), $data);
161+
$this->assertArrayHasKeyWith('answer', 42, $data);
162162

163163
// Will fail with the following message:
164164
//
165165
// Failed asserting that Array &0 () is an array that
166-
// has the key 'answer' whose value is identical to 42.
166+
// has the key 'answer' whose value is equal to 42.
167167
```
168168

169169
### Constraint `SequentialArray`

src/ArrayAssertsTrait.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,35 +107,35 @@ public static function associativeArray(
107107
* constraint.
108108
*
109109
* @param int|string $key the key of the item to check
110-
* @param Constraint $constraint the constraint the item's value is applied to
110+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
111111
* @param array|ArrayAccess $array the array to check
112112
* @param string $message additional information about the test
113113
*
114114
* @throws ExpectationFailedException
115115
* @throws InvalidArgumentException
116116
* @throws Exception
117117
*/
118-
public static function assertArrayHasKeyWith($key, Constraint $constraint, $array, string $message = ''): void
118+
public static function assertArrayHasKeyWith($key, $constraint, $array, string $message = ''): void
119119
{
120120
if (!(is_array($array) || ($array instanceof ArrayAccess))) {
121121
throw InvalidArgumentException::create(3, 'array or ArrayAccess');
122122
}
123123

124-
$constraint = new ArrayHasKeyWith($key, $constraint);
125-
PHPUnitAssert::assertThat($array, $constraint, $message);
124+
$itemConstraint = new ArrayHasKeyWith($key, $constraint);
125+
PHPUnitAssert::assertThat($array, $itemConstraint, $message);
126126
}
127127

128128
/**
129129
* Returns a new instance of the ArrayHasKeyWith constraint.
130130
*
131-
* @param int|string $key the key of the item to check
132-
* @param Constraint $constraint the constraint the item's value is applied to
131+
* @param int|string $key the key of the item to check
132+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
133133
*
134134
* @return ArrayHasKeyWith
135135
*
136136
* @throws InvalidArgumentException
137137
*/
138-
public static function arrayHasKeyWith($key, Constraint $constraint): ArrayHasKeyWith
138+
public static function arrayHasKeyWith($key, $constraint): ArrayHasKeyWith
139139
{
140140
return new ArrayHasKeyWith($key, $constraint);
141141
}

src/Constraint/ArrayHasKeyWith.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
use ArrayAccess;
2323
use PHPUnit\Framework\Constraint\Constraint;
24+
use PHPUnit\Framework\Constraint\IsEqual;
2425
use PHPUnit\Framework\InvalidArgumentException;
2526

2627
/**
@@ -31,7 +32,9 @@
3132
* if the key doesn't exist in the array.
3233
*
3334
* The item's key and the constraint the value must pass are passed in the
34-
* constructor.
35+
* constructor. The constraint can either be an arbitrary `Constraint` instance
36+
* (e.g. `PHPUnit\Framework\Constraint\StringContains`), or any static value,
37+
* requiring an exact match of the value.
3538
*/
3639
class ArrayHasKeyWith extends Constraint
3740
{
@@ -44,19 +47,19 @@ class ArrayHasKeyWith extends Constraint
4447
/**
4548
* ArrayHasKeyWith constructor.
4649
*
47-
* @param int|string $key the key of the item to check
48-
* @param Constraint $constraint the constraint the item's value is applied to
50+
* @param int|string $key the key of the item to check
51+
* @param Constraint|mixed $constraint the constraint the item's value is applied to
4952
*
5053
* @throws InvalidArgumentException
5154
*/
52-
public function __construct($key, Constraint $constraint)
55+
public function __construct($key, $constraint)
5356
{
5457
if (!(is_int($key) || is_string($key))) {
5558
throw InvalidArgumentException::create(1, 'integer or string');
5659
}
5760

5861
$this->key = $key;
59-
$this->constraint = $constraint;
62+
$this->constraint = !($constraint instanceof Constraint) ? new IsEqual($constraint) : $constraint;
6063
}
6164

6265
/**

tests/Unit/Constraint/ArrayHasKeyWithTest.php

Lines changed: 30 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ class ArrayHasKeyWithTest extends TestCase
3737
/**
3838
* @dataProvider dataProviderInvalidParameters
3939
*
40-
* @param string|int $key
41-
* @param Constraint $constraint
42-
* @param string $expectedException
43-
* @param string $expectedExceptionMessage
40+
* @param string|int $key
41+
* @param Constraint|mixed $constraint
42+
* @param string $expectedException
43+
* @param string $expectedExceptionMessage
4444
*/
4545
public function testInvalidParameters(
4646
$key,
47-
Constraint $constraint,
47+
$constraint,
4848
string $expectedException,
4949
string $expectedExceptionMessage
5050
): void {
@@ -64,11 +64,11 @@ public function dataProviderInvalidParameters(): array
6464
/**
6565
* @dataProvider dataProviderSelfDescribing
6666
*
67-
* @param string|int $key
68-
* @param Constraint $constraint
69-
* @param string $expectedDescription
67+
* @param string|int $key
68+
* @param Constraint|mixed $constraint
69+
* @param string $expectedDescription
7070
*/
71-
public function testSelfDescribing($key, Constraint $constraint, string $expectedDescription): void
71+
public function testSelfDescribing($key, $constraint, string $expectedDescription): void
7272
{
7373
$mockedConstraint = $this->mockConstraint($constraint, [ 'toString' => $this->once() ]);
7474

@@ -87,17 +87,13 @@ public function dataProviderSelfDescribing(): array
8787
/**
8888
* @dataProvider dataProviderEvaluate
8989
*
90-
* @param string|int $key
91-
* @param Constraint $constraint
92-
* @param mixed $other
93-
* @param mixed $expectedEvaluationValue
90+
* @param string|int $key
91+
* @param Constraint|mixed $constraint
92+
* @param mixed $other
93+
* @param mixed $expectedEvaluationValue
9494
*/
95-
public function testEvaluate(
96-
$key,
97-
Constraint $constraint,
98-
$other,
99-
$expectedEvaluationValue
100-
): void {
95+
public function testEvaluate($key, $constraint, $other, $expectedEvaluationValue): void
96+
{
10197
$mockedConstraint = $this->mockConstraint(
10298
$constraint,
10399
[ 'evaluate' => $this->once() ],
@@ -123,15 +119,15 @@ public function dataProviderEvaluate(): array
123119
/**
124120
* @dataProvider dataProviderEvaluateFail
125121
*
126-
* @param string|int $key
127-
* @param Constraint $constraint
128-
* @param mixed $other
129-
* @param mixed $expectedEvaluationValue
130-
* @param string $expectedExceptionMessage
122+
* @param string|int $key
123+
* @param Constraint|mixed $constraint
124+
* @param mixed $other
125+
* @param mixed $expectedEvaluationValue
126+
* @param string $expectedExceptionMessage
131127
*/
132128
public function testEvaluateFail(
133129
$key,
134-
Constraint $constraint,
130+
$constraint,
135131
$other,
136132
$expectedEvaluationValue,
137133
string $expectedExceptionMessage
@@ -162,12 +158,12 @@ public function dataProviderEvaluateFail(): array
162158
/**
163159
* @dataProvider dataProviderPreEvaluateFail
164160
*
165-
* @param string|int $key
166-
* @param Constraint $constraint
167-
* @param mixed $other
168-
* @param string $expectedExceptionMessage
161+
* @param string|int $key
162+
* @param Constraint|mixed $constraint
163+
* @param mixed $other
164+
* @param string $expectedExceptionMessage
169165
*/
170-
public function testPreEvaluateFail($key, Constraint $constraint, $other, string $expectedExceptionMessage): void
166+
public function testPreEvaluateFail($key, $constraint, $other, string $expectedExceptionMessage): void
171167
{
172168
$mockedConstraint = $this->mockConstraint($constraint);
173169

@@ -191,11 +187,11 @@ public function dataProviderPreEvaluateFail(): array
191187
/**
192188
* @dataProvider dataProviderCountable
193189
*
194-
* @param string|int $key
195-
* @param Constraint $constraint
196-
* @param int $expectedCount
190+
* @param string|int $key
191+
* @param Constraint|mixed $constraint
192+
* @param int $expectedCount
197193
*/
198-
public function testCountable($key, Constraint $constraint, int $expectedCount): void
194+
public function testCountable($key, $constraint, int $expectedCount): void
199195
{
200196
$mockedConstraint = $this->mockConstraint($constraint, [ 'count' => $this->once() ]);
201197

tests/data/ArrayHasKeyWithTest.yml

Lines changed: 18 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+
paramsKeyStatic: &paramsKeyStatic
41+
key: item
42+
constraint: static value
4043
constraintMatches: &constraintMatches
4144
constraint: { options: { matches: true } }
4245

@@ -57,6 +60,8 @@ testSelfDescribing:
5760
expectedDescription: is an array that has the key 'one' whose value is funny
5861
- <<: *paramsKeyZero
5962
expectedDescription: is an array that has the key 0 whose value matches something
63+
- <<: *paramsKeyStatic
64+
expectedDescription: is an array that has the key 'item' whose value is equal to 'static value'
6065

6166
testEvaluate:
6267
- <<: *paramsKeyTwo
@@ -81,6 +86,11 @@ testEvaluate:
8186
~object: PhrozenByte\PHPUnitArrayAsserts\Tests\Utils\ArrayAccessible
8287
data: [ 1, 2, 3 ]
8388
expectedEvaluationValue: 1
89+
- <<: *paramsKeyStatic
90+
other:
91+
item: static value
92+
other: 42
93+
expectedEvaluationValue: static value
8494
- key: 789
8595
constraint:
8696
~object: PhrozenByte\PHPUnitArrayAsserts\Tests\Utils\TestConstraint
@@ -124,6 +134,12 @@ testPreEvaluateFail:
124134
other: ~
125135
expectedExceptionMessage: |-
126136
Failed asserting that %s is an array that has the key 0 whose value matches something.
137+
- <<: *paramsKeyStatic
138+
other:
139+
item: other value
140+
other: 42
141+
expectedExceptionMessage: |-
142+
Failed asserting that %s is an array that has the key 'item' whose value is equal to 'static value'.
127143
- key: non-existing
128144
constraint:
129145
~object: PhrozenByte\PHPUnitArrayAsserts\Tests\Utils\TestConstraint
@@ -155,3 +171,5 @@ testCountable:
155171
expectedCount: 2
156172
- <<: *paramsKeyZero
157173
expectedCount: 3
174+
- <<: *paramsKeyStatic
175+
expectedCount: 2

0 commit comments

Comments
 (0)