Skip to content

Commit 468fb74

Browse files
[12.x] feat: Add whereNull and whereNotNull to Assertablejson (#55131)
* add whereNull to AssertableJson * add whereNotNull to AssertableJson * whereNull custom exception message * Update Matching.php --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent d6915c1 commit 468fb74

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

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

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,52 @@ public function whereNot(string $key, $expected): self
9292
return $this;
9393
}
9494

95+
/**
96+
* Asserts that the property is null.
97+
*
98+
* @param string $key
99+
* @return $this
100+
*/
101+
public function whereNull(string $key): self
102+
{
103+
$this->has($key);
104+
105+
$actual = $this->prop($key);
106+
107+
PHPUnit::assertNull(
108+
$actual,
109+
sprintf(
110+
'Property [%s] should be null.',
111+
$this->dotPath($key),
112+
)
113+
);
114+
115+
return $this;
116+
}
117+
118+
/**
119+
* Asserts that the property is not null.
120+
*
121+
* @param string $key
122+
* @return $this
123+
*/
124+
public function whereNotNull(string $key): self
125+
{
126+
$this->has($key);
127+
128+
$actual = $this->prop($key);
129+
130+
PHPUnit::assertNotNull(
131+
$actual,
132+
sprintf(
133+
'Property [%s] should not be null.',
134+
$this->dotPath($key),
135+
)
136+
);
137+
138+
return $this;
139+
}
140+
95141
/**
96142
* Asserts that all properties match their expected values.
97143
*

tests/Testing/Fluent/AssertTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,72 @@ public function testAssertWhereFailsUsingBackedEnum()
580580
$assert->where('bar', BackedEnum::test_empty);
581581
}
582582

583+
public function testAssertWhereNullMatchesValue()
584+
{
585+
$assert = AssertableJson::fromArray([
586+
'bar' => null,
587+
]);
588+
589+
$assert->whereNull('bar');
590+
}
591+
592+
public function testAssertWhereNullFailsWhenNotNull()
593+
{
594+
$assert = AssertableJson::fromArray([
595+
'bar' => 'value',
596+
]);
597+
598+
$this->expectException(AssertionFailedError::class);
599+
$this->expectExceptionMessage('Property [bar] should be null.');
600+
601+
$assert->whereNull('bar');
602+
}
603+
604+
public function testAssertWhereNullFailsWhenMissing()
605+
{
606+
$assert = AssertableJson::fromArray([
607+
'bar' => 'value',
608+
]);
609+
610+
$this->expectException(AssertionFailedError::class);
611+
$this->expectExceptionMessage('Property [baz] does not exist.');
612+
613+
$assert->whereNull('baz');
614+
}
615+
616+
public function testAssertWhereNotNullMatchesValue()
617+
{
618+
$assert = AssertableJson::fromArray([
619+
'bar' => 'value',
620+
]);
621+
622+
$assert->whereNotNull('bar');
623+
}
624+
625+
public function testAssertWhereNotNullFailsWhenNull()
626+
{
627+
$assert = AssertableJson::fromArray([
628+
'bar' => null,
629+
]);
630+
631+
$this->expectException(AssertionFailedError::class);
632+
$this->expectExceptionMessage('Property [bar] should not be null.');
633+
634+
$assert->whereNotNull('bar');
635+
}
636+
637+
public function testAssertWhereNotNullFailsWhenMissing()
638+
{
639+
$assert = AssertableJson::fromArray([
640+
'bar' => 'value',
641+
]);
642+
643+
$this->expectException(AssertionFailedError::class);
644+
$this->expectExceptionMessage('Property [baz] does not exist.');
645+
646+
$assert->whereNotNull('baz');
647+
}
648+
583649
public function testAssertWhereContainsFailsWithEmptyValue()
584650
{
585651
$assert = AssertableJson::fromArray([]);

0 commit comments

Comments
 (0)