From a9cdf9332712438a321edbe86978f9f76067c08f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sat, 23 Jul 2022 12:08:05 -0300 Subject: [PATCH 1/3] Add whereNot method --- .../Testing/Fluent/Concerns/Matching.php | 43 +++++ tests/Testing/Fluent/AssertTest.php | 148 ++++++++++++++++++ 2 files changed, 191 insertions(+) diff --git a/src/Illuminate/Testing/Fluent/Concerns/Matching.php b/src/Illuminate/Testing/Fluent/Concerns/Matching.php index 949047b82bad..9ff7a31823ea 100644 --- a/src/Illuminate/Testing/Fluent/Concerns/Matching.php +++ b/src/Illuminate/Testing/Fluent/Concerns/Matching.php @@ -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. * diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index 8397603a7182..13df59d4e18c 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -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([ @@ -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->where('bar', function ($value) { + return $value === 'baz'; + }); + } + public function testScope() { $assert = AssertableJson::fromArray([ From 7f9361580ca75464720293d64ab0d8f27d7baac0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sat, 23 Jul 2022 12:08:38 -0300 Subject: [PATCH 2/3] Pint --- tests/Testing/Fluent/AssertTest.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index 13df59d4e18c..8ec5c6d37c29 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -159,12 +159,12 @@ public function testAssertHasWithWhereNotDoesNotFail() 'data' => [ [ 'id' => 1, - 'name' => 'Taylor' + 'name' => 'Taylor', ], [ 'id' => 2, - 'name' => 'Nuno' - ] + 'name' => 'Nuno', + ], ], ]); @@ -180,12 +180,12 @@ public function testAssertHasWithWhereNotFails() 'data' => [ [ 'id' => 1, - 'name' => 'Taylor' + 'name' => 'Taylor', ], [ 'id' => 2, - 'name' => 'Mateus' - ] + 'name' => 'Mateus', + ], ], ]); @@ -204,12 +204,12 @@ public function testAssertHasWithWhereNotDoesNotFailClosure() 'data' => [ [ 'id' => 1, - 'name' => 'Taylor' + 'name' => 'Taylor', ], [ 'id' => 2, - 'name' => 'Mateus' - ] + 'name' => 'Mateus', + ], ], ]); @@ -225,12 +225,12 @@ public function testAssertHasWithWhereNotFailsClosure() 'data' => [ [ 'id' => 1, - 'name' => 'Taylor' + 'name' => 'Taylor', ], [ 'id' => 2, - 'name' => 'Mateus' - ] + 'name' => 'Mateus', + ], ], ]); From 70f70c8fcb03e99ba9b79f3f26510ec173f91cfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mateus=20Guimar=C3=A3es?= Date: Sat, 23 Jul 2022 20:54:56 -0300 Subject: [PATCH 3/3] fix test --- tests/Testing/Fluent/AssertTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Testing/Fluent/AssertTest.php b/tests/Testing/Fluent/AssertTest.php index 8ec5c6d37c29..1e4f28e00b23 100644 --- a/tests/Testing/Fluent/AssertTest.php +++ b/tests/Testing/Fluent/AssertTest.php @@ -768,7 +768,7 @@ public function testAssertWhereNotFailsWhenMatchesValueUsingClosure() $this->expectException(AssertionFailedError::class); $this->expectExceptionMessage('Property [bar] was marked as invalid using a closure.'); - $assert->where('bar', function ($value) { + $assert->whereNot('bar', function ($value) { return $value === 'baz'; }); }