From f0f79ab24491e27a72888fca9c54c97a0aabe233 Mon Sep 17 00:00:00 2001 From: ceceppa Date: Thu, 16 Jul 2020 07:34:43 +0100 Subject: [PATCH 1/3] feat(expect): add more methods --- src/Expectation.php | 40 ++++++++++++++++++++++++++ tests/.snapshots/success.txt | 26 +++++++++++++++-- tests/Expect/toBeDirectory.php | 17 +++++++++++ tests/Expect/toBeReadableDirectory.php | 15 ++++++++++ tests/Expect/toBeWritableDirectory.php | 15 ++++++++++ 5 files changed, 110 insertions(+), 3 deletions(-) create mode 100644 tests/Expect/toBeDirectory.php create mode 100644 tests/Expect/toBeReadableDirectory.php create mode 100644 tests/Expect/toBeWritableDirectory.php diff --git a/src/Expectation.php b/src/Expectation.php index 4aa2eee9..f46053fa 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -372,6 +372,46 @@ final class Expectation return $this; } + /** + * Assert that the value array has the key. + */ + public function toHaveKey(string $key): Expectation + { + Assert::assertArrayHasKey($key, $this->value); + + return $this; + } + + /** + * Assert that the value is a directory. + */ + public function toBeDirectory(): Expectation + { + Assert::assertDirectoryExists($this->value); + + return $this; + } + + /** + * Assert that the value is a directory and is readable. + */ + public function toBeReadableDirectory(): Expectation + { + Assert::assertDirectoryIsReadable($this->value); + + return $this; + } + + /** + * Assert that the value is a directory and is writable. + */ + public function toBeWritableDirectory(): Expectation + { + Assert::assertDirectoryIsWritable($this->value); + + return $this; + } + /** * Dynamically calls methods on the class without any arguments. * diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index af19b3e5..d9a8d83b 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -24,6 +24,11 @@ PASS Tests\Expect\toBeCallable ✓ pass ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeDirectory + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toBeEmpty @@ -99,6 +104,11 @@ PASS Tests\Expect\toBeObject ✓ pass ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeReadableDirectory + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toBeResource @@ -119,12 +129,22 @@ PASS Tests\Expect\toBeTrue ✓ strict comparisons ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeWritableDirectory + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toContain ✓ passes strings ✓ passes arrays ✓ failures + ✓ not failures + + PASS Tests\Expect\toCount + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toEqual @@ -137,7 +157,7 @@ ✓ failures ✓ not failures - PASS Tests\Expect\toHaveCount + PASS Tests\Expect\toHaveKey ✓ pass ✓ failures ✓ not failures @@ -312,5 +332,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 6 skipped, 183 passed - Time: 5.77s + Tests: 6 skipped, 195 passed + Time: 5.27s diff --git a/tests/Expect/toBeDirectory.php b/tests/Expect/toBeDirectory.php new file mode 100644 index 00000000..f30df144 --- /dev/null +++ b/tests/Expect/toBeDirectory.php @@ -0,0 +1,17 @@ +toBeDirectory(); +}); + +test('failures', function () { + expect('/random/path/whatever')->toBeDirectory(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('.')->not->toBeDirectory(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeReadableDirectory.php b/tests/Expect/toBeReadableDirectory.php new file mode 100644 index 00000000..88f96019 --- /dev/null +++ b/tests/Expect/toBeReadableDirectory.php @@ -0,0 +1,15 @@ +toBeWritableDirectory(); +}); + +test('failures', function () { + expect('/random/path/whatever')->toBeWritableDirectory(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(sys_get_temp_dir())->not->toBeWritableDirectory(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeWritableDirectory.php b/tests/Expect/toBeWritableDirectory.php new file mode 100644 index 00000000..88f96019 --- /dev/null +++ b/tests/Expect/toBeWritableDirectory.php @@ -0,0 +1,15 @@ +toBeWritableDirectory(); +}); + +test('failures', function () { + expect('/random/path/whatever')->toBeWritableDirectory(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(sys_get_temp_dir())->not->toBeWritableDirectory(); +})->throws(ExpectationFailedException::class); From 46e900e8d20785418341559cc2f44233949ea935 Mon Sep 17 00:00:00 2001 From: ceceppa Date: Thu, 16 Jul 2020 07:35:31 +0100 Subject: [PATCH 2/3] feat(expect): add more methods --- tests/.snapshots/success.txt | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index d9a8d83b..cf138b1c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -140,11 +140,6 @@ ✓ passes strings ✓ passes arrays ✓ failures - ✓ not failures - - PASS Tests\Expect\toCount - ✓ pass - ✓ failures ✓ not failures PASS Tests\Expect\toEqual @@ -155,6 +150,11 @@ PASS Tests\Expect\toEqualWithDelta ✓ pass ✓ failures + ✓ not failures + + PASS Tests\Expect\toHaveCount + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toHaveKey From 03201cb8b7e8243d4b216c46e4a3b986662892d3 Mon Sep 17 00:00:00 2001 From: ceceppa Date: Thu, 16 Jul 2020 07:57:05 +0100 Subject: [PATCH 3/3] feat(expect): add more methods --- tests/Expect/toEqualCanonicalizing.php | 16 ++++++++++++++++ tests/Expect/toHaveKey.php | 15 +++++++++++++++ 2 files changed, 31 insertions(+) create mode 100644 tests/Expect/toEqualCanonicalizing.php create mode 100644 tests/Expect/toHaveKey.php diff --git a/tests/Expect/toEqualCanonicalizing.php b/tests/Expect/toEqualCanonicalizing.php new file mode 100644 index 00000000..5e1178b9 --- /dev/null +++ b/tests/Expect/toEqualCanonicalizing.php @@ -0,0 +1,16 @@ +toEqualCanonicalizing([3, 1, 2]); + expect(['g', 'a', 'z'])->not->toEqualCanonicalizing(['a', 'z']); +}); + +test('failures', function () { + expect([3, 2, 1])->toEqualCanonicalizing([1, 2]); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(['a', 'b', 'c'])->not->toEqualCanonicalizing(['b', 'a', 'c']); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toHaveKey.php b/tests/Expect/toHaveKey.php new file mode 100644 index 00000000..1695688b --- /dev/null +++ b/tests/Expect/toHaveKey.php @@ -0,0 +1,15 @@ + 1, 'b', 'c' => 'world'])->toHaveKey('c'); +}); + +test('failures', function () { + expect(['a' => 1, 'b', 'c' => 'world'])->toHaveKey('hello'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(['a' => 1, 'hello' => 'world', 'c'])->not->toHaveKey('hello'); +})->throws(ExpectationFailedException::class);