Skip to content

Commit c17bd77

Browse files
committed
Various small improvements
1 parent 2bb59a8 commit c17bd77

2 files changed

Lines changed: 9 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
PHPUnitArrayAssertions
22
======================
33

4-
`PHPUnitArrayAssertions` is a small [PHPUnit](https://phpunit.de/) extension to improve testing of PHP arrays and array-like data. It introduces the `AssociativeArray`, `ArrayHasKeyWith`, `SequentialArray`, and `ArrayHasItemWith` constraints. It is often used for API testing to assert whether an API result matches certain criteria - regarding both its structure, and the data.
4+
[`PHPUnitArrayAssertions`](https://github.com/PhrozenByte/phpunit-array-asserts) is a small [PHPUnit](https://phpunit.de/) extension to improve testing of PHP arrays and array-like data. It introduces the `AssociativeArray`, `ArrayHasKeyWith`, `SequentialArray`, and `ArrayHasItemWith` constraints. It is often used for API testing to assert whether an API result matches certain criteria - regarding both its structure, and the data.
55

66
This PHPUnit extension allows developers to test structure and data in single assertion, making test cases less repetitive and easier to understand. In some way it's an alternative to PHPUnit's `ArraySubset` constraint that was deprecated in PHPUnit 8 and removed in PHPUnit 9 - just way more powerful and less confusing. Refer to the ["Usage" section](#usage) and ["Example" section](#example) below for more info.
77

@@ -262,6 +262,8 @@ $this->assertArrayHasItemWith(2, $this->identicalTo('Arthur Dent'), $data);
262262
Example
263263
-------
264264

265+
Here's a (more or less) real-world example of `PHPUnitArrayAssertions`. Check out the `testWithPHPUnitArrayAsserts()` method to see how a complex API response is tested. For a comparison with an implementation utilizing just PHPUnit's core features, check out the `testWithoutPHPUnitArrayAsserts()` method. Without `PHPUnitArrayAssertions` you end up having 17 lines of pretty repetitive code, with this PHPUnit extension you can test the response with 7 lines of easy to understand code.
266+
265267
```php
266268
<?php
267269
declare(strict_types=1);

‎src/ArrayAssertsTrait.php‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,12 @@ public static function sequentialArray(
186186
* @throws InvalidArgumentException
187187
* @throws Exception
188188
*/
189-
public static function assertArrayHasItemWith(int $index, Constraint $constraint, $array, string $message = ''): void
190-
{
189+
public static function assertArrayHasItemWith(
190+
int $index,
191+
Constraint $constraint,
192+
$array,
193+
string $message = ''
194+
): void {
191195
if (!(is_array($array) || ($array instanceof Traversable))) {
192196
throw InvalidArgumentException::create(3, 'array or Traversable');
193197
}

0 commit comments

Comments
 (0)