From 3cc6b98fd10af2fe2db67aa0753fae08d03b5e79 Mon Sep 17 00:00:00 2001 From: Arkadiusz Kondas Date: Sun, 30 Dec 2018 16:27:34 +0100 Subject: [PATCH] Add support for ObjectState Matcher (is, has) (#4) --- CHANGELOG.md | 10 +++++++- spec/Proget/Tests/BazSpec.php | 23 +++++++++++++++++++ ...ehaviorMethodsClassReflectionExtension.php | 10 +++++--- tests/Baz.php | 15 ++++++++++++ 4 files changed, 54 insertions(+), 4 deletions(-) create mode 100644 spec/Proget/Tests/BazSpec.php diff --git a/CHANGELOG.md b/CHANGELOG.md index f540b17..f52d832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,8 +11,16 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. * **Removed** for now removed features. * **Fixed** for any bug fixes. * **Security** in case of vulnerabilities. + + +## [0.1.1] 2018-12-29 +### Added + * support for ObjectState Matcher (is, has) + +### Removed + * unused symfony/finder dependency -## Unreleased +## [0.1.0] 2018-12-29 ### Added * first initial release diff --git a/spec/Proget/Tests/BazSpec.php b/spec/Proget/Tests/BazSpec.php new file mode 100644 index 0000000..c000d9f --- /dev/null +++ b/spec/Proget/Tests/BazSpec.php @@ -0,0 +1,23 @@ +shouldHaveType(Baz::class); + } + + public function it_should_allow_to_enable(): void + { + $this->enable(); + + $this->shouldBeEnabled(); + } +} diff --git a/src/Reflection/ObjectBehaviorMethodsClassReflectionExtension.php b/src/Reflection/ObjectBehaviorMethodsClassReflectionExtension.php index 3257572..3b3695b 100644 --- a/src/Reflection/ObjectBehaviorMethodsClassReflectionExtension.php +++ b/src/Reflection/ObjectBehaviorMethodsClassReflectionExtension.php @@ -57,10 +57,14 @@ public function getMethod(ClassReflection $classReflection, string $methodName): return $subjectReflection->getMethod($methodName, new OutOfClassScope()); } + $sourceClass = $this->getSourceClassName($classReflection); + + if (preg_match('/^should(Be|Have)(.+)$/', $methodName)) { + return $this->broker->getClass($sourceClass)->getMethod(str_replace(['shouldBe', 'shouldHave'], ['is', 'has'], $methodName), new OutOfClassScope()); + } + return new ObjectBehaviorMethodReflection( - $this->broker->getClass( - $this->getSourceClassName($classReflection) - )->getMethod($methodName, new OutOfClassScope()) + $this->broker->getClass($sourceClass)->getMethod($methodName, new OutOfClassScope()) ); } diff --git a/tests/Baz.php b/tests/Baz.php index 5ecc641..a9b35a2 100644 --- a/tests/Baz.php +++ b/tests/Baz.php @@ -6,8 +6,23 @@ class Baz { + /** + * @var bool + */ + private $enabled = false; + public function someInt(): int { return 10; } + + public function enable(): void + { + $this->enabled = true; + } + + public function isEnabled(): bool + { + return $this->enabled; + } }