Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Add assertion to verify type of key in JSON
  • Loading branch information
svenluijten committed Mar 17, 2021
commit f338f6963eb290af49d5707938e2367e8b9d8aa8
37 changes: 37 additions & 0 deletions src/Illuminate/Testing/Fluent/Concerns/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,43 @@ public function whereAll(array $bindings): self
return $this;
}

/**
* Asserts that the property is of the expected type.
*
* @param string $key
* @param string $expected
* @return $this
*/
public function whereType(string $key, string $expected): self
{
$this->has($key);

$actual = $this->prop($key);

PHPUnit::assertSame(
$expected,
strtolower(gettype($actual)),
sprintf('Property [%s] is not of expected type [%s].', $this->dotPath($key), $expected)
);

return $this;
}

/**
* Asserts that all properties are of their expected types.
*
* @param array $bindings
* @return $this
*/
public function whereAllType(array $bindings): self
{
foreach ($bindings as $key => $value) {
$this->whereType($key, $value);
}

return $this;
}

/**
* Ensures that all properties are sorted the same way, recursively.
*
Expand Down
79 changes: 79 additions & 0 deletions tests/Testing/Fluent/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,85 @@ public function testAssertWhereAllFailsWhenAtLeastOnePropDoesNotMatchValue()
]);
}

public function testAssertWhereTypeString()
{
$assert = AssertableJson::fromArray([
'foo' => 'bar',
]);

$assert->whereType('foo', 'string');
}

public function testAssertWhereTypeInteger()
{
$assert = AssertableJson::fromArray([
'foo' => 123,
]);

$assert->whereType('foo', 'integer');
}

public function testAssertWhereTypeBoolean()
{
$assert = AssertableJson::fromArray([
'foo' => true,
]);

$assert->whereType('foo', 'boolean');
}

public function testAssertWhereTypeDouble()
{
$assert = AssertableJson::fromArray([
'foo' => 12.3,
]);

$assert->whereType('foo', 'double');
}

public function testAssertWhereTypeArray()
{
$assert = AssertableJson::fromArray([
'foo' => ['bar', 'baz'],
'bar' => ['foo' => 'baz'],
]);

$assert->whereType('foo', 'array');
$assert->whereType('bar', 'array');
}

public function testAssertWhereTypeNull()
{
$assert = AssertableJson::fromArray([
'foo' => null,
]);

$assert->whereType('foo', 'null');
}

public function testAssertWhereAllType()
{
$assert = AssertableJson::fromArray([
'one' => 'foo',
'two' => 123,
'three' => true,
'four' => 12.3,
'five' => ['foo', 'bar'],
'six' => ['foo' => 'bar'],
'seven' => null,
]);

$assert->whereAllType([
'one' => 'string',
'two' => 'integer',
'three' => 'boolean',
'four' => 'double',
'five' => 'array',
'six' => 'array',
'seven' => 'null',
]);
}

public function testAssertHasAll()
{
$assert = AssertableJson::fromArray([
Expand Down