diff --git a/composer.json b/composer.json index 3084e26..522151e 100644 --- a/composer.json +++ b/composer.json @@ -15,5 +15,8 @@ }, "require-dev": { "consolidation/robo": "^3.0" + }, + "conflict": { + "codeception/codeception": "<5.0.6" } } diff --git a/src/Stub.php b/src/Stub.php index 3c4a385..ae31b40 100644 --- a/src/Stub.php +++ b/src/Stub.php @@ -92,17 +92,6 @@ public static function make($class, array $params = [], $testCase = false) self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflection); - } - - /** - * Set __mock flag, if at all possible - */ - private static function markAsMock(object $mock, ReflectionClass $reflection): object - { - if (!$reflection->hasMethod('__set')) { - $mock->__mocked = $reflection->getName(); - } return $mock; } @@ -169,11 +158,11 @@ public static function factory($class, int $num = 1, array $params = []): array */ public static function makeEmptyExcept($class, string $method, array $params = [], $testCase = false) { - [$class, $reflectionClass, $methods] = self::createEmpty($class, $method); + [$class, $methods] = self::createEmpty($class, $method); $mock = self::generateMock($class, $methods, [], '', false, $testCase); self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflectionClass); + return $mock; } /** @@ -223,17 +212,14 @@ public static function makeEmptyExcept($class, string $method, array $params = [ public static function makeEmpty($class, array $params = [], $testCase = false) { $class = self::getClassname($class); - $reflection = new ReflectionClass($class); - - $methods = get_class_methods($class); $methods = array_filter( - $methods, + get_class_methods($class), fn($i) => !in_array($i, Stub::$magicMethods) ); $mock = self::generateMock($class, $methods, [], '', false, $testCase); self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflection); + return $mock; } /** @@ -307,7 +293,7 @@ public static function construct($class, array $constructorParams = [], array $p $mock = self::generateMock($class, $arguments, $constructorParams, $testCase); self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflection); + return $mock; } /** @@ -354,22 +340,18 @@ public static function construct($class, array $constructorParams = [], array $p * @param bool|PHPUnitTestCase $testCase * * @return PHPUnitMockObject&RealInstanceType - * @throws ReflectionException */ public static function constructEmpty($class, array $constructorParams = [], array $params = [], $testCase = false) { $class = self::getClassname($class); - $reflection = new ReflectionClass($class); - - $methods = get_class_methods($class); $methods = array_filter( - $methods, + get_class_methods($class), fn($i) => !in_array($i, Stub::$magicMethods) ); $mock = self::generateMock($class, $methods, $constructorParams, $testCase); self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflection); + return $mock; } /** @@ -423,11 +405,11 @@ public static function constructEmptyExcept( array $params = [], $testCase = false ) { - [$class, $reflectionClass, $methods] = self::createEmpty($class, $method); + [$class, $methods] = self::createEmpty($class, $method); $mock = self::generateMock($class, $methods, $constructorParams, $testCase); self::bindParameters($mock, $params); - return self::markAsMock($mock, $reflectionClass); + return $mock; } private static function generateMock() @@ -633,6 +615,6 @@ private static function createEmpty($class, string $method): array ); $methods = count($methods) ? $methods : null; - return [$class, $reflectionClass, $methods]; + return [$class, $methods]; } } diff --git a/src/Test/Feature/Stub.php b/src/Test/Feature/Stub.php index 70a7a4c..34b17bf 100644 --- a/src/Test/Feature/Stub.php +++ b/src/Test/Feature/Stub.php @@ -290,6 +290,7 @@ public function constructEmpty($class, array $constructorParams = [], array $par * @template RealInstanceType of object * @param class-string|RealInstanceType|callable(): class-string $class - A class to be mocked * @return MockObject&RealInstanceType + * @throws \ReflectionException */ public function constructEmptyExcept($class, string $method, array $constructorParams = [], array $params = []) { diff --git a/tests/StubTest.php b/tests/StubTest.php index 9707575..9325ad4 100644 --- a/tests/StubTest.php +++ b/tests/StubTest.php @@ -160,42 +160,42 @@ public function testStubsFromObject() $dummy ); $dummy = Stub::make(new DummyOverloadableClass()); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); $dummy = Stub::makeEmpty(new DummyClass()); $this->assertInstanceOf( MockObject::class, $dummy ); $dummy = Stub::makeEmpty(new DummyOverloadableClass()); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); $dummy = Stub::makeEmptyExcept(new DummyClass(), 'helloWorld'); $this->assertInstanceOf( MockObject::class, $dummy ); $dummy = Stub::makeEmptyExcept(new DummyOverloadableClass(), 'helloWorld'); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); $dummy = Stub::construct(new DummyClass()); $this->assertInstanceOf( MockObject::class, $dummy ); $dummy = Stub::construct(new DummyOverloadableClass()); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); $dummy = Stub::constructEmpty(new DummyClass()); $this->assertInstanceOf( MockObject::class, $dummy ); $dummy = Stub::constructEmpty(new DummyOverloadableClass()); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); $dummy = Stub::constructEmptyExcept(new DummyClass(), 'helloWorld'); $this->assertInstanceOf( MockObject::class, $dummy ); $dummy = Stub::constructEmptyExcept(new DummyOverloadableClass(), 'helloWorld'); - $this->assertObjectHasProperty('__mocked', $dummy); + $this->assertSame(DummyOverloadableClass::class, get_parent_class($dummy)); } protected function assertMethodReplaced($dummy)