Skip to content

Commit

Permalink
pass excepted methods to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
Mohamed Rostrami committed Oct 15, 2019
1 parent dd0a021 commit 69cc51c
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
55 changes: 37 additions & 18 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,12 @@ class Generator
* @param object $proxyTarget
* @param bool $allowMockingUnknownTypes
* @param bool $returnValueGeneration
*
* @throws Exception
* @throws RuntimeException
* @throws \PHPUnit\Framework\Exception
* @throws \ReflectionException
* @param array $methodsExcepted
*
* @return MockObject
* @throws \ReflectionException
*/
public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null, $allowMockingUnknownTypes = true, $returnValueGeneration = true)
public function getMock($type, $methods = [], array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, $proxyTarget = null, $allowMockingUnknownTypes = true, $returnValueGeneration = true, array $methodsExcepted = [])
{
if (!\is_array($type) && !\is_string($type)) {
throw InvalidArgumentHelper::factory(1, 'array or string');
Expand Down Expand Up @@ -185,7 +182,8 @@ function ($type) {
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
$callOriginalMethods,
$methodsExcepted
);

return $this->getObject(
Expand All @@ -207,20 +205,22 @@ function ($type) {
* the last parameter
*
* @param string $originalClassName
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @param bool $cloneArguments
* @param array $methodsExcept
*
* @throws \ReflectionException
* @throws RuntimeException
* @throws Exception
*
* @return MockObject
*/
public function getMockForAbstractClass($originalClassName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true)
public function getMockForAbstractClass($originalClassName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true, array $methodsExcept = [])
{
if (!\is_string($originalClassName)) {
throw InvalidArgumentHelper::factory(1, 'string');
Expand Down Expand Up @@ -253,7 +253,12 @@ public function getMockForAbstractClass($originalClassName, array $arguments = [
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$cloneArguments
$cloneArguments,
false,
null,
true,
true,
$methodsExcept
);
}

Expand All @@ -268,20 +273,22 @@ public function getMockForAbstractClass($originalClassName, array $arguments = [
* `$mockedMethods` parameter.
*
* @param string $traitName
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @param bool $cloneArguments
* @param array $methodsExcept
*
* @throws \ReflectionException
* @throws RuntimeException
* @throws Exception
*
* @return MockObject
*/
public function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true)
public function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = true, array $methodsExcept = [])
{
if (!\is_string($traitName)) {
throw InvalidArgumentHelper::factory(1, 'string');
Expand Down Expand Up @@ -321,7 +328,7 @@ public function getMockForTrait($traitName, array $arguments = [], $mockClassNam
$className['className']
);

return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments);
return $this->getMockForAbstractClass($className['className'], $arguments, $mockClassName, $callOriginalConstructor, $callOriginalClone, $callAutoload, $mockedMethods, $cloneArguments, $methodsExcept);
}

/**
Expand Down Expand Up @@ -392,13 +399,14 @@ public function getObjectForTrait($traitName, array $arguments = [], $traitClass
* @param bool $callAutoload
* @param bool $cloneArguments
* @param bool $callOriginalMethods
* @param array $methodsExcepted
*
* @throws \ReflectionException
* @throws \PHPUnit\Framework\MockObject\RuntimeException
*
* @return array
*/
public function generate($type, array $methods = null, $mockClassName = '', $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false)
public function generate($type, array $methods = null, $mockClassName = '', $callOriginalClone = true, $callAutoload = true, $cloneArguments = true, $callOriginalMethods = false, array $methodsExcepted = [])
{
if (\is_array($type)) {
\sort($type);
Expand All @@ -412,12 +420,14 @@ public function generate($type, array $methods = null, $mockClassName = '', $cal
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
$callOriginalMethods,
$methodsExcepted
);
}
$key = \md5(
\is_array($type) ? \implode('_', $type) : $type .
\serialize($methods) .
\serialize($methodsExcepted) .
\serialize($callOriginalClone) .
\serialize($cloneArguments) .
\serialize($callOriginalMethods)
Expand All @@ -431,7 +441,8 @@ public function generate($type, array $methods = null, $mockClassName = '', $cal
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
$callOriginalMethods,
$methodsExcepted
);
}

Expand Down Expand Up @@ -544,16 +555,23 @@ public function getClassMethods($className): array
}

/**
* @throws \ReflectionException
* @param string $className
* @param bool $callOriginalMethods
* @param bool $cloneArguments
* @param array $methodsExcepted
*
* @return MockMethod[]
* @throws \ReflectionException
*/
public function mockClassMethods(string $className, bool $callOriginalMethods, bool $cloneArguments): array
public function mockClassMethods(string $className, bool $callOriginalMethods, bool $cloneArguments, array $methodsExcepted = []): array
{
$class = new ReflectionClass($className);
$methods = [];

foreach ($class->getMethods() as $method) {
if ($methodsExcepted !== [] && in_array($method->getName(), $methodsExcepted)) {
continue;
}
if (($method->isPublic() || $method->isAbstract()) && $this->canMockMethod($method)) {
$methods[] = MockMethod::fromReflection($method, $callOriginalMethods, $cloneArguments);
}
Expand Down Expand Up @@ -655,14 +673,15 @@ private function evalClass($code, $className): void
* @param bool $callAutoload
* @param bool $cloneArguments
* @param bool $callOriginalMethods
* @param array $methodsExcepted
*
* @throws \InvalidArgumentException
* @throws \ReflectionException
* @throws RuntimeException
*
* @return array
*/
private function generateMock($type, $explicitMethods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods)
private function generateMock($type, $explicitMethods, $mockClassName, $callOriginalClone, $callAutoload, $cloneArguments, $callOriginalMethods, $methodsExcepted)
{
$classTemplate = $this->getTemplate('mocked_class.tpl');

Expand Down Expand Up @@ -830,7 +849,7 @@ private function generateMock($type, $explicitMethods, $mockClassName, $callOrig
if ($explicitMethods === [] &&
($isClass || $isInterface)) {
$mockMethods->addMethods(
...$this->mockClassMethods($mockClassName['fullClassName'], $callOriginalMethods, $cloneArguments)
...$this->mockClassMethods($mockClassName['fullClassName'], $callOriginalMethods, $cloneArguments, $methodsExcepted)
);
}

Expand Down
9 changes: 6 additions & 3 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ public function getMock()
$this->callOriginalMethods,
$this->proxyTarget,
$this->allowMockingUnknownTypes,
$this->returnValueGeneration
$this->returnValueGeneration,
$this->methodsExcept
);

$this->testCase->registerMockObject($object);
Expand All @@ -143,7 +144,8 @@ public function getMockForAbstractClass()
$this->originalClone,
$this->autoload,
$this->methods,
$this->cloneArguments
$this->cloneArguments,
$this->methodsExcept
);

$this->testCase->registerMockObject($object);
Expand All @@ -166,7 +168,8 @@ public function getMockForTrait()
$this->originalClone,
$this->autoload,
$this->methods,
$this->cloneArguments
$this->cloneArguments,
$this->methodsExcept
);

$this->testCase->registerMockObject($object);
Expand Down
7 changes: 5 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1487,18 +1487,20 @@ protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClas
* `$mockedMethods` parameter.
*
* @param string $traitName
* @param array $arguments
* @param string $mockClassName
* @param bool $callOriginalConstructor
* @param bool $callOriginalClone
* @param bool $callAutoload
* @param array $mockedMethods
* @param bool $cloneArguments
* @param array $methodsExcept
*
* @throws Exception
* @throws ReflectionException
* @throws \InvalidArgumentException
*/
protected function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = false): MockObject
protected function getMockForTrait($traitName, array $arguments = [], $mockClassName = '', $callOriginalConstructor = true, $callOriginalClone = true, $callAutoload = true, $mockedMethods = [], $cloneArguments = false, array $methodsExcept = []): MockObject
{
$mockObject = $this->getMockObjectGenerator()->getMockForTrait(
$traitName,
Expand All @@ -1508,7 +1510,8 @@ protected function getMockForTrait($traitName, array $arguments = [], $mockClass
$callOriginalClone,
$callAutoload,
$mockedMethods,
$cloneArguments
$cloneArguments,
$methodsExcept
);

$this->registerMockObject($mockObject);
Expand Down

0 comments on commit 69cc51c

Please sign in to comment.