Skip to content

Commit

Permalink
Closes #5313
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 6, 2024
1 parent 3b2c9bb commit 9edff8d
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 169 deletions.
1 change: 1 addition & 0 deletions ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ All notable changes of the PHPUnit 12.0 release series are documented in this fi

* [#5247](https://github.com/sebastianbergmann/phpunit/issues/5247): `TestCase::getMockForAbstractClass()`
* [#5249](https://github.com/sebastianbergmann/phpunit/issues/5249): `TestCase::getMockForTrait()`
* [#5313](https://github.com/sebastianbergmann/phpunit/issues/5313): `MockBuilder::getMockForTrait()`
* [#5314](https://github.com/sebastianbergmann/phpunit/issues/5314): `MockBuilder::getMockForAbstractClass()`
* [#5978](https://github.com/sebastianbergmann/phpunit/issues/5978): Support for PHP 8.2

Expand Down
1 change: 0 additions & 1 deletion DEPRECATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co
| [#5240](https://github.com/sebastianbergmann/phpunit/issues/5240) | `TestCase::createTestProxy()` | 10.1.0 | |
| [#5242](https://github.com/sebastianbergmann/phpunit/issues/5242) | `TestCase::getMockFromWsdl()` | 10.1.0 | |
| [#5244](https://github.com/sebastianbergmann/phpunit/issues/5244) | `TestCase::getObjectForTrait()` | 10.1.0 | |
| [#5306](https://github.com/sebastianbergmann/phpunit/issues/5306) | `MockBuilder::getMockForTrait()` | 10.1.0 | |
| [#5307](https://github.com/sebastianbergmann/phpunit/issues/5307) | `MockBuilder::disableProxyingToOriginalMethods()` | 10.1.0 | |
| [#5307](https://github.com/sebastianbergmann/phpunit/issues/5307) | `MockBuilder::enableProxyingToOriginalMethods()` | 10.1.0 | |
| [#5307](https://github.com/sebastianbergmann/phpunit/issues/5307) | `MockBuilder::setProxyTarget()` | 10.1.0 | |
Expand Down
115 changes: 0 additions & 115 deletions src/Framework/MockObject/Generator/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
use PHPUnit\Event\Code\TestMethodBuilder;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Framework\MockObject\ConfigurableMethod;
use PHPUnit\Framework\MockObject\DoubledCloneMethod;
use PHPUnit\Framework\MockObject\GeneratedAsMockObject;
Expand Down Expand Up @@ -234,120 +233,6 @@ public function testDoubleForInterfaceIntersection(array $interfaces, bool $mock
);
}

/**
* Returns a mock object for the specified abstract class with all abstract
* methods of the class mocked.
*
* Concrete methods to mock can be specified with the $mockedMethods parameter.
*
* @param list<mixed> $arguments
* @param ?list<non-empty-string> $mockedMethods
*
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws DuplicateMethodException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
* @throws NameAlreadyInUseException
* @throws OriginalConstructorInvocationRequiredException
* @throws ReflectionException
* @throws RuntimeException
* @throws UnknownClassException
* @throws UnknownTypeException
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5241
*/
public function mockObjectForAbstractClass(string $originalClassName, array $arguments = [], string $mockClassName = '', bool $callOriginalConstructor = true, bool $callOriginalClone = true, bool $callAutoload = true, ?array $mockedMethods = null, bool $cloneArguments = true): MockObject
{
if (class_exists($originalClassName, $callAutoload) ||
interface_exists($originalClassName, $callAutoload)) {
$reflector = $this->reflectClass($originalClassName);
$methods = $mockedMethods;

foreach ($reflector->getMethods() as $method) {
if ($method->isAbstract() && !in_array($method->getName(), $methods ?? [], true)) {
$methods[] = $method->getName();
}
}

if (empty($methods)) {
$methods = null;
}

$mockObject = $this->testDouble(
$originalClassName,
true,
true,
$methods,
$arguments,
$mockClassName,
$callOriginalConstructor,
$callOriginalClone,
$callAutoload,
$cloneArguments,
);

assert($mockObject instanceof $originalClassName);
assert($mockObject instanceof MockObject);

return $mockObject;
}

throw new UnknownClassException($originalClassName);
}

/**
* Returns a mock object for the specified trait with all abstract methods
* of the trait mocked. Concrete methods to mock can be specified with the
* `$mockedMethods` parameter.
*
* @param trait-string $traitName
* @param list<mixed> $arguments
* @param ?list<non-empty-string> $mockedMethods
*
* @throws ClassIsEnumerationException
* @throws ClassIsFinalException
* @throws DuplicateMethodException
* @throws InvalidArgumentException
* @throws InvalidMethodNameException
* @throws NameAlreadyInUseException
* @throws OriginalConstructorInvocationRequiredException
* @throws ReflectionException
* @throws RuntimeException
* @throws UnknownClassException
* @throws UnknownTraitException
* @throws UnknownTypeException
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5243
*/
public function mockObjectForTrait(string $traitName, array $arguments = [], string $mockClassName = '', bool $callOriginalConstructor = true, bool $callOriginalClone = true, bool $callAutoload = true, ?array $mockedMethods = null, bool $cloneArguments = true): MockObject
{
if (!trait_exists($traitName, $callAutoload)) {
throw new UnknownTraitException($traitName);
}

$className = $this->generateClassName(
$traitName,
'',
'Trait_',
);

$classTemplate = $this->loadTemplate('trait_class.tpl');

$classTemplate->setVar(
[
'prologue' => 'abstract ',
'class_name' => $className['className'],
'trait_name' => $traitName,
],
);

$mockTrait = new MockTrait($classTemplate->render(), $className['className']);
$mockTrait->generate();

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

/**
* Returns an object for the specified trait.
*
Expand Down
40 changes: 0 additions & 40 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
use function array_merge;
use function assert;
use function debug_backtrace;
use function trait_exists;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Framework\Exception;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
Expand Down Expand Up @@ -123,44 +121,6 @@ public function getMock(): MockObject
return $object;
}

/**
* Creates a mock object for a trait using a fluent interface.
*
* @throws Exception
* @throws ReflectionException
* @throws RuntimeException
*
* @return MockedType&MockObject
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5306
*/
public function getMockForTrait(): MockObject
{
EventFacade::emitter()->testTriggeredPhpunitDeprecation(
$this->testCase->valueObjectForEvents(),
'MockBuilder::getMockForTrait() is deprecated and will be removed in PHPUnit 12 without replacement.',
);

assert(trait_exists($this->type));

$object = $this->generator->mockObjectForTrait(
$this->type,
$this->constructorArgs,
$this->mockClassName ?? '',
$this->originalConstructor,
$this->originalClone,
$this->autoload,
$this->methods,
$this->cloneArguments,
);

assert($object instanceof MockObject);

$this->testCase->registerMockObject($object);

return $object;
}

/**
* Specifies the subset of methods to mock, requiring each to exist in the class.
*
Expand Down
13 changes: 0 additions & 13 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
use PHPUnit\TestFixture\MockObject\ExtendableClass;
use PHPUnit\TestFixture\MockObject\ExtendableClassCallingMethodInConstructor;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod;

#[CoversClass(MockBuilder::class)]
#[CoversClass(CannotUseAddMethodsException::class)]
Expand Down Expand Up @@ -112,18 +111,6 @@ public function testCannotCreateMockObjectForExtendableClassAddingMethodToItWith
->getMock();
}

#[IgnorePhpunitDeprecations]
#[TestDox('getMockForTrait() can be used to create a mock object for a trait')]
public function testCreatesMockObjectForTraitAndAllowsConfigurationOfMethods(): void
{
$double = $this->getMockBuilder(TraitWithConcreteAndAbstractMethod::class)
->getMockForTrait();

$double->method('abstractMethod')->willReturn(true);

$this->assertTrue($double->concreteMethod());
}

#[TestDox('onlyMethods() can be used to configure which methods should be doubled')]
public function testCreatesPartialMockObjectForExtendableClass(): void
{
Expand Down

0 comments on commit 9edff8d

Please sign in to comment.