Skip to content

Commit 19f34ab

Browse files
committed
Utilize PHPUnitThrowableAsserts's CallableProxy and CachedCallableProxy
1 parent d4a40fb commit 19f34ab

5 files changed

Lines changed: 134 additions & 85 deletions

File tree

tests/Unit/ArrayAssertsTraitTest.php

Lines changed: 73 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
use PhrozenByte\PHPUnitArrayAsserts\Constraint\AssociativeArray;
3333
use PhrozenByte\PHPUnitArrayAsserts\Constraint\SequentialArray;
3434
use PhrozenByte\PHPUnitArrayAsserts\Tests\TestCase;
35+
use PhrozenByte\PHPUnitThrowableAsserts\CachedCallableProxy;
3536
use Traversable;
3637

3738
/**
@@ -68,10 +69,15 @@ public function testAssociativeArray(
6869
[ $constraints, $allowMissing, $allowAdditional ]
6970
);
7071

71-
$this->assertCallableThrowsNot(function () use ($constraints, $allowMissing, $allowAdditional) {
72-
$itemConstraint = Assert::associativeArray($constraints, $allowMissing, $allowAdditional);
73-
$this->assertInstanceOf(AssociativeArray::class, $itemConstraint);
74-
}, InvalidArgumentException::class);
72+
$callableProxy = new CachedCallableProxy(
73+
[ Assert::class, 'associativeArray' ],
74+
$constraints,
75+
$allowMissing,
76+
$allowAdditional
77+
);
78+
79+
$this->assertCallableThrowsNot($callableProxy, InvalidArgumentException::class);
80+
$this->assertInstanceOf(AssociativeArray::class, $callableProxy->getReturnValue());
7581
}
7682

7783
/**
@@ -94,9 +100,16 @@ public function testAssertAssociativeArray(
94100
[ $array, '' ]
95101
);
96102

97-
$this->assertCallableThrowsNot(function () use ($constraints, $allowMissing, $allowAdditional, $array) {
98-
Assert::assertAssociativeArray($constraints, $array, $allowMissing, $allowAdditional);
99-
}, InvalidArgumentException::class);
103+
$this->assertCallableThrowsNot(
104+
$this->callableProxy(
105+
[ Assert::class, 'assertAssociativeArray' ],
106+
$constraints,
107+
$array,
108+
$allowMissing,
109+
$allowAdditional
110+
),
111+
InvalidArgumentException::class
112+
);
100113
}
101114

102115
/**
@@ -131,9 +144,17 @@ public function testAssertAssociativeArrayFail(
131144
[ $array, '' ]
132145
);
133146

134-
$this->assertCallableThrows(function () use ($constraints, $allowMissing, $allowAdditional, $array) {
135-
Assert::assertAssociativeArray($constraints, $array, $allowMissing, $allowAdditional);
136-
}, $expectedException, $expectedExceptionMessage);
147+
$this->assertCallableThrows(
148+
$this->callableProxy(
149+
[ Assert::class, 'assertAssociativeArray' ],
150+
$constraints,
151+
$array,
152+
$allowMissing,
153+
$allowAdditional
154+
),
155+
$expectedException,
156+
$expectedExceptionMessage
157+
);
137158
}
138159

139160
/**
@@ -159,10 +180,9 @@ public function testArrayHasKeyWith(
159180
[ $key, $constraint ]
160181
);
161182

162-
$this->assertCallableThrowsNot(function () use ($key, $constraint) {
163-
$itemConstraint = Assert::arrayHasKeyWith($key, $constraint);
164-
$this->assertInstanceOf(ArrayHasKeyWith::class, $itemConstraint);
165-
}, InvalidArgumentException::class);
183+
$callableProxy = new CachedCallableProxy([ Assert::class, 'arrayHasKeyWith' ], $key, $constraint);
184+
$this->assertCallableThrowsNot($callableProxy, InvalidArgumentException::class);
185+
$this->assertInstanceOf(ArrayHasKeyWith::class, $callableProxy->getReturnValue());
166186
}
167187

168188
/**
@@ -183,9 +203,10 @@ public function testAssertArrayHasKeyWith(
183203
[ $array, '' ]
184204
);
185205

186-
$this->assertCallableThrowsNot(function () use ($key, $constraint, $array) {
187-
Assert::assertArrayHasKeyWith($key, $constraint, $array);
188-
}, InvalidArgumentException::class);
206+
$this->assertCallableThrowsNot(
207+
$this->callableProxy([ Assert::class, 'assertArrayHasKeyWith' ], $key, $constraint, $array),
208+
InvalidArgumentException::class
209+
);
189210
}
190211

191212
/**
@@ -218,9 +239,11 @@ public function testAssertArrayHasKeyWithFail(
218239
[ $array, '' ]
219240
);
220241

221-
$this->assertCallableThrows(function () use ($key, $constraint, $array) {
222-
Assert::assertArrayHasKeyWith($key, $constraint, $array);
223-
}, $expectedException, $expectedExceptionMessage);
242+
$this->assertCallableThrows(
243+
$this->callableProxy([ Assert::class, 'assertArrayHasKeyWith' ], $key, $constraint, $array),
244+
$expectedException,
245+
$expectedExceptionMessage
246+
);
224247
}
225248

226249
/**
@@ -248,10 +271,15 @@ public function testSequentialArray(
248271
[ $minItems, $maxItems, $constraint ]
249272
);
250273

251-
$this->assertCallableThrowsNot(function () use ($minItems, $maxItems, $constraint) {
252-
$itemConstraint = Assert::sequentialArray($minItems, $maxItems, $constraint);
253-
$this->assertInstanceOf(SequentialArray::class, $itemConstraint);
254-
}, InvalidArgumentException::class);
274+
$callableProxy = new CachedCallableProxy(
275+
[ Assert::class, 'sequentialArray' ],
276+
$minItems,
277+
$maxItems,
278+
$constraint
279+
);
280+
281+
$this->assertCallableThrowsNot($callableProxy, InvalidArgumentException::class);
282+
$this->assertInstanceOf(SequentialArray::class, $callableProxy->getReturnValue());
255283
}
256284

257285
/**
@@ -274,9 +302,10 @@ public function testAssertSequentialArray(
274302
[ $array, '' ]
275303
);
276304

277-
$this->assertCallableThrowsNot(function () use ($minItems, $maxItems, $constraint, $array) {
278-
Assert::assertSequentialArray($array, $minItems, $maxItems, $constraint);
279-
}, InvalidArgumentException::class);
305+
$this->assertCallableThrowsNot(
306+
$this->callableProxy([ Assert::class, 'assertSequentialArray' ], $array, $minItems, $maxItems, $constraint),
307+
InvalidArgumentException::class
308+
);
280309
}
281310

282311
/**
@@ -311,9 +340,11 @@ public function testAssertSequentialArrayFail(
311340
[ $array, '' ]
312341
);
313342

314-
$this->assertCallableThrows(function () use ($minItems, $maxItems, $constraint, $array) {
315-
Assert::assertSequentialArray($array, $minItems, $maxItems, $constraint);
316-
}, $expectedException, $expectedExceptionMessage);
343+
$this->assertCallableThrows(
344+
$this->callableProxy([ Assert::class, 'assertSequentialArray' ], $array, $minItems, $maxItems, $constraint),
345+
$expectedException,
346+
$expectedExceptionMessage
347+
);
317348
}
318349

319350
/**
@@ -339,10 +370,9 @@ public function testArrayHasItemWith(
339370
[ $index, $constraint ]
340371
);
341372

342-
$this->assertCallableThrowsNot(function () use ($index, $constraint) {
343-
$itemConstraint = Assert::arrayHasItemWith($index, $constraint);
344-
$this->assertInstanceOf(ArrayHasItemWith::class, $itemConstraint);
345-
}, InvalidArgumentException::class);
373+
$callableProxy = new CachedCallableProxy([ Assert::class, 'arrayHasItemWith' ], $index, $constraint);
374+
$this->assertCallableThrowsNot($callableProxy, InvalidArgumentException::class);
375+
$this->assertInstanceOf(ArrayHasItemWith::class, $callableProxy->getReturnValue());
346376
}
347377

348378
/**
@@ -363,9 +393,10 @@ public function testAssertArrayHasItemWith(
363393
[ $array, '' ]
364394
);
365395

366-
$this->assertCallableThrowsNot(function () use ($index, $constraint, $array) {
367-
Assert::assertArrayHasItemWith($index, $constraint, $array);
368-
}, InvalidArgumentException::class);
396+
$this->assertCallableThrowsNot(
397+
$this->callableProxy([ Assert::class, 'assertArrayHasItemWith' ], $index, $constraint, $array),
398+
InvalidArgumentException::class
399+
);
369400
}
370401

371402
/**
@@ -398,9 +429,11 @@ public function testAssertArrayHasItemWithFail(
398429
[ $array, '' ]
399430
);
400431

401-
$this->assertCallableThrows(function () use ($index, $constraint, $array) {
402-
Assert::assertArrayHasItemWith($index, $constraint, $array);
403-
}, $expectedException, $expectedExceptionMessage);
432+
$this->assertCallableThrows(
433+
$this->callableProxy([ Assert::class, 'assertArrayHasItemWith' ], $index, $constraint, $array),
434+
$expectedException,
435+
$expectedExceptionMessage
436+
);
404437
}
405438

406439
/**

tests/Unit/Constraint/ArrayHasItemWithTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ public function testEvaluate(
7777

7878
$itemConstraint = new ArrayHasItemWith($index, $mockedConstraint);
7979

80-
$this->assertCallableThrowsNot(static function () use ($itemConstraint, $other) {
81-
$itemConstraint->evaluate($other);
82-
}, ExpectationFailedException::class);
80+
$this->assertCallableThrowsNot(
81+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
82+
ExpectationFailedException::class
83+
);
8384
}
8485

8586
/**
@@ -114,10 +115,11 @@ public function testEvaluateFail(
114115

115116
$itemConstraint = new ArrayHasItemWith($index, $mockedConstraint);
116117

117-
$expectedExceptionMessage = sprintf($expectedExceptionMessage, (new Exporter())->export($other));
118-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
119-
$itemConstraint->evaluate($other);
120-
}, ExpectationFailedException::class, $expectedExceptionMessage);
118+
$this->assertCallableThrows(
119+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
120+
ExpectationFailedException::class,
121+
sprintf($expectedExceptionMessage, (new Exporter())->export($other))
122+
);
121123
}
122124

123125
/**
@@ -146,10 +148,11 @@ public function testPreEvaluateFail(
146148

147149
$itemConstraint = new ArrayHasItemWith($index, $mockedConstraint);
148150

149-
$expectedExceptionMessage = sprintf($expectedExceptionMessage, (new Exporter())->export($other));
150-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
151-
$itemConstraint->evaluate($other);
152-
}, ExpectationFailedException::class, $expectedExceptionMessage);
151+
$this->assertCallableThrows(
152+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
153+
ExpectationFailedException::class,
154+
sprintf($expectedExceptionMessage, (new Exporter())->export($other))
155+
);
153156
}
154157

155158
/**

tests/Unit/Constraint/ArrayHasKeyWithTest.php

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,10 @@ public function testEvaluate(
104104

105105
$itemConstraint = new ArrayHasKeyWith($key, $mockedConstraint);
106106

107-
$this->assertCallableThrowsNot(static function () use ($itemConstraint, $other) {
108-
$itemConstraint->evaluate($other);
109-
}, ExpectationFailedException::class);
107+
$this->assertCallableThrowsNot(
108+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
109+
ExpectationFailedException::class
110+
);
110111
}
111112

112113
/**
@@ -141,10 +142,11 @@ public function testEvaluateFail(
141142

142143
$itemConstraint = new ArrayHasKeyWith($key, $mockedConstraint);
143144

144-
$expectedExceptionMessage = sprintf($expectedExceptionMessage, (new Exporter())->export($other));
145-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
146-
$itemConstraint->evaluate($other);
147-
}, ExpectationFailedException::class, $expectedExceptionMessage);
145+
$this->assertCallableThrows(
146+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
147+
ExpectationFailedException::class,
148+
sprintf($expectedExceptionMessage, (new Exporter())->export($other))
149+
);
148150
}
149151

150152
/**
@@ -169,10 +171,11 @@ public function testPreEvaluateFail($key, Constraint $constraint, $other, string
169171

170172
$itemConstraint = new ArrayHasKeyWith($key, $mockedConstraint);
171173

172-
$expectedExceptionMessage = sprintf($expectedExceptionMessage, (new Exporter())->export($other));
173-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
174-
$itemConstraint->evaluate($other);
175-
}, ExpectationFailedException::class, $expectedExceptionMessage);
174+
$this->assertCallableThrows(
175+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
176+
ExpectationFailedException::class,
177+
sprintf($expectedExceptionMessage, (new Exporter())->export($other))
178+
);
176179
}
177180

178181
/**

tests/Unit/Constraint/AssociativeArrayTest.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,10 @@ public function testEvaluate(
120120

121121
$itemConstraint = new AssociativeArray($mockedConstraints, $allowMissing, $allowAdditional);
122122

123-
$this->assertCallableThrowsNot(static function () use ($itemConstraint, $other) {
124-
$itemConstraint->evaluate($other);
125-
}, ExpectationFailedException::class);
123+
$this->assertCallableThrowsNot(
124+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
125+
ExpectationFailedException::class
126+
);
126127
}
127128

128129
/**
@@ -166,9 +167,11 @@ public function testEvaluateFail(
166167

167168
$itemConstraint = new AssociativeArray($mockedConstraints, $allowMissing, $allowAdditional);
168169

169-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
170-
$itemConstraint->evaluate($other);
171-
}, ExpectationFailedException::class, $expectedExceptionMessage);
170+
$this->assertCallableThrows(
171+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
172+
ExpectationFailedException::class,
173+
$expectedExceptionMessage
174+
);
172175
}
173176

174177
/**
@@ -199,9 +202,11 @@ public function testPreEvaluateFail(
199202

200203
$itemConstraint = new AssociativeArray($mockedConstraints, $allowMissing, $allowAdditional);
201204

202-
$this->assertCallableThrows(static function () use ($itemConstraint, $other) {
203-
$itemConstraint->evaluate($other);
204-
}, ExpectationFailedException::class, $expectedExceptionMessage);
205+
$this->assertCallableThrows(
206+
$this->callableProxy([ $itemConstraint, 'evaluate' ], $other),
207+
ExpectationFailedException::class,
208+
$expectedExceptionMessage
209+
);
205210
}
206211

207212
/**

0 commit comments

Comments
 (0)