Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add dynamic property __mocked to mocked object #40

Merged
merged 1 commit into from
Dec 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,8 @@
},
"require-dev": {
"consolidation/robo": "^3.0"
},
"conflict": {
"codeception/codeception": "<5.0.6"
}
}
38 changes: 10 additions & 28 deletions src/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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;
}

/**
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -633,6 +615,6 @@ private static function createEmpty($class, string $method): array
);

$methods = count($methods) ? $methods : null;
return [$class, $reflectionClass, $methods];
return [$class, $methods];
}
}
1 change: 1 addition & 0 deletions src/Test/Feature/Stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,7 @@ public function constructEmpty($class, array $constructorParams = [], array $par
* @template RealInstanceType of object
* @param class-string<RealInstanceType>|RealInstanceType|callable(): class-string<RealInstanceType> $class - A class to be mocked
* @return MockObject&RealInstanceType
* @throws \ReflectionException
*/
public function constructEmptyExcept($class, string $method, array $constructorParams = [], array $params = [])
{
Expand Down
12 changes: 6 additions & 6 deletions tests/StubTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down