Skip to content

Commit 9981c77

Browse files
[8.x] Add assertion to verify type of key in JSON (#36638)
* Add assertion to verify type of key in JSON * Add support for union types * Make StyleCI happy again * Update src/Illuminate/Testing/Fluent/Concerns/Matching.php Co-authored-by: Dries Vints <[email protected]> Co-authored-by: Dries Vints <[email protected]>
1 parent 3dbddc0 commit 9981c77

File tree

2 files changed

+179
-0
lines changed

2 files changed

+179
-0
lines changed

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,47 @@ public function whereAll(array $bindings): self
6262
return $this;
6363
}
6464

65+
/**
66+
* Asserts that the property is of the expected type.
67+
*
68+
* @param string $key
69+
* @param string|array $expected
70+
* @return $this
71+
*/
72+
public function whereType(string $key, $expected): self
73+
{
74+
$this->has($key);
75+
76+
$actual = $this->prop($key);
77+
78+
if (! is_array($expected)) {
79+
$expected = explode('|', $expected);
80+
}
81+
82+
PHPUnit::assertContains(
83+
strtolower(gettype($actual)),
84+
$expected,
85+
sprintf('Property [%s] is not of expected type [%s].', $this->dotPath($key), implode('|', $expected))
86+
);
87+
88+
return $this;
89+
}
90+
91+
/**
92+
* Asserts that all properties are of their expected types.
93+
*
94+
* @param array $bindings
95+
* @return $this
96+
*/
97+
public function whereAllType(array $bindings): self
98+
{
99+
foreach ($bindings as $key => $value) {
100+
$this->whereType($key, $value);
101+
}
102+
103+
return $this;
104+
}
105+
65106
/**
66107
* Ensures that all properties are sorted the same way, recursively.
67108
*

tests/Testing/Fluent/AssertTest.php

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,144 @@ public function testAssertWhereAllFailsWhenAtLeastOnePropDoesNotMatchValue()
704704
]);
705705
}
706706

707+
public function testAssertWhereTypeString()
708+
{
709+
$assert = AssertableJson::fromArray([
710+
'foo' => 'bar',
711+
]);
712+
713+
$assert->whereType('foo', 'string');
714+
}
715+
716+
public function testAssertWhereTypeInteger()
717+
{
718+
$assert = AssertableJson::fromArray([
719+
'foo' => 123,
720+
]);
721+
722+
$assert->whereType('foo', 'integer');
723+
}
724+
725+
public function testAssertWhereTypeBoolean()
726+
{
727+
$assert = AssertableJson::fromArray([
728+
'foo' => true,
729+
]);
730+
731+
$assert->whereType('foo', 'boolean');
732+
}
733+
734+
public function testAssertWhereTypeDouble()
735+
{
736+
$assert = AssertableJson::fromArray([
737+
'foo' => 12.3,
738+
]);
739+
740+
$assert->whereType('foo', 'double');
741+
}
742+
743+
public function testAssertWhereTypeArray()
744+
{
745+
$assert = AssertableJson::fromArray([
746+
'foo' => ['bar', 'baz'],
747+
'bar' => ['foo' => 'baz'],
748+
]);
749+
750+
$assert->whereType('foo', 'array');
751+
$assert->whereType('bar', 'array');
752+
}
753+
754+
public function testAssertWhereTypeNull()
755+
{
756+
$assert = AssertableJson::fromArray([
757+
'foo' => null,
758+
]);
759+
760+
$assert->whereType('foo', 'null');
761+
}
762+
763+
public function testAssertWhereAllType()
764+
{
765+
$assert = AssertableJson::fromArray([
766+
'one' => 'foo',
767+
'two' => 123,
768+
'three' => true,
769+
'four' => 12.3,
770+
'five' => ['foo', 'bar'],
771+
'six' => ['foo' => 'bar'],
772+
'seven' => null,
773+
]);
774+
775+
$assert->whereAllType([
776+
'one' => 'string',
777+
'two' => 'integer',
778+
'three' => 'boolean',
779+
'four' => 'double',
780+
'five' => 'array',
781+
'six' => 'array',
782+
'seven' => 'null',
783+
]);
784+
}
785+
786+
public function testAssertWhereTypeWhenWrongTypeIsGiven()
787+
{
788+
$assert = AssertableJson::fromArray([
789+
'foo' => 'bar',
790+
]);
791+
792+
$this->expectException(AssertionFailedError::class);
793+
$this->expectExceptionMessage('Property [foo] is not of expected type [integer].');
794+
795+
$assert->whereType('foo', 'integer');
796+
}
797+
798+
public function testAssertWhereTypeWithUnionTypes()
799+
{
800+
$firstAssert = AssertableJson::fromArray([
801+
'foo' => 'bar',
802+
]);
803+
804+
$secondAssert = AssertableJson::fromArray([
805+
'foo' => null,
806+
]);
807+
808+
$firstAssert->whereType('foo', ['string', 'null']);
809+
$secondAssert->whereType('foo', ['string', 'null']);
810+
}
811+
812+
public function testAssertWhereTypeWhenWrongUnionTypeIsGiven()
813+
{
814+
$assert = AssertableJson::fromArray([
815+
'foo' => 123,
816+
]);
817+
818+
$this->expectException(AssertionFailedError::class);
819+
$this->expectExceptionMessage('Property [foo] is not of expected type [string|null].');
820+
821+
$assert->whereType('foo', ['string', 'null']);
822+
}
823+
824+
public function testAssertWhereTypeWithPipeInUnionType()
825+
{
826+
$assert = AssertableJson::fromArray([
827+
'foo' => 'bar',
828+
]);
829+
830+
$assert->whereType('foo', 'string|null');
831+
}
832+
833+
public function testAssertWhereTypeWithPipeInWrongUnionType()
834+
{
835+
$assert = AssertableJson::fromArray([
836+
'foo' => 'bar',
837+
]);
838+
839+
$this->expectException(AssertionFailedError::class);
840+
$this->expectExceptionMessage('Property [foo] is not of expected type [integer|null].');
841+
842+
$assert->whereType('foo', 'integer|null');
843+
}
844+
707845
public function testAssertHasAll()
708846
{
709847
$assert = AssertableJson::fromArray([

0 commit comments

Comments
 (0)