From 4f3a74f560fabd93689c8b8cb57f12a56815008f Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Tue, 10 Oct 2023 11:49:42 +0200 Subject: [PATCH 1/6] Add baseObject(Not)HasProperty verifier --- .../Verify/Verifiers/VerifyAny.php | 12 +++++++++ .../Verify/Verifiers/VerifyBaseObject.php | 26 +++++++++++++++++++ tests/VerifyTest.php | 24 +++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/src/Codeception/Verify/Verifiers/VerifyAny.php b/src/Codeception/Verify/Verifiers/VerifyAny.php index c1bcc15..5be2167 100644 --- a/src/Codeception/Verify/Verifiers/VerifyAny.php +++ b/src/Codeception/Verify/Verifiers/VerifyAny.php @@ -98,6 +98,18 @@ public function baseObjectNotHasAttribute(string $attributeName, string $message return $this; } + public function baseObjectHasProperty(string $propertyName, string $message = ''): self + { + Verify::BaseObject($this->actual)->hasProperty($propertyName, $message); + return $this; + } + + public function baseObjectNotHasProperty(string $propertyName, string $message = ''): self + { + Verify::BaseObject($this->actual)->notHasProperty($propertyName, $message); + return $this; + } + public function callableThrows($throws = null, string $message = ''): self { Verify::Callable($this->actual)->throws($throws, $message); diff --git a/src/Codeception/Verify/Verifiers/VerifyBaseObject.php b/src/Codeception/Verify/Verifiers/VerifyBaseObject.php index c937dc3..90ae93a 100644 --- a/src/Codeception/Verify/Verifiers/VerifyBaseObject.php +++ b/src/Codeception/Verify/Verifiers/VerifyBaseObject.php @@ -46,4 +46,30 @@ public function notHasAttribute(string $attributeName, string $message = ''): se Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message); return $this; } + + /** + * Verifies that an object has a specified property. + * + * @param string $propertyName + * @param string $message + * @return self + */ + public function hasProperty(string $propertyName, string $message = ''): self + { + Assert::assertObjectHasProperty($propertyName, $this->actual, $message); + return $this; + } + + /** + * Verifies that an object does not have a specified property. + * + * @param string $propertyName + * @param string $message + * @return self + */ + public function notHasProperty(string $propertyName, string $message = ''): self + { + Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message); + return $this; + } } \ No newline at end of file diff --git a/tests/VerifyTest.php b/tests/VerifyTest.php index c215c11..e981457 100644 --- a/tests/VerifyTest.php +++ b/tests/VerifyTest.php @@ -324,6 +324,30 @@ public function testDoesNotThrow(): void verify($func)->callableDoesNotThrow(Exception::class, 'foo'); })->callableThrows(new ExpectationFailedException("exception 'Exception' with message 'foo' was not expected to be thrown")); } + + public function testObjectHasProperty(): void + { + $object = new \Stdclass(); + $object->bar = 'baz'; + + verify($object)->baseObjectHasProperty('bar'); + + verify(function () use ($object): void { + verify($object)->baseObjectHasProperty('foo', 'foobar'); + })->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" has property \"foo\".")); + } + + public function testObjectNotHasProperty(): void + { + $object = new \Stdclass(); + $object->bar = 'baz'; + + verify($object)->baseObjectNotHasProperty('foo'); + + verify(function () use ($object): void { + verify($object)->baseObjectNotHasProperty('bar', 'foobar'); + })->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" does not have property \"bar\".")); + } } From e02a6ecd052db470c27837cdeb10a5fe914cd563 Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Tue, 10 Oct 2023 11:55:41 +0200 Subject: [PATCH 2/6] Add baseObject(Not)ToHaveProperty expectation --- .../Verify/Expectations/ExpectAny.php | 12 +++++++ .../Verify/Expectations/ExpectBaseObject.php | 26 ++++++++++++++ tests/ExpectTest.php | 35 +++++++++++++++++++ 3 files changed, 73 insertions(+) create mode 100644 tests/ExpectTest.php diff --git a/src/Codeception/Verify/Expectations/ExpectAny.php b/src/Codeception/Verify/Expectations/ExpectAny.php index 9bbc582..9ad4f15 100644 --- a/src/Codeception/Verify/Expectations/ExpectAny.php +++ b/src/Codeception/Verify/Expectations/ExpectAny.php @@ -98,6 +98,18 @@ public function baseObjectNotToHaveAttribute(string $attributeName, string $mess return $this; } + public function baseObjectToHaveProperty(string $propertyName, string $message = ''): self + { + Expect::BaseObject($this->actual)->toHaveProperty($propertyName, $message); + return $this; + } + + public function baseObjectNotToHaveProperty(string $propertyName, string $message = ''): self + { + Expect::BaseObject($this->actual)->notToHaveProperty($propertyName, $message); + return $this; + } + public function callableToThrow($throws = null, string $message = ''): self { Expect::Callable($this->actual)->toThrow($throws, $message); diff --git a/src/Codeception/Verify/Expectations/ExpectBaseObject.php b/src/Codeception/Verify/Expectations/ExpectBaseObject.php index 394be23..d321303 100644 --- a/src/Codeception/Verify/Expectations/ExpectBaseObject.php +++ b/src/Codeception/Verify/Expectations/ExpectBaseObject.php @@ -46,4 +46,30 @@ public function toHaveAttribute(string $attributeName, string $message = ''): se Assert::assertObjectHasAttribute($attributeName, $this->actual, $message); return $this; } + + /** + * Expect that an object does not have a specified property. + * + * @param string $propertyName + * @param string $message + * @return self + */ + public function notToHaveProperty(string $propertyName, string $message = ''): self + { + Assert::assertObjectNotHasProperty($propertyName, $this->actual, $message); + return $this; + } + + /** + * Expect that an object has a specified property. + * + * @param string $propertyName + * @param string $message + * @return self + */ + public function toHaveProperty(string $propertyName, string $message = ''): self + { + Assert::assertObjectHasProperty($propertyName, $this->actual, $message); + return $this; + } } \ No newline at end of file diff --git a/tests/ExpectTest.php b/tests/ExpectTest.php new file mode 100644 index 0000000..21f5fd8 --- /dev/null +++ b/tests/ExpectTest.php @@ -0,0 +1,35 @@ +bar = 'baz'; + + expect($object)->baseObjectToHaveProperty('bar'); + + verify(function () use ($object): void { + expect($object)->baseObjectToHaveProperty('foo', 'foobar'); + })->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" has property \"foo\".")); + } + + public function testObjectNotToHaveProperty(): void + { + $object = new \Stdclass(); + $object->bar = 'baz'; + + expect($object)->baseObjectNotToHaveProperty('foo'); + + verify(function () use ($object): void { + expect($object)->baseObjectNotToHaveProperty('bar', 'foobar'); + })->callableThrows(new ExpectationFailedException("foobar\nFailed asserting that object of class \"stdClass\" does not have property \"bar\".")); + } +} From 597052ceb7de2dfe0e7d33e77ab8c16651260413 Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Tue, 10 Oct 2023 12:01:09 +0200 Subject: [PATCH 3/6] Add new hasProperty to docs --- docs/supported_expectations.md | 2 ++ docs/supported_verifiers.md | 2 ++ 2 files changed, 4 insertions(+) diff --git a/docs/supported_expectations.md b/docs/supported_expectations.md index 711b344..2c9d3b9 100644 --- a/docs/supported_expectations.md +++ b/docs/supported_expectations.md @@ -23,6 +23,8 @@ toHaveSameSizeAs ``` notToHaveAttribute toHaveAttribute +notToHaveProperty +toHaveProperty ``` ### Callable diff --git a/docs/supported_verifiers.md b/docs/supported_verifiers.md index acedbb6..a8e323f 100644 --- a/docs/supported_verifiers.md +++ b/docs/supported_verifiers.md @@ -23,6 +23,8 @@ sameSize ``` hasAttribute notHasAttribute +hasProperty +notHasProperty ``` ### Callable From 688e5767bf6d700da1bda92edffba22431c2a327 Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Thu, 12 Oct 2023 20:30:46 +0200 Subject: [PATCH 4/6] Require phpunit >=9.6.11 Version 9.6.11 had the new asssertObjectHasProperty method added, which completely replace the assertObjectHasAttribute ones in phpunit 10.x --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index c3a692e..80e7149 100644 --- a/composer.json +++ b/composer.json @@ -16,7 +16,7 @@ "require": { "php": "^7.4 || ^8.0", "ext-dom": "*", - "phpunit/phpunit": "^9.5 | ^10.0" + "phpunit/phpunit": "^9.6.11 || ^10.0" }, "autoload": { "files": [ From 370af7a517d0181f29b638e72826a5c0aece69c1 Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Thu, 12 Oct 2023 20:37:52 +0200 Subject: [PATCH 5/6] Deprecate objectHasAttribute expectations/verifiers --- docs/supported_expectations.md | 2 -- docs/supported_verifiers.md | 2 -- src/Codeception/Verify/Expectations/ExpectBaseObject.php | 8 ++++++-- src/Codeception/Verify/Verifiers/VerifyBaseObject.php | 8 ++++++-- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/docs/supported_expectations.md b/docs/supported_expectations.md index 2c9d3b9..cb5ca17 100644 --- a/docs/supported_expectations.md +++ b/docs/supported_expectations.md @@ -21,8 +21,6 @@ toHaveSameSizeAs ### BaseObject ``` -notToHaveAttribute -toHaveAttribute notToHaveProperty toHaveProperty ``` diff --git a/docs/supported_verifiers.md b/docs/supported_verifiers.md index a8e323f..d6c181d 100644 --- a/docs/supported_verifiers.md +++ b/docs/supported_verifiers.md @@ -21,8 +21,6 @@ sameSize ### BaseObject ``` -hasAttribute -notHasAttribute hasProperty notHasProperty ``` diff --git a/src/Codeception/Verify/Expectations/ExpectBaseObject.php b/src/Codeception/Verify/Expectations/ExpectBaseObject.php index d321303..4fa362e 100644 --- a/src/Codeception/Verify/Expectations/ExpectBaseObject.php +++ b/src/Codeception/Verify/Expectations/ExpectBaseObject.php @@ -24,26 +24,30 @@ public function __construct(object $object) /** * Expect that an object does not have a specified attribute. * + * @deprecated Deprecated in favour of notToHaveProperty + * * @param string $attributeName * @param string $message * @return self */ public function notToHaveAttribute(string $attributeName, string $message = ''): self { - Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message); + Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message); return $this; } /** * Expect that an object has a specified attribute. * + * @deprecated Deprecated in favour of toHaveProperty + * * @param string $attributeName * @param string $message * @return self */ public function toHaveAttribute(string $attributeName, string $message = ''): self { - Assert::assertObjectHasAttribute($attributeName, $this->actual, $message); + Assert::assertObjectHasProperty($attributeName, $this->actual, $message); return $this; } diff --git a/src/Codeception/Verify/Verifiers/VerifyBaseObject.php b/src/Codeception/Verify/Verifiers/VerifyBaseObject.php index 90ae93a..882a7ce 100644 --- a/src/Codeception/Verify/Verifiers/VerifyBaseObject.php +++ b/src/Codeception/Verify/Verifiers/VerifyBaseObject.php @@ -24,26 +24,30 @@ public function __construct(object $object) /** * Verifies that an object has a specified attribute. * + * @deprecated Deprecated in favour of hasProperty + * * @param string $attributeName * @param string $message * @return self */ public function hasAttribute(string $attributeName, string $message = ''): self { - Assert::assertObjectHasAttribute($attributeName, $this->actual, $message); + Assert::assertObjectHasProperty($attributeName, $this->actual, $message); return $this; } /** * Verifies that an object does not have a specified attribute. * + * @deprecated Deprecated in favour of notHasProperty + * * @param string $attributeName * @param string $message * @return self */ public function notHasAttribute(string $attributeName, string $message = ''): self { - Assert::assertObjectNotHasAttribute($attributeName, $this->actual, $message); + Assert::assertObjectNotHasProperty($attributeName, $this->actual, $message); return $this; } From 4f37d780b1b5dec51442fbd82f38609e2b0ba82d Mon Sep 17 00:00:00 2001 From: Stefan Linke Date: Thu, 12 Oct 2023 20:45:47 +0200 Subject: [PATCH 6/6] Fix relative links in README In theory, github markdown docs say that: > Links starting with / will be relative to the repository root. Yet, they are resolved to be relative to the github.com domain. Maybe it's a special case with the `/docs` , since that also references the actual github docs. Changed them to explicit relative links. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index f54ba54..ee91729 100644 --- a/README.md +++ b/README.md @@ -147,7 +147,7 @@ Verify is open-sourced software licensed under the [MIT][9] License. [4]: https://chaijs.com/ [5]: https://jasmine.github.io/ [6]: https://rspec.info/ -[7]: /docs/supported_verifiers.md -[8]: /docs/supported_expectations.md -[9]: /LICENSE -[10]: /UPGRADE.md +[7]: ./docs/supported_verifiers.md +[8]: ./docs/supported_expectations.md +[9]: ./LICENSE +[10]: ./UPGRADE.md