Skip to content

Commit eaa3206

Browse files
committed
ArrayAssertsTrait: Add methods to create constraint instances
1 parent f84f00d commit eaa3206

2 files changed

Lines changed: 83 additions & 18 deletions

File tree

‎README.md‎

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,10 @@ $data = [
6868
// - "id" with a numeric value,
6969
// - "name" with the value "Arthur Dent", and
7070
// - "options" with another associative array with the key "panic", whose value must be a boolean
71-
self::assertAssociativeArray([
72-
'id' => new IsType(IsType::TYPE_INT),
73-
'name' => new IsIdentical('Arthur Dent'),
74-
'options' => new AssociativeArray([ 'panic' => new IsType(IsType::TYPE_BOOL) ], true)
71+
$this->assertAssociativeArray([
72+
'id' => $this->isType(IsType::TYPE_INT),
73+
'name' => $this->identicalTo('Arthur Dent'),
74+
'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ], true)
7575
], $data);
7676
```
7777

@@ -109,7 +109,7 @@ $data = [
109109
];
110110

111111
// asserts that $data has the item `name` with the value "Arthur Dent"
112-
self::ArrayHasKeyWith('name', new IsIdentical('Arthur Dent'), $data);
112+
$this->assertArrayHasKeyWith('name', $this->identicalTo('Arthur Dent'), $data);
113113
```
114114

115115
### Constraint `SequentialArray`
@@ -153,7 +153,7 @@ $data = [
153153
];
154154

155155
// asserts that `$data` is a non-empty sequential array with non-empty items
156-
self::assertSequentialArray($data, 1, null, self::logicalNot(new IsEmpty()));
156+
$this->assertSequentialArray($data, 1, null, $this->logicalNot($this->isEmpty()));
157157
```
158158

159159
### Constraint `ArrayHasItemWith`
@@ -195,7 +195,7 @@ $data = [
195195
];
196196

197197
// asserts that `$data` contains "Life, the Universe and Everything" as third item (i.e. at index 2)
198-
self::ArrayHasItemWith(2, new IsIdentical("Life, the Universe and Everything""));
198+
$this->assertArrayHasItemWith(2, $this->identicalTo("Life, the Universe and Everything"));
199199
```
200200

201201
Example
@@ -207,19 +207,18 @@ declare(strict_types=1);
207207

208208
namespace YourName\YourProject\Tests;
209209

210-
use PHPUnit\Framework\Constraint\IsIdentical;
211210
use PHPUnit\Framework\Constraint\IsType;
212211
use PHPUnit\Framework\TestCase;
213212
use PhrozenByte\PHPUnitArrayAsserts\ArrayAssertsTrait;
214-
use PhrozenByte\PHPUnitArrayAsserts\Constraint\AssociativeArray;
215-
use PhrozenByte\PHPUnitArrayAsserts\Constraint\SequentialArray;
216213

217214
class MyTest extends TestCase
218215
{
219216
use ArrayAssertsTrait;
220217

221218
public function testWithPHPUnitArrayAsserts(): void
222219
{
220+
// 7 lines of easy to understand code to check the API response *with* PHPUnitArrayAsserts
221+
223222
// implement your test, the result is stored in $responseData
224223

225224
$responseData = [
@@ -232,19 +231,19 @@ class MyTest extends TestCase
232231
]
233232
];
234233

235-
$this->assertArrayHasKeyWith('users', new SequentialArray(1), $responseData);
234+
$this->assertArrayHasKeyWith('users', $this->sequentialArray(1), $responseData);
236235

237236
$this->assertAssociativeArray([
238-
'id' => new IsType(IsType::TYPE_INT),
239-
'name' => new IsIdentical('Arthur Dent'),
240-
'options' => new AssociativeArray([ 'panic' => new IsType(IsType::TYPE_BOOL) ], true)
237+
'id' => $this->isType(IsType::TYPE_INT),
238+
'name' => $this->identicalTo('Arthur Dent'),
239+
'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ], true)
241240
], $responseData['users'][0]);
242-
243-
// 7 lines of easier to understand code to check the API response *with* PHPUnitArrayAsserts
244241
}
245242

246243
public function testWithoutPHPUnitArrayAsserts(): void
247244
{
245+
// 18 lines of pretty repetitive code to check the API response *without* PHPUnitArrayAsserts
246+
248247
// implement your test, the result is stored in $responseData
249248

250249
$responseData = [
@@ -275,8 +274,6 @@ class MyTest extends TestCase
275274

276275
$this->assertArrayHasKey('panic', $userData['options']);
277276
$this->assertIsBool($userData['options']['bool']);
278-
279-
// 18 lines of pretty repetitive code to check the API response *without* PHPUnitArrayAsserts
280277
}
281278
}
282279
```

‎src/ArrayAssertsTrait.php‎

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,25 @@ public static function assertAssociativeArray(
6767
PHPUnitAssert::assertThat($array, $constraint, $message);
6868
}
6969

70+
/**
71+
* Returns a new instance of the AssociativeArray constraint.
72+
*
73+
* @param Constraint[] $constraints an associative array with the expected keys and constraints to apply
74+
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to FALSE)
75+
* @param bool $allowMissing whether missing items should fail the constraint (defaults to FALSE)
76+
*
77+
* @return AssociativeArray
78+
*
79+
* @throws Exception
80+
*/
81+
public static function associativeArray(
82+
array $constraints,
83+
bool $allowAdditional = false,
84+
bool $allowMissing = false
85+
): AssociativeArray {
86+
return new AssociativeArray($constraints, $allowAdditional, $allowMissing);
87+
}
88+
7089
/**
7190
* Asserts that an array has a given key and that its value passes another
7291
* constraint.
@@ -94,6 +113,21 @@ public static function assertArrayHasKeyWith($key, Constraint $constraint, $arra
94113
PHPUnitAssert::assertThat($array, $constraint, $message);
95114
}
96115

116+
/**
117+
* Returns a new instance of the ArrayHasKeyWith constraint.
118+
*
119+
* @param int|string $key the key of the item to check
120+
* @param Constraint $constraint the constraint the item's value is applied to
121+
*
122+
* @return ArrayHasKeyWith
123+
*
124+
* @throws Exception
125+
*/
126+
public static function arrayHasKeyWith($key, Constraint $constraint): ArrayHasKeyWith
127+
{
128+
return new ArrayHasKeyWith($key, $constraint);
129+
}
130+
97131
/**
98132
* Asserts that a value is like a sequential array, has a minimum and/or
99133
* maximum number of items, and that all items pass another constraint.
@@ -135,6 +169,25 @@ public static function assertSequentialArray(
135169
PHPUnitAssert::assertThat($array, $constraint, $message);
136170
}
137171

172+
/**
173+
* Returns a new instance of the SequentialArray constraint.
174+
*
175+
* @param int $minItems required minimum number of items, defaults to 0
176+
* @param int|null $maxItems required maximum number of items, defaults to NULL (infinite)
177+
* @param Constraint|null $constraint optional constraint to apply all items to (defaults to NULL)
178+
*
179+
* @return SequentialArray
180+
*
181+
* @throws Exception
182+
*/
183+
public static function sequentialArray(
184+
int $minItems,
185+
int $maxItems = null,
186+
Constraint $constraint = null
187+
): SequentialArray {
188+
return new SequentialArray($minItems, $maxItems, $constraint);
189+
}
190+
138191
/**
139192
* Asserts that an array has a item at a given index and that its value
140193
* passes another constraint.
@@ -157,4 +210,19 @@ public static function assertArrayHasItemWith(int $index, Constraint $constraint
157210
$constraint = new ArrayHasItemWith($index, $constraint);
158211
PHPUnitAssert::assertThat($array, $constraint, $message);
159212
}
213+
214+
/**
215+
* Returns a new instance of the ArrayHasItemWith constraint.
216+
*
217+
* @param int $index the index of the item to check
218+
* @param Constraint $constraint the constraint the item's value is applied to
219+
*
220+
* @return ArrayHasItemWith
221+
*
222+
* @throws Exception
223+
*/
224+
public static function arrayHasItemWith(int $index, Constraint $constraint): ArrayHasItemWith
225+
{
226+
return new ArrayHasItemWith($index, $constraint);
227+
}
160228
}

0 commit comments

Comments
 (0)