From 1aec8bac55a072ccc60913f56fef43dd1b260cbf Mon Sep 17 00:00:00 2001 From: Nuno Maduro Date: Tue, 14 Jul 2020 23:37:02 +0200 Subject: [PATCH] feat(expect): adds toHaveProperty --- src/Expectation.php | 10 +++++++ tests/.snapshots/success.txt | 29 ++++++++----------- tests/Expect/toEqualIgnoringCase.php | 15 ---------- tests/Expect/{toCount.php => toHaveCount.php} | 0 tests/Expect/toHaveProperty.php | 18 ++++++++++++ tests/Features/AfterEach.php | 4 +-- 6 files changed, 42 insertions(+), 34 deletions(-) delete mode 100644 tests/Expect/toEqualIgnoringCase.php rename tests/Expect/{toCount.php => toHaveCount.php} (100%) create mode 100644 tests/Expect/toHaveProperty.php diff --git a/src/Expectation.php b/src/Expectation.php index 16f90982..b5d775a3 100644 --- a/src/Expectation.php +++ b/src/Expectation.php @@ -156,6 +156,16 @@ final class Expectation return $this; } + /** + * Assert that the $value contains the property $name. + */ + public function toHaveProperty(string $name): Expectation + { + Assert::assertTrue(property_exists($this->value, $name)); + + return $this; + } + /** * Asserts that two variables have the same value. * diff --git a/tests/.snapshots/success.txt b/tests/.snapshots/success.txt index 6f9ce702..e66bf634 100644 --- a/tests/.snapshots/success.txt +++ b/tests/.snapshots/success.txt @@ -120,31 +120,26 @@ PASS Tests\Expect\toContain ✓ passes ✓ 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\Expect\toHaveCount + ✓ pass + ✓ failures + ✓ not failures + + PASS Tests\Expect\toHaveProperty + ✓ pass + ✓ failures ✓ not failures PASS Tests\Features\AfterAll @@ -312,5 +307,5 @@ WARN Tests\Visual\Success - visual snapshot of test suite on success - Tests: 2 risked, 6 skipped, 181 passed - Time: 5.72s + Tests: 2 risked, 6 skipped, 178 passed + Time: 5.70s diff --git a/tests/Expect/toEqualIgnoringCase.php b/tests/Expect/toEqualIgnoringCase.php deleted file mode 100644 index 2016fa4d..00000000 --- a/tests/Expect/toEqualIgnoringCase.php +++ /dev/null @@ -1,15 +0,0 @@ -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/toCount.php b/tests/Expect/toHaveCount.php similarity index 100% rename from tests/Expect/toCount.php rename to tests/Expect/toHaveCount.php diff --git a/tests/Expect/toHaveProperty.php b/tests/Expect/toHaveProperty.php new file mode 100644 index 00000000..a7c0c649 --- /dev/null +++ b/tests/Expect/toHaveProperty.php @@ -0,0 +1,18 @@ +foo = 'bar'; + +test('pass', function () use ($obj) { + expect($obj)->toHaveProperty('foo'); +}); + +test('failures', function () use ($obj) { + expect($obj)->toHaveProperty('bar'); +})->throws(ExpectationFailedException::class); + +test('not failures', function () use ($obj) { + expect($obj)->not->toHaveProperty('foo'); +})->throws(ExpectationFailedException::class); diff --git a/tests/Features/AfterEach.php b/tests/Features/AfterEach.php index 5c223833..e16f8529 100644 --- a/tests/Features/AfterEach.php +++ b/tests/Features/AfterEach.php @@ -11,10 +11,10 @@ afterEach(function () use ($state) { }); it('does not get executed before the test', function () { - expect(property_exists($this->state, 'bar'))->toBeFalse(); + expect($this->state)->not->toHaveProperty('bar'); }); it('gets executed after the test', function () { - expect(property_exists($this->state, 'bar'))->toBeTrue(); + expect($this->state)->toHaveProperty('bar'); expect($this->state->bar)->toBe(2); });