Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[9.x] Add whereNot method to Fluent JSON testing matchers #43383

Merged
merged 3 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
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