Skip to content

Commit 8e3c01a

Browse files
committed
Move argument checking from ArrayAssertsTrait to constraint constructors
1 parent 644cc39 commit 8e3c01a

4 files changed

Lines changed: 32 additions & 39 deletions

File tree

‎src/ArrayAssertsTrait.php‎

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,6 @@ public static function assertAssociativeArray(
5555
bool $allowAdditional = true,
5656
string $message = ''
5757
): void {
58-
$isNoConstraint = static function ($constraint): bool { return !($constraint instanceof Constraint); };
59-
if (!(is_array($constraints) && !array_filter($constraints, $isNoConstraint))) {
60-
throw InvalidArgumentException::create(1, sprintf('array of %s', Constraint::class));
61-
}
62-
6358
if (!(is_array($array) || ($array instanceof ArrayAccess))) {
6459
throw InvalidArgumentException::create(2, 'array or ArrayAccess');
6560
}
@@ -82,7 +77,7 @@ public static function assertAssociativeArray(
8277
*
8378
* @return AssociativeArray
8479
*
85-
* @throws Exception
80+
* @throws InvalidArgumentException
8681
*/
8782
public static function associativeArray(
8883
array $constraints,
@@ -107,10 +102,6 @@ public static function associativeArray(
107102
*/
108103
public static function assertArrayHasKeyWith($key, Constraint $constraint, $array, string $message = ''): void
109104
{
110-
if (!(is_int($key) || is_string($key))) {
111-
throw InvalidArgumentException::create(1, 'integer or string');
112-
}
113-
114105
if (!(is_array($array) || ($array instanceof ArrayAccess))) {
115106
throw InvalidArgumentException::create(3, 'array or ArrayAccess');
116107
}
@@ -127,7 +118,7 @@ public static function assertArrayHasKeyWith($key, Constraint $constraint, $arra
127118
*
128119
* @return ArrayHasKeyWith
129120
*
130-
* @throws Exception
121+
* @throws InvalidArgumentException
131122
*/
132123
public static function arrayHasKeyWith($key, Constraint $constraint): ArrayHasKeyWith
133124
{
@@ -155,18 +146,6 @@ public static function assertSequentialArray(
155146
Constraint $constraint = null,
156147
string $message = ''
157148
): void {
158-
if ($minItems < 0) {
159-
throw InvalidArgumentException::create(1, 'non-negative integer');
160-
}
161-
162-
if ($maxItems !== null) {
163-
if ($maxItems < 0) {
164-
throw InvalidArgumentException::create(2, 'non-negative integer');
165-
} elseif ($minItems > $maxItems) {
166-
throw InvalidArgumentException::create(2, 'integer not lesser than argument #2');
167-
}
168-
}
169-
170149
if (!(is_array($array) || ($array instanceof Traversable))) {
171150
throw InvalidArgumentException::create(3, 'array or Traversable');
172151
}
@@ -184,7 +163,7 @@ public static function assertSequentialArray(
184163
*
185164
* @return SequentialArray
186165
*
187-
* @throws Exception
166+
* @throws InvalidArgumentException
188167
*/
189168
public static function sequentialArray(
190169
int $minItems,
@@ -224,8 +203,6 @@ public static function assertArrayHasItemWith(int $index, Constraint $constraint
224203
* @param Constraint $constraint the constraint the item's value is applied to
225204
*
226205
* @return ArrayHasItemWith
227-
*
228-
* @throws Exception
229206
*/
230207
public static function arrayHasItemWith(int $index, Constraint $constraint): ArrayHasItemWith
231208
{

‎src/Constraint/ArrayHasKeyWith.php‎

Lines changed: 7 additions & 0 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\InvalidArgumentException;
2425

2526
/**
2627
* Constraint that asserts that an array has a given key and that its value
@@ -45,9 +46,15 @@ class ArrayHasKeyWith extends Constraint
4546
*
4647
* @param int|string $key the key of the item to check
4748
* @param Constraint $constraint the constraint the item's value is applied to
49+
*
50+
* @throws InvalidArgumentException
4851
*/
4952
public function __construct($key, Constraint $constraint)
5053
{
54+
if (!(is_int($key) || is_string($key))) {
55+
throw InvalidArgumentException::create(1, 'integer or string');
56+
}
57+
5158
$this->key = $key;
5259
$this->constraint = $constraint;
5360
}

‎src/Constraint/AssociativeArray.php‎

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
use ArrayAccess;
2323
use LucidFrame\Console\ConsoleTable;
2424
use PHPUnit\Framework\Constraint\Constraint;
25-
use PHPUnit\Framework\Exception as PHPUnitException;
25+
use PHPUnit\Framework\InvalidArgumentException;
2626

2727
/**
2828
* Constraint that asserts that a value is an associative array matching a
@@ -55,18 +55,17 @@ class AssociativeArray extends Constraint
5555
* @param bool $allowMissing whether missing items should fail the constraint (defaults to FALSE)
5656
* @param bool $allowAdditional whether additional items should fail the constraint (defaults to TRUE);
5757
* this option works for native arrays only
58+
*
59+
* @throws InvalidArgumentException
5860
*/
5961
public function __construct(array $constraints, bool $allowMissing = false, bool $allowAdditional = true)
6062
{
61-
foreach ($constraints as $key => $constraint) {
62-
if (!($constraint instanceof Constraint)) {
63-
$errorTemplate = 'All constraints of %s must be instances of %s.';
64-
throw new PHPUnitException(sprintf($errorTemplate, __CLASS__, Constraint::class));
65-
}
66-
67-
$this->constraints[$key] = $constraint;
63+
$isNoConstraint = static function ($constraint): bool { return !($constraint instanceof Constraint); };
64+
if (!(is_array($constraints) && !array_filter($constraints, $isNoConstraint))) {
65+
throw InvalidArgumentException::create(1, sprintf('array of %s', Constraint::class));
6866
}
6967

68+
$this->constraints = $constraints;
7069
$this->allowMissing = $allowMissing;
7170
$this->allowAdditional = $allowAdditional;
7271
}

‎src/Constraint/SequentialArray.php‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
use IteratorAggregate;
2626
use NoRewindIterator;
2727
use PHPUnit\Framework\Constraint\Constraint;
28-
use PHPUnit\Framework\Exception as PHPUnitException;
28+
use PHPUnit\Framework\InvalidArgumentException;
2929
use Traversable;
3030

3131
/**
@@ -62,16 +62,26 @@ class SequentialArray extends Constraint
6262
* @param int $minItems required minimum number of items, defaults to 0
6363
* @param int|null $maxItems required maximum number of items, defaults to NULL (infinite)
6464
* @param Constraint|null $constraint optional constraint to apply all items to (defaults to NULL)
65+
*
66+
* @throws InvalidArgumentException
6567
*/
6668
public function __construct(int $minItems = 0, int $maxItems = null, Constraint $constraint = null)
6769
{
70+
if ($minItems < 0) {
71+
throw InvalidArgumentException::create(1, 'non-negative integer');
72+
}
73+
74+
if ($maxItems !== null) {
75+
if ($maxItems < 0) {
76+
throw InvalidArgumentException::create(2, 'non-negative integer');
77+
} elseif ($minItems > $maxItems) {
78+
throw InvalidArgumentException::create(2, 'integer not lesser than argument #2');
79+
}
80+
}
81+
6882
$this->minItems = $minItems;
6983
$this->maxItems = $maxItems;
7084
$this->constraint = $constraint;
71-
72-
if (($this->minItems < 0) || (($this->maxItems !== null) && ($this->minItems > $this->maxItems))) {
73-
throw new PHPUnitException(sprintf('Count constraints of %s must not be contradictory.', __CLASS__));
74-
}
7585
}
7686

7787
/**

0 commit comments

Comments
 (0)