From 819825bdd244e9dff73bb17207c0d8a9c257729f Mon Sep 17 00:00:00 2001 From: ceceppa Date: Tue, 14 Jul 2020 08:08:13 +0100 Subject: [PATCH] feat(expect): add more methods --- src/Expectation.php | 142 ++++++++++++++++-- tests/.snapshots/success.txt | 78 ++++++++-- tests/Expect/{ToBe.php => toBe.php} | 0 ...oBeReadableDirectory.php => toBeArray.php} | 7 +- ...toBeWritableDirectory.php => toBeBool.php} | 7 +- tests/Expect/toBeCallable.php | 18 +++ ...oBeExistingDirectory.php => toBeFloat.php} | 9 +- tests/Expect/toBeInfinite.php | 16 ++ tests/Expect/toBeInstanceOf.php | 16 ++ tests/Expect/toBeInt.php | 16 ++ tests/Expect/toBeIterable.php | 23 +++ tests/Expect/toBeNAN.php | 16 ++ tests/Expect/toBeNull.php | 16 ++ tests/Expect/toBeNumeric.php | 16 ++ tests/Expect/toBeObject.php | 16 ++ tests/Expect/toBeResource.php | 22 +++ tests/Expect/toBeScalar.php | 15 ++ tests/Expect/toBeString.php | 16 ++ 18 files changed, 419 insertions(+), 30 deletions(-) rename tests/Expect/{ToBe.php => toBe.php} (100%) rename tests/Expect/{toBeReadableDirectory.php => toBeArray.php} (58%) rename tests/Expect/{toBeWritableDirectory.php => toBeBool.php} (58%) create mode 100644 tests/Expect/toBeCallable.php rename tests/Expect/{toBeExistingDirectory.php => toBeFloat.php} (57%) create mode 100644 tests/Expect/toBeInfinite.php create mode 100644 tests/Expect/toBeInstanceOf.php create mode 100644 tests/Expect/toBeInt.php create mode 100644 tests/Expect/toBeIterable.php create mode 100644 tests/Expect/toBeNAN.php create mode 100644 tests/Expect/toBeNull.php create mode 100644 tests/Expect/toBeNumeric.php create mode 100644 tests/Expect/toBeObject.php create mode 100644 tests/Expect/toBeResource.php create mode 100644 tests/Expect/toBeScalar.php create mode 100644 tests/Expect/toBeString.php diff --git a/src/Expectation.php b/src/Expectation.php index d8f36164..db123468 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -230,7 +230,7 @@ final class Expectation /** * Assert that the absolute difference between $value and $this->value - * is greather thatn $delta. + * is greater than $delta. * * @param mixed $value */ @@ -242,31 +242,153 @@ final class Expectation } /** - * Assert that the value is a directory. + * Assert that the value infinite. */ - public function toBeExistingDirectory(): Expectation + public function toBeInfinite(): Expectation { - Assert::assertDirectoryExists($this->value); + Assert::assertInfinite($this->value); return $this; } /** - * Assert that the value is a directory and is readable. + * Assert that the value is an instance of $value. + * + * @param mixed $value */ - public function toBeReadableDirectory(): Expectation + public function toBeInstanceOf($value): Expectation { - Assert::assertDirectoryIsReadable($this->value); + Assert::assertInstanceOf($value, $this->value); return $this; } /** - * Assert that the value is a directory and is writable. + * Assert that the value is an array. */ - public function toBeWritableDirectory(): Expectation + public function toBeArray(): Expectation { - Assert::assertDirectoryIsWritable($this->value); + Assert::assertIsArray($this->value); + + return $this; + } + + /** + * Assert that the value is of type bool. + */ + public function toBeBool(): Expectation + { + Assert::assertIsBool($this->value); + + return $this; + } + + /** + * Assert that the value is of type callable. + */ + public function toBeCallable(): Expectation + { + Assert::assertIsCallable($this->value); + + return $this; + } + + /** + * Assert that the value is type of float. + */ + public function toBeFloat(): Expectation + { + Assert::assertIsFloat($this->value); + + return $this; + } + + /** + * Assert that the value is type of int. + */ + public function toBeInt(): Expectation + { + Assert::assertIsInt($this->value); + + return $this; + } + + /** + * Assert that the value is type of iterable. + */ + public function toBeIterable(): Expectation + { + Assert::assertIsIterable($this->value); + + return $this; + } + + /** + * Assert that the value is type of numeric. + */ + public function toBeNumeric(): Expectation + { + Assert::assertIsNumeric($this->value); + + return $this; + } + + /** + * Assert that the value is type of object. + */ + public function toBeObject(): Expectation + { + Assert::assertIsObject($this->value); + + return $this; + } + + /** + * Assert that the value is type of resource. + */ + public function toBeResource(): Expectation + { + Assert::assertIsResource($this->value); + + return $this; + } + + /** + * Assert that the value is type of scalar. + */ + public function toBeScalar(): Expectation + { + Assert::assertIsScalar($this->value); + + return $this; + } + + /** + * Assert that the value is type of string. + */ + public function toBeString(): Expectation + { + Assert::assertIsString($this->value); + + return $this; + } + + /** + * Assert that the value is NAN. + */ + public function toBeNan(): Expectation + { + Assert::assertNan($this->value); + + return $this; + } + + /** + * Assert that the value is NAN. + */ + public function toBeNull(): Expectation + { + Assert::assertNull($this->value); return $this; } diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index fe3b6350..916c0d6c 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -2,9 +2,24 @@ PASS Tests\CustomTestCase\ExecutedTest ✓ that gets executed - PASS Tests\Expect\ToBe + PASS Tests\Expect\toBe ✓ strict comparisons ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeArray + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeBool + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeCallable + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toBeEmpty @@ -12,13 +27,13 @@ ✓ failures ✓ not failures - PASS Tests\Expect\toBeExistingDirectory - ✓ pass + PASS Tests\Expect\toBeFalse + ✓ strict comparisons ✓ failures ✓ not failures - PASS Tests\Expect\toBeFalse - ✓ strict comparisons + PASS Tests\Expect\toBeFloat + ✓ pass ✓ failures ✓ not failures @@ -30,6 +45,26 @@ PASS Tests\Expect\toBeGreatherThanOrEqual ✓ passes ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeInfinite + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeInstanceOf + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeInt + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeIterable + ✓ pass + ✓ failures ✓ not failures PASS Tests\Expect\toBeLessThan @@ -42,12 +77,37 @@ ✓ failures ✓ not failures - PASS Tests\Expect\toBeReadableDirectory + PASS Tests\Expect\toBeNAN ✓ pass ✓ failures ✓ not failures - PASS Tests\Expect\toBeWritableDirectory + PASS Tests\Expect\toBeNull + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeNumeric + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeObject + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeResource + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeScalar + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeString ✓ pass ✓ failures ✓ not failures @@ -267,5 +327,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 6 skipped, 156 passed - Time: 4.42s + Tests: 6 skipped, 192 passed + Time: 5.20s diff --git a/tests/Expect/ToBe.php b/tests/Expect/toBe.php similarity index 100% rename from tests/Expect/ToBe.php rename to tests/Expect/toBe.php diff --git a/tests/Expect/toBeReadableDirectory.php b/tests/Expect/toBeArray.php similarity index 58% rename from tests/Expect/toBeReadableDirectory.php rename to tests/Expect/toBeArray.php index 704109b5..3fcec231 100644 --- a/tests/Expect/toBeReadableDirectory.php +++ b/tests/Expect/toBeArray.php @@ -3,13 +3,14 @@ use PHPUnit\Framework\ExpectationFailedException; test('pass', function () { - expect(sys_get_temp_dir())->toBeReadableDirectory(); + expect([1, 2, 3])->toBeArray(); + expect('1, 2, 3')->not->toBeArray(); }); test('failures', function () { - expect('/random/path/whatever')->toBeReadableDirectory(); + expect(null)->toBeArray(); })->throws(ExpectationFailedException::class); test('not failures', function () { - expect(sys_get_temp_dir())->not->toBeReadableDirectory(); + expect(['a', 'b', 'c'])->not->toBeArray(); })->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeWritableDirectory.php b/tests/Expect/toBeBool.php similarity index 58% rename from tests/Expect/toBeWritableDirectory.php rename to tests/Expect/toBeBool.php index 88f96019..5dc37b21 100644 --- a/tests/Expect/toBeWritableDirectory.php +++ b/tests/Expect/toBeBool.php @@ -3,13 +3,14 @@ use PHPUnit\Framework\ExpectationFailedException; test('pass', function () { - expect(sys_get_temp_dir())->toBeWritableDirectory(); + expect(true)->toBeBool(); + expect(0)->not->toBeBool(); }); test('failures', function () { - expect('/random/path/whatever')->toBeWritableDirectory(); + expect(null)->toBeBool(); })->throws(ExpectationFailedException::class); test('not failures', function () { - expect(sys_get_temp_dir())->not->toBeWritableDirectory(); + expect(false)->not->toBeBool(); })->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeCallable.php b/tests/Expect/toBeCallable.php new file mode 100644 index 00000000..2bd8f139 --- /dev/null +++ b/tests/Expect/toBeCallable.php @@ -0,0 +1,18 @@ +toBeCallable(); + expect(null)->not->toBeCallable(); +}); + +test('failures', function () { + $hello = 5; + + expect($hello)->toBeCallable(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(function () { return 42; })->not->toBeCallable(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeExistingDirectory.php b/tests/Expect/toBeFloat.php similarity index 57% rename from tests/Expect/toBeExistingDirectory.php rename to tests/Expect/toBeFloat.php index 6f2eb5f9..69aa2306 100644 --- a/tests/Expect/toBeExistingDirectory.php +++ b/tests/Expect/toBeFloat.php @@ -3,15 +3,14 @@ use PHPUnit\Framework\ExpectationFailedException; test('pass', function () { - $temp = sys_get_temp_dir(); - - expect($temp)->toBeExistingDirectory(); + expect(1.0)->toBeFloat(); + expect(1)->not->toBeFloat(); }); test('failures', function () { - expect('/random/path/whatever')->toBeExistingDirectory(); + expect(42)->toBeFloat(); })->throws(ExpectationFailedException::class); test('not failures', function () { - expect('.')->not->toBeExistingDirectory(); + expect(log(3))->not->toBeFloat(); })->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeInfinite.php b/tests/Expect/toBeInfinite.php new file mode 100644 index 00000000..ebcb0a36 --- /dev/null +++ b/tests/Expect/toBeInfinite.php @@ -0,0 +1,16 @@ +toBeInfinite(); + expect(log(1))->not->toBeInfinite(); +}); + +test('failures', function () { + expect(asin(2))->toBeInfinite(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(INF)->not->toBeInfinite(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeInstanceOf.php b/tests/Expect/toBeInstanceOf.php new file mode 100644 index 00000000..03b3e881 --- /dev/null +++ b/tests/Expect/toBeInstanceOf.php @@ -0,0 +1,16 @@ +toBeInstanceOf(Exception::class); + expect(new Exception())->not->toBeInstanceOf(RuntimeException::class); +}); + +test('failures', function () { + expect(new Exception())->toBeInstanceOf(RuntimeException::class); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(new Exception())->not->toBeInstanceOf(Exception::class); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeInt.php b/tests/Expect/toBeInt.php new file mode 100644 index 00000000..9eb1b3db --- /dev/null +++ b/tests/Expect/toBeInt.php @@ -0,0 +1,16 @@ +toBeInt(); + expect(42.0)->not->toBeInt(); +}); + +test('failures', function () { + expect(42.0)->toBeInt(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(6 * 7)->not->toBeInt(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeIterable.php b/tests/Expect/toBeIterable.php new file mode 100644 index 00000000..fca23688 --- /dev/null +++ b/tests/Expect/toBeIterable.php @@ -0,0 +1,23 @@ +toBeIterable(); + expect(null)->not->toBeIterable(); +}); + +test('failures', function () { + expect(42)->toBeIterable(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + function gen(): iterable + { + yield 1; + yield 2; + yield 3; + } + + expect(gen())->not->toBeIterable(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeNAN.php b/tests/Expect/toBeNAN.php new file mode 100644 index 00000000..0767f1ad --- /dev/null +++ b/tests/Expect/toBeNAN.php @@ -0,0 +1,16 @@ +toBeNan(); + expect(log(0))->not->toBeNan(); +}); + +test('failures', function () { + expect(1)->toBeNan(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(acos(1.5))->not->toBeNan(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeNull.php b/tests/Expect/toBeNull.php new file mode 100644 index 00000000..2bd7d987 --- /dev/null +++ b/tests/Expect/toBeNull.php @@ -0,0 +1,16 @@ +toBeNull(); + expect('')->not->toBeNull(); +}); + +test('failures', function () { + expect('hello')->toBeNull(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(null)->not->toBeNull(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeNumeric.php b/tests/Expect/toBeNumeric.php new file mode 100644 index 00000000..710ca236 --- /dev/null +++ b/tests/Expect/toBeNumeric.php @@ -0,0 +1,16 @@ +toBeNumeric(); + expect('A')->not->toBeNumeric(); +}); + +test('failures', function () { + expect(null)->toBeNumeric(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(6 * 7)->not->toBeNumeric(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeObject.php b/tests/Expect/toBeObject.php new file mode 100644 index 00000000..e227b4cb --- /dev/null +++ b/tests/Expect/toBeObject.php @@ -0,0 +1,16 @@ + 1])->toBeObject(); + expect(['a' => 1])->not->toBeObject(); +}); + +test('failures', function () { + expect(null)->toBeObject(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect((object) 'ciao')->not->toBeObject(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeResource.php b/tests/Expect/toBeResource.php new file mode 100644 index 00000000..575fabe3 --- /dev/null +++ b/tests/Expect/toBeResource.php @@ -0,0 +1,22 @@ +toBeResource(); + expect(null)->not->toBeResource(); +}); + +test('failures', function () { + expect(null)->toBeResource(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () use ($resource) { + expect($resource)->not->toBeResource(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeScalar.php b/tests/Expect/toBeScalar.php new file mode 100644 index 00000000..236c0e0b --- /dev/null +++ b/tests/Expect/toBeScalar.php @@ -0,0 +1,15 @@ +toBeScalar(); +}); + +test('failures', function () { + expect(null)->toBeScalar(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(42)->not->toBeScalar(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toBeString.php b/tests/Expect/toBeString.php new file mode 100644 index 00000000..91c31881 --- /dev/null +++ b/tests/Expect/toBeString.php @@ -0,0 +1,16 @@ +toBeString(); + expect(1.1)->not->toBeString(); +}); + +test('failures', function () { + expect(null)->toBeString(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('42')->not->toBeString(); +})->throws(ExpectationFailedException::class);