From f0f79ab24491e27a72888fca9c54c97a0aabe233 Mon Sep 17 00:00:00 2001 From: ceceppa Date: Thu, 16 Jul 2020 07:34:43 +0100 Subject: [PATCH] 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);