Skip to content

Commit

Permalink
[9.x] Add whereNot method to Fluent JSON testing matchers (#43383)
Browse files Browse the repository at this point in the history
* Add whereNot method

* Pint

* fix test
  • Loading branch information
mateusjatenee authored Jul 27, 2022
1 parent 1d26aae commit 603202f
Show file tree
Hide file tree
Showing 2 changed files with 191 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/Illuminate/Testing/Fluent/Concerns/Matching.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,49 @@ public function where(string $key, $expected): self
return $this;
}

/**
* Asserts that the property does not match the expected value.
*
* @param string $key
* @param mixed|\Closure $expected
* @return $this
*/
public function whereNot(string $key, $expected): self
{
$this->has($key);

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

if ($expected instanceof Closure) {
PHPUnit::assertFalse(
$expected(is_array($actual) ? Collection::make($actual) : $actual),
sprintf('Property [%s] was marked as invalid using a closure.', $this->dotPath($key))
);

return $this;
}

if ($expected instanceof Arrayable) {
$expected = $expected->toArray();
}

$this->ensureSorted($expected);
$this->ensureSorted($actual);

PHPUnit::assertNotSame(
$expected,
$actual,
sprintf(
'Property [%s] contains a value that should be missing: [%s, %s]',
$this->dotPath($key),
$key,
$expected
)
);

return $this;
}

/**
* Asserts that all properties match their expected values.
*
Expand Down
148 changes: 148 additions & 0 deletions tests/Testing/Fluent/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,96 @@ public function testAssertHasOnlyCountFailsScoped()
});
}

public function testAssertHasWithWhereNotDoesNotFail()
{
$assert = AssertableJson::fromArray([
'data' => [
[
'id' => 1,
'name' => 'Taylor',
],
[
'id' => 2,
'name' => 'Nuno',
],
],
]);

$assert->has('data', function ($bar) {
$bar->has(2)
->each(fn ($json) => $json->whereNot('id', 3)->etc());
});
}

public function testAssertHasWithWhereNotFails()
{
$assert = AssertableJson::fromArray([
'data' => [
[
'id' => 1,
'name' => 'Taylor',
],
[
'id' => 2,
'name' => 'Mateus',
],
],
]);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [data.1.id] contains a value that should be missing: [id, 2]');

$assert->has('data', function ($bar) {
$bar->has(2)
->each(fn ($json) => $json->whereNot('id', 2)->etc());
});
}

public function testAssertHasWithWhereNotDoesNotFailClosure()
{
$assert = AssertableJson::fromArray([
'data' => [
[
'id' => 1,
'name' => 'Taylor',
],
[
'id' => 2,
'name' => 'Mateus',
],
],
]);

$assert->has('data', function ($bar) {
$bar->has(2)
->each(fn ($json) => $json->whereNot('id', fn ($value) => $value === 3)->etc());
});
}

public function testAssertHasWithWhereNotFailsClosure()
{
$assert = AssertableJson::fromArray([
'data' => [
[
'id' => 1,
'name' => 'Taylor',
],
[
'id' => 2,
'name' => 'Mateus',
],
],
]);

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [data.1.id] was marked as invalid using a closure.');

$assert->has('data', function ($bar) {
$bar->has(2)
->each(fn ($json) => $json->whereNot('id', fn ($value) => $value === 2)->etc());
});
}

public function testAssertCount()
{
$assert = AssertableJson::fromArray([
Expand Down Expand Up @@ -625,6 +715,64 @@ public function testAssertNestedWhereFailsWhenDoesNotMatchValue()
$assert->where('example.nested', 'another-value');
}

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

$assert->whereNot('bar', 'different_value');
}

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [bar] contains a value that should be missing: [bar, value]');

$assert->whereNot('bar', 'value');
}

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [baz] does not exist.');

$assert->whereNot('baz', 'value');
}

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

$assert->whereNot('bar', function ($value) {
return $value === 'foo';
});
}

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

$this->expectException(AssertionFailedError::class);
$this->expectExceptionMessage('Property [bar] was marked as invalid using a closure.');

$assert->whereNot('bar', function ($value) {
return $value === 'baz';
});
}

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

0 comments on commit 603202f

Please sign in to comment.