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 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": [ diff --git a/docs/supported_expectations.md b/docs/supported_expectations.md index 711b344..cb5ca17 100644 --- a/docs/supported_expectations.md +++ b/docs/supported_expectations.md @@ -21,8 +21,8 @@ toHaveSameSizeAs ### BaseObject ``` -notToHaveAttribute -toHaveAttribute +notToHaveProperty +toHaveProperty ``` ### Callable diff --git a/docs/supported_verifiers.md b/docs/supported_verifiers.md index acedbb6..d6c181d 100644 --- a/docs/supported_verifiers.md +++ b/docs/supported_verifiers.md @@ -21,8 +21,8 @@ sameSize ### BaseObject ``` -hasAttribute -notHasAttribute +hasProperty +notHasProperty ``` ### Callable 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..4fa362e 100644 --- a/src/Codeception/Verify/Expectations/ExpectBaseObject.php +++ b/src/Codeception/Verify/Expectations/ExpectBaseObject.php @@ -24,26 +24,56 @@ 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; + } + + /** + * 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/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..882a7ce 100644 --- a/src/Codeception/Verify/Verifiers/VerifyBaseObject.php +++ b/src/Codeception/Verify/Verifiers/VerifyBaseObject.php @@ -24,26 +24,56 @@ 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; + } + + /** + * 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/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\".")); + } +} 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\".")); + } }