Skip to content

Commit b7e48fc

Browse files
committed
AssociativeArray::$allowAdditional now defaults to TRUE
1 parent dec5b02 commit b7e48fc

3 files changed

Lines changed: 31 additions & 25 deletions

File tree

‎README.md‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ All options do the same, the only difference is that the static class and trait
3333

3434
The `AssociativeArray` constraint asserts that a value is an associative array matching a given structure and that the array's items pass other constraints.
3535

36-
Any native array and `ArrayAccess` object is considered an associative array, no matter which keys they use. However, the array's items are applied to the matching constraint (parameter `$consotraints`). By default, additional items will fail the constraint (parameter `$allowAdditional`, defaults to `false`). The same is true when items are missing (parameter `$allowMissing`, defaults to `false`). The expected keys and constraints to apply, as well as whether additional and/or missing items should fail the constraint, are passed in the constructor.
36+
Any native array and `ArrayAccess` object is considered an associative array, no matter which keys they use. However, the array's items are applied to the matching constraint (parameter `$consotraints`). By default, missing items will fail the constraint (parameter `$allowMissing`, defaults to `false`). Additional items will be ignored by default (parameter `$allowAdditional`, defaults to `true`). If you want the constraint to fail when additional items exist, set this option to `true`, however, please note that this works for native arrays only. The expected keys and constraints to apply, as well as whether missing and/or additional items should fail the constraint, are passed in the constructor.
3737

3838
**Usage:**
3939

@@ -42,16 +42,16 @@ Any native array and `ArrayAccess` object is considered an associative array, no
4242
self::assertAssociativeArray(
4343
array $constraints, // an associative array with the expected keys and constraints to apply
4444
array|ArrayAccess $array, // the associative array to check
45-
bool $allowAdditional = false, // whether additional items should fail the constraint
4645
bool $allowMissing = false, // whether missing items should fail the constraint
46+
bool $allowAdditional = true, // whether additional items should fail the constraint
4747
string $message = '' // additional information about the test
4848
);
4949

5050
// using new instance of `\PhrozenByte\PHPUnitArrayAsserts\Constraint\AssociativeArray`
5151
new AssociativeArray(
5252
array $constraints,
53-
bool $allowAdditional = false,
54-
bool $allowMissing = false
53+
bool $allowMissing = false,
54+
bool $allowAdditional = true
5555
);
5656
```
5757

@@ -236,13 +236,13 @@ class MyTest extends TestCase
236236
$this->assertAssociativeArray([
237237
'id' => $this->isType(IsType::TYPE_INT),
238238
'name' => $this->identicalTo('Arthur Dent'),
239-
'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ], true)
239+
'options' => $this->associativeArray([ 'panic' => $this->isType(IsType::TYPE_BOOL) ])
240240
], $responseData['users'][0]);
241241
}
242242

243243
public function testWithoutPHPUnitArrayAsserts(): void
244244
{
245-
// 18 lines of pretty repetitive code to check the API response *without* PHPUnitArrayAsserts
245+
// 17 lines of pretty repetitive code to check the API response *without* PHPUnitArrayAsserts
246246

247247
// implement your test, the result is stored in $responseData
248248

@@ -261,7 +261,6 @@ class MyTest extends TestCase
261261
$this->assertGreaterThanOrEqual(1, count($responseData['users'])); // won't work for Traversable
262262

263263
$userData = $responseData['users'][0]; // we can't really rely on the existence of key "0" here :/
264-
$this->assertEmpty(array_diff_key($userData, [ 'id' => true, 'name' => true, 'options' => true ]));
265264

266265
$this->assertArrayHasKey('id', $userData);
267266
$this->assertIsInt($userData['id']);
@@ -273,7 +272,7 @@ class MyTest extends TestCase
273272
$this->assertIsArray($userData['options']);
274273

275274
$this->assertArrayHasKey('panic', $userData['options']);
276-
$this->assertIsBool($userData['options']['bool']);
275+
$this->assertIsBool($userData['options']['panic']);
277276
}
278277
}
279278
```

