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
Prev Previous commit
Next Next commit
Add support for union types
  • Loading branch information
svenluijten committed Mar 17, 2021
commit 0499e674259859a9940f3023ef3bff21a6e9da5b
17 changes: 11 additions & 6 deletions src/Illuminate/Testing/Fluent/Concerns/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Closure;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert as PHPUnit;

Expand Down Expand Up @@ -65,20 +66,24 @@ public function whereAll(array $bindings): self
/**
* Asserts that the property is of the expected type.
*
* @param string $key
* @param string $expected
* @param string $key
* @param string|array $expected
* @return $this
*/
public function whereType(string $key, string $expected): self
public function whereType(string $key, $expected): self
{
$this->has($key);

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

PHPUnit::assertSame(
$expected,
if (! is_array($expected)) {
$expected = explode('|', $expected);
}

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

return $this;
Expand Down
59 changes: 59 additions & 0 deletions tests/Testing/Fluent/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -783,6 +783,65 @@ public function testAssertWhereAllType()
]);
}

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [foo] is not of expected type [integer].');

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

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

$secondAssert = AssertableJson::fromArray([
'foo' => null,
]);

$firstAssert->whereType('foo', ['string', 'null']);
$secondAssert->whereType('foo', ['string', 'null']);
}

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [foo] is not of expected type [string|null].');

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

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

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

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [foo] is not of expected type [integer|null].');

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

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