Skip to content

Commit 603202f

Browse files
[9.x] Add whereNot method to Fluent JSON testing matchers (#43383)
* Add whereNot method * Pint * fix test
1 parent 1d26aae commit 603202f

File tree

2 files changed

+191
-0
lines changed

2 files changed

+191
-0
lines changed

src/Illuminate/Testing/Fluent/Concerns/Matching.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,49 @@ public function where(string $key, $expected): self
4747
return $this;
4848
}
4949

50+
/**
51+
* Asserts that the property does not match the expected value.
52+
*
53+
* @param string $key
54+
* @param mixed|\Closure $expected
55+
* @return $this
56+
*/
57+
public function whereNot(string $key, $expected): self
58+
{
59+
$this->has($key);
60+
61+
$actual = $this->prop($key);
62+
63+
if ($expected instanceof Closure) {
64+
PHPUnit::assertFalse(
65+
$expected(is_array($actual) ? Collection::make($actual) : $actual),
66+
sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key))
67+
);
68+
69+
return $this;
70+
}
71+
72+
if ($expected instanceof Arrayable) {
73+
$expected = $expected->toArray();
74+
}
75+
76+
$this->ensureSorted($expected);
77+
$this->ensureSorted($actual);
78+
79+
PHPUnit::assertNotSame(
80+
$expected,
81+
$actual,
82+
sprintf(
83+
'Property [%s] contains a value that should be missing: [%s, %s]',
84+
$this->dotPath($key),
85+
$key,
86+
$expected
87+
)
88+
);
89+
90+
return $this;
91+
}
92+
5093
/**
5194
* Asserts that all properties match their expected values.
5295
*

tests/Testing/Fluent/AssertTest.php

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,96 @@ public function testAssertHasOnlyCountFailsScoped()
153153
});
154154
}
155155

156+
public function testAssertHasWithWhereNotDoesNotFail()
157+
{
158+
$assert = AssertableJson::fromArray([
159+
'data' => [
160+
[
161+
'id' => 1,
162+
'name' => 'Taylor',
163+
],
164+
[
165+
'id' => 2,
166+
'name' => 'Nuno',
167+
],
168+
],
169+
]);
170+
171+
$assert->has('data', function ($bar) {
172+
$bar->has(2)
173+
->each(fn ($json) => $json->whereNot('id', 3)->etc());
174+
});
175+
}
176+
177+
public function testAssertHasWithWhereNotFails()
178+
{
179+
$assert = AssertableJson::fromArray([
180+
'data' => [
181+
[
182+
'id' => 1,
183+
'name' => 'Taylor',
184+
],
185+
[
186+
'id' => 2,
187+
'name' => 'Mateus',
188+
],
189+
],
190+
]);
191+
192+
$this->expectException(AssertionFailedError::class);
193+
$this->expectExceptionMessage('Property [data.1.id] contains a value that should be missing: [id, 2]');
194+
195+
$assert->has('data', function ($bar) {
196+
$bar->has(2)
197+
->each(fn ($json) => $json->whereNot('id', 2)->etc());
198+
});
199+
}
200+
201+
public function testAssertHasWithWhereNotDoesNotFailClosure()
202+
{
203+
$assert = AssertableJson::fromArray([
204+
'data' => [
205+
[
206+
'id' => 1,
207+
'name' => 'Taylor',
208+
],
209+
[
210+
'id' => 2,
211+
'name' => 'Mateus',
212+
],
213+
],
214+
]);
215+
216+
$assert->has('data', function ($bar) {
217+
$bar->has(2)
218+
->each(fn ($json) => $json->whereNot('id', fn ($value) => $value === 3)->etc());
219+
});
220+
}
221+
222+
public function testAssertHasWithWhereNotFailsClosure()
223+
{
224+
$assert = AssertableJson::fromArray([
225+
'data' => [
226+
[
227+
'id' => 1,
228+
'name' => 'Taylor',
229+
],
230+
[
231+
'id' => 2,
232+
'name' => 'Mateus',
233+
],
234+
],
235+
]);
236+
237+
$this->expectException(AssertionFailedError::class);
238+
$this->expectExceptionMessage('Property [data.1.id] was marked as invalid using a closure.');
239+
240+
$assert->has('data', function ($bar) {
241+
$bar->has(2)
242+
->each(fn ($json) => $json->whereNot('id', fn ($value) => $value === 2)->etc());
243+
});
244+
}
245+
156246
public function testAssertCount()
157247
{
158248
$assert = AssertableJson::fromArray([
@@ -625,6 +715,64 @@ public function testAssertNestedWhereFailsWhenDoesNotMatchValue()
625715
$assert->where('example.nested', 'another-value');
626716
}
627717

718+
public function testAssertWhereDoesNotMatchValue()
719+
{
720+
$assert = AssertableJson::fromArray([
721+
'bar' => 'value',
722+
]);
723+
724+
$assert->whereNot('bar', 'different_value');
725+
}
726+
727+
public function testAssertWhereNotFailsWhenMatchingValue()
728+
{
729+
$assert = AssertableJson::fromArray([
730+
'bar' => 'value',
731+
]);
732+
733+
$this->expectException(AssertionFailedError::class);
734+
$this->expectExceptionMessage('Property [bar] contains a value that should be missing: [bar, value]');
735+
736+
$assert->whereNot('bar', 'value');
737+
}
738+
739+
public function testAssertWhereNotFailsWhenNotMissing()
740+
{
741+
$assert = AssertableJson::fromArray([
742+
'bar' => 'value',
743+
]);
744+
745+
$this->expectException(AssertionFailedError::class);
746+
$this->expectExceptionMessage('Property [baz] does not exist.');
747+
748+
$assert->whereNot('baz', 'value');
749+
}
750+
751+
public function testAssertWhereNotUsingClosure()
752+
{
753+
$assert = AssertableJson::fromArray([
754+
'bar' => 'baz',
755+
]);
756+
757+
$assert->whereNot('bar', function ($value) {
758+
return $value === 'foo';
759+
});
760+
}
761+
762+
public function testAssertWhereNotFailsWhenMatchesValueUsingClosure()
763+
{
764+
$assert = AssertableJson::fromArray([
765+
'bar' => 'baz',
766+
]);
767+
768+
$this->expectException(AssertionFailedError::class);
769+
$this->expectExceptionMessage('Property [bar] was marked as invalid using a closure.');
770+
771+
$assert->whereNot('bar', function ($value) {
772+
return $value === 'baz';
773+
});
774+
}
775+
628776
public function testScope()
629777
{
630778
$assert = AssertableJson::fromArray([

0 commit comments

Comments
 (0)