‎src/ArrayAssertsTrait.php‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,9 @@ trait ArrayAssertsTrait
3939
*
4040
* @param Constraint[] $constraints an associative array with the expected keys and constraints to apply
4141
* @param array|ArrayAccess $array the associative array to check
42-
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to FALSE)
4342
* @param bool $allowMissing whether missing items should fail the constraint (defaults to FALSE)
43+
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to TRUE);
44+
* this option works for native arrays only
4445
* @param string $message additional information about the test
4546
*
4647
* @throws ExpectationFailedException
@@ -50,8 +51,8 @@ trait ArrayAssertsTrait
5051
public static function assertAssociativeArray(
5152
array $constraints,
5253
$array,
53-
bool $allowAdditional = false,
5454
bool $allowMissing = false,
55+
bool $allowAdditional = true,
5556
string $message = ''
5657
): void {
5758
$isNoConstraint = static function ($constraint): bool { return !($constraint instanceof Constraint); };
@@ -63,27 +64,32 @@ public static function assertAssociativeArray(
6364
throw InvalidArgumentException::create(2, 'array or ArrayAccess');
6465
}
6566

66-
$constraint = new AssociativeArray($constraints, $allowAdditional, $allowMissing);
67+
if (!is_array($array) && !$allowAdditional) {
68+
throw InvalidArgumentException::create(2, 'array when argument #4 is set true');
69+
}
70+
71+
$constraint = new AssociativeArray($constraints, $allowMissing, $allowAdditional);
6772
PHPUnitAssert::assertThat($array, $constraint, $message);
6873
}
6974

7075
/**
7176
* Returns a new instance of the AssociativeArray constraint.
7277
*
7378
* @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)
7579
* @param bool $allowMissing whether missing items should fail the constraint (defaults to FALSE)
80+
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to TRUE);
81+
* this option works for native arrays only
7682
*
7783
* @return AssociativeArray
7884
*
7985
* @throws Exception
8086
*/
8187
public static function associativeArray(
8288
array $constraints,
83-
bool $allowAdditional = false,
84-
bool $allowMissing = false
89+
bool $allowMissing = false,
90+
bool $allowAdditional = true
8591
): AssociativeArray {
86-
return new AssociativeArray($constraints, $allowAdditional, $allowMissing);
92+
return new AssociativeArray($constraints, $allowMissing, $allowAdditional);
8793
}
8894

8995
/**

‎src/Constraint/AssociativeArray.php‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,20 @@ class AssociativeArray extends Constraint
4343
protected $constraints = [];
4444

4545
/** @var bool */
46-
protected $allowAdditional;
46+
protected $allowMissing;
4747

4848
/** @var bool */
49-
protected $allowMissing;
49+
protected $allowAdditional;
5050

5151
/**
5252
* AssociativeArray constructor.
5353
*
5454
* @param Constraint[] $constraints an associative array with the expected keys and constraints to apply
55-
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to FALSE)
5655
* @param bool $allowMissing whether missing items should fail the constraint (defaults to FALSE)
56+
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to TRUE);
57+
* this option works for native arrays only
5758
*/
58-
public function __construct(array $constraints, bool $allowAdditional = false, bool $allowMissing = false)
59+
public function __construct(array $constraints, bool $allowMissing = false, bool $allowAdditional = true)
5960
{
6061
foreach ($constraints as $key => $constraint) {
6162
if (!($constraint instanceof Constraint)) {
@@ -66,8 +67,8 @@ public function __construct(array $constraints, bool $allowAdditional = false, b
6667
$this->constraints[$key] = $constraint;
6768
}
6869

69-
$this->allowAdditional = $allowAdditional;
7070
$this->allowMissing = $allowMissing;
71+
$this->allowAdditional = $allowAdditional;
7172
}
7273

7374
/**
@@ -82,7 +83,7 @@ public function toString(): string
8283
}
8384

8485
return 'is an associative array that '
85-
. implode(!$this->allowMissing ? ' and ' : ' or ', $constraintDescriptions)
86+
. implode(!$this->allowMissing ? ' and ' : ' and/or ', $constraintDescriptions)
8687
. ($this->allowAdditional ? ' or any other item' : '');
8788
}
8889

@@ -104,7 +105,7 @@ protected function matches($other): bool
104105
}
105106
}
106107

107-
if (!$this->allowAdditional) {
108+
if (is_array($other) && !$this->allowAdditional) {
108109
return !array_diff_key($other, $this->constraints);
109110
}
110111

@@ -165,9 +166,9 @@ protected function additionalFailureDescription($other): string
165166

166167
$output = $table->getTable();
167168
$output .= sprintf(
168-
"[%s] Allow additional; [%s] Allow missing\n",
169-
$this->allowAdditional ? 'x' : ' ',
170-
$this->allowMissing ? 'x' : ' '
169+
"[%s] Allow missing; [%s] Allow additional\n",
170+
$this->allowMissing ? 'x' : ' ',
171+
$this->allowAdditional ? 'x' : ' '
171172
);
172173

173174
return $output;

0 commit comments

Comments
 (0)