diff --git a/src/Expectation.php b/src/Expectation.php index 44357b9b..ae4b0ad5 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -53,7 +53,17 @@ final class Expectation } /** - * Assert that value is false. + * Assert the value is empty. + */ + public function toBeEmpty() + { + Assert::assertEmpty($this->value); + + return $this; + } + + /** + * Assert the value is false. */ public function toBeFalse() { @@ -63,7 +73,7 @@ final class Expectation } /** - * Assert that value is greater than expected one. + * Assert the value is greater than expected one. * * @param int|float $value */ @@ -123,9 +133,29 @@ final class Expectation } /** - * Assert that needles is a substring of value. + * Assert the value contains only variables of type. * - * @param string $needle + * @param string|mixed $type + */ + public function toContainOnly($type) + { + Assert::assertContainsOnly($type, $this->value); + + return $this; + } + + /** + * Assert the value contains only instances of $instance. + */ + public function toContainOnlyInstancesOf(string $instance) + { + Assert::assertContainsOnlyInstancesOf($instance, $this->value); + + return $this; + } + + /** + * Assert that needles is a substring of value. */ public function toContainString(string $needle) { @@ -137,8 +167,6 @@ final class Expectation /** * Assert that needles is a substring of value, ignoring the * difference in casing. - * - * @param string $needle */ public function toContainStringIgnoringCase(string $needle) { @@ -147,6 +175,102 @@ final class Expectation return $this; } + /** + * Assert that $count matches the number of elements of $value. + */ + public function toCount(int $count) + { + Assert::assertCount($count, $this->value); + + return $this; + } + + /** + * Asserts that two variables have the same value. + * + * @param mixed $value + */ + public function toEqual($value) + { + Assert::assertEquals($value, $this->value); + + return $this; + } + + /** + * Asserts that two variables are equals, ignoring the casing + * for the comparison. + * + * @param mixed $value + */ + public function toEqualIgnoringCase($value) + { + Assert::assertEqualsIgnoringCase($value, $this->value); + + return $this; + } + + /** + * Asserts that two variables have the same value. + * The contents of $expected and $actual are canonicalized before + * they are compared. For instance, when the two variables $value and + * $this->value are arrays, then these arrays are sorted before they are + * compared. When $value and $this->value are objects, + * each object is converted to an array containing all private, + * protected and public attributes. + * + * @param mixed $value + */ + public function toEqualCanonicalizing($value) + { + Assert::assertEqualsCanonicalizing($value, $this->value); + + return $this; + } + + /** + * Assert that the absolute difference between $value and $this->value + * is greather thatn $delta. + * + * @param mixed $value + */ + public function toEqualWithDelta($value, float $delta) + { + Assert::assertEqualsWithDelta($value, $this->value, $delta); + + return $this; + } + + /** + * Assert that the value is a directory. + */ + public function toBeExistingDirectory() + { + Assert::assertDirectoryExists($this->value); + + return $this; + } + + /** + * Assert that the value is a directory and is readable. + */ + public function toBeReadableDirectory() + { + Assert::assertDirectoryIsReadable($this->value); + + return $this; + } + + /** + * Assert that the value is a directory and is writable. + */ + public function toBeWritableDirectory() + { + 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 bcff1d84..aa0a5df6 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -5,6 +5,96 @@ PASS Tests\Expect\ToBe ✓ strict comparisons ✓ failures + ✓ not failures + + PASS Tests\Expect\thatDirectoryExists + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\thatDirectoryIsReadable + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeEmpty + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeFalse + ✓ strict comparisons + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeGreatherThan + ✓ passes + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeGreatherThanOrEqual + ✓ passes + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeLessThan + ✓ passes + ✓ failures + ✓ not failures + + PASS Tests\Expect\toBeLessThanOrEqual + ✓ passes + ✓ failures + ✓ not failures + + PASS Tests\Expect\toContain + ✓ passes + ✓ failures + ✓ not failures + + PASS Tests\Expect\toContainOnly + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toContainOnlyInstancesOf + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toContainString + ✓ is case sensitive + ✓ failures + ✓ not failures + + PASS Tests\Expect\toContainStringIgnoringCase + ✓ ignore difference in casing + ✓ failures + ✓ not failures + + PASS Tests\Expect\toCount + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toEqual + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toEqualCanonicalizing + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toEqualIgnoringCase + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toEqualWithDelta + ✓ pass + ✓ failures ✓ not failures PASS Tests\Features\AfterAll @@ -172,5 +262,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 6 skipped, 99 passed - Time: 3.62s + Tests: 6 skipped, 153 passed + Time: 4.35s diff --git a/tests/Expect/thatDirectoryExists.php b/tests/Expect/thatDirectoryExists.php new file mode 100644 index 00000000..6f2eb5f9 --- /dev/null +++ b/tests/Expect/thatDirectoryExists.php @@ -0,0 +1,17 @@ +toBeExistingDirectory(); +}); + +test('failures', function () { + expect('/random/path/whatever')->toBeExistingDirectory(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('.')->not->toBeExistingDirectory(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/thatDirectoryIsReadable.php b/tests/Expect/thatDirectoryIsReadable.php new file mode 100644 index 00000000..88f96019 --- /dev/null +++ b/tests/Expect/thatDirectoryIsReadable.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/toBeEmpty.php b/tests/Expect/toBeEmpty.php new file mode 100644 index 00000000..05e5180a --- /dev/null +++ b/tests/Expect/toBeEmpty.php @@ -0,0 +1,18 @@ +toBeEmpty(); + expect(null)->toBeEmpty(); +}); + +test('failures', function () { + expect([1, 2])->toBeEmpty(); + expect(' ')->toBeEmpty(); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect([])->not->toBeEmpty(); + expect(null)->not->toBeEmpty(); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toContainOnly.php b/tests/Expect/toContainOnly.php new file mode 100644 index 00000000..2e9fecfe --- /dev/null +++ b/tests/Expect/toContainOnly.php @@ -0,0 +1,17 @@ +toContainOnly('int'); + expect(['hello', 'world'])->toContainOnly('string'); +}); + +test('failures', function () { + expect([1, 2, '3'])->toContainOnly('string'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect([1, 2, 3])->not->toContainOnly('int'); + expect(['hello', 'world'])->not->toContainOnly('string'); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toContainOnlyInstancesOf.php b/tests/Expect/toContainOnlyInstancesOf.php new file mode 100644 index 00000000..fdda161b --- /dev/null +++ b/tests/Expect/toContainOnlyInstancesOf.php @@ -0,0 +1,23 @@ +toContainOnlyInstancesOf(Expectation::class); +}); + +test('failures', function () { + $expected = [new Expectation('whatever')]; + + expect($expected)->toContainOnlyInstancesOf(AddsTests::class); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + $expected = [new Expectation('whatever')]; + + expect($expected)->not->toContainOnlyInstancesOf(Expectation::class); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toCount.php b/tests/Expect/toCount.php new file mode 100644 index 00000000..6e8d5fcd --- /dev/null +++ b/tests/Expect/toCount.php @@ -0,0 +1,15 @@ +toCount(3); +}); + +test('failures', function () { + expect([1, 2, 3])->toCount(4); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect([1, 2, 3])->not->toCount(3); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toEqual.php b/tests/Expect/toEqual.php new file mode 100644 index 00000000..de0b7e50 --- /dev/null +++ b/tests/Expect/toEqual.php @@ -0,0 +1,15 @@ +toEqual(123); +}); + +test('failures', function () { + expect(['a', 'b', 'c'])->toEqual(['a', 'b']); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('042')->not->toEqual(42); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toEqualCanonicalizing.php b/tests/Expect/toEqualCanonicalizing.php new file mode 100644 index 00000000..2d6c299f --- /dev/null +++ b/tests/Expect/toEqualCanonicalizing.php @@ -0,0 +1,16 @@ +toEqualCanonicalizing(123); + expect(['a', 'b', 'c'])->toEqualCanonicalizing(['c', 'a', 'b']); +}); + +test('failures', function () { + expect(['a', 'b', 'c'])->toEqualCanonicalizing(['a', 'b']); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('042')->not->toEqualCanonicalizing(42); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toEqualIgnoringCase.php b/tests/Expect/toEqualIgnoringCase.php new file mode 100644 index 00000000..2016fa4d --- /dev/null +++ b/tests/Expect/toEqualIgnoringCase.php @@ -0,0 +1,15 @@ +toEqualIgnoringCase('HELLO'); +}); + +test('failures', function () { + expect('hello')->toEqualIgnoringCase('BAR'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect('HELLO')->not->toEqualIgnoringCase('HelLo'); +})->throws(ExpectationFailedException::class); diff --git a/tests/Expect/toEqualWithDelta.php b/tests/Expect/toEqualWithDelta.php new file mode 100644 index 00000000..bb78b86d --- /dev/null +++ b/tests/Expect/toEqualWithDelta.php @@ -0,0 +1,15 @@ +toEqualWithDelta(1.3, .4); +}); + +test('failures', function () { + expect(1.0)->toEqualWithDelta(1.5, .1); +})->throws(ExpectationFailedException::class); + +test('not failures', function () { + expect(1.0)->not->toEqualWithDelta(1.6, .7); +})->throws(ExpectationFailedException::class);