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

setMethodsExcept when only one method exist #3897

Closed
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
41 changes: 26 additions & 15 deletions src/Framework/MockObject/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,11 @@ class Generator
* @param bool $allowMockingUnknownTypes
* @param bool $returnValueGeneration
*
* @throws Exception
* @throws RuntimeException
* @throws \PHPUnit\Framework\Exception
* @throws \ReflectionException
*
* @return MockObject
*/
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 Down Expand Up @@ -220,7 +218,7 @@ function ($type) {
*
* @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 +251,12 @@ public function getMockForAbstractClass($originalClassName, array $arguments = [
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$cloneArguments
$cloneArguments,
false,
null,
true,
true,
$methodsExcept
);
}

Expand Down Expand Up @@ -281,7 +284,7 @@ public function getMockForAbstractClass($originalClassName, array $arguments = [
*
* @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 +324,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 @@ -398,7 +401,7 @@ public function getObjectForTrait($traitName, array $arguments = [], $traitClass
*
* @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 +415,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 +436,8 @@ public function generate($type, array $methods = null, $mockClassName = '', $cal
$callOriginalClone,
$callAutoload,
$cloneArguments,
$callOriginalMethods
$callOriginalMethods,
$methodsExcepted
);
}

Expand Down Expand Up @@ -548,12 +554,16 @@ public function getClassMethods($className): array
*
* @return MockMethod[]
*/
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 +665,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 +841,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
5 changes: 3 additions & 2 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -1498,7 +1498,7 @@ protected function getMockFromWsdl($wsdlFile, $originalClassName = '', $mockClas
* @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 +1508,8 @@ protected function getMockForTrait($traitName, array $arguments = [], $mockClass
$callOriginalClone,
$callAutoload,
$mockedMethods,
$cloneArguments
$cloneArguments,
$methodsExcept
);

$this->registerMockObject($mockObject);
Expand Down
20 changes: 20 additions & 0 deletions tests/_files/ClassWithSinglePublicMethod.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

/**
* A class with only one public method
*/
class ClassWithSinglePublicMethod
{
public function falseReturn()
{
return false;
}
}
9 changes: 9 additions & 0 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,13 @@ public function testProvidesAFluentInterface(): void

$this->assertInstanceOf(MockBuilder::class, $spec);
}

public function testExceptMethodFromClassWithSinglePublicMethod(): void
{
$spec = $this->getMockBuilder(ClassWithSinglePublicMethod::class)
->setMethodsExcept(['falseReturn'])
->getMock();

$this->assertFalse($spec->falseReturn());
}
}