Skip to content

Commit

Permalink
Closes #5321
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 6, 2024
1 parent 4adaf9b commit 4b851e1
Show file tree
Hide file tree
Showing 5 changed files with 1 addition and 132 deletions.
1 change: 1 addition & 0 deletions ChangeLog-12.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ All notable changes of the PHPUnit 12.0 release series are documented in this fi
* [#5250](https://github.com/sebastianbergmann/phpunit/issues/5250): `TestCase::getObjectForTrait()`
* [#5313](https://github.com/sebastianbergmann/phpunit/issues/5313): `MockBuilder::getMockForTrait()`
* [#5314](https://github.com/sebastianbergmann/phpunit/issues/5314): `MockBuilder::getMockForAbstractClass()`
* [#5321](https://github.com/sebastianbergmann/phpunit/issues/5321): `MockBuilder::addMethods()`
* [#5978](https://github.com/sebastianbergmann/phpunit/issues/5978): Support for PHP 8.2

[12.0.0]: https://github.com/sebastianbergmann/phpunit/compare/11.5...main
1 change: 0 additions & 1 deletion DEPRECATIONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ This functionality is currently [hard-deprecated](https://phpunit.de/backward-co
| [#5309](https://github.com/sebastianbergmann/phpunit/issues/5309) | `MockBuilder::enableAutoload()` | 10.1.0 | |
| [#5315](https://github.com/sebastianbergmann/phpunit/issues/5315) | `MockBuilder::disableArgumentCloning()` | 10.1.0 | |
| [#5315](https://github.com/sebastianbergmann/phpunit/issues/5315) | `MockBuilder::enableArgumentCloning()` | 10.1.0 | |
| [#5320](https://github.com/sebastianbergmann/phpunit/issues/5320) | `MockBuilder::addMethods()` | 10.1.0 | |
| [#5415](https://github.com/sebastianbergmann/phpunit/issues/5415) | Support for doubling interfaces (or classes) that have a method named `method` | 11.0.0 | |
| [#5423](https://github.com/sebastianbergmann/phpunit/issues/5423) | `TestCase::onConsecutiveCalls()` | 10.3.0 | Use `$double->willReturn()` instead of `$double->will($this->onConsecutiveCalls())` |
| [#5423](https://github.com/sebastianbergmann/phpunit/issues/5423) | `TestCase::returnArgument()` | 10.3.0 | Use `$double->willReturnArgument()` instead of `$double->will($this->returnArgument())` |
Expand Down

This file was deleted.

52 changes: 0 additions & 52 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use function debug_backtrace;
use PHPUnit\Event\Facade as EventFacade;
use PHPUnit\Framework\InvalidArgumentException;
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
use PHPUnit\Framework\MockObject\Generator\ClassIsFinalException;
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
Expand Down Expand Up @@ -164,57 +163,6 @@ public function onlyMethods(array $methods): self
return $this;
}

/**
* Specifies methods that don't exist in the class which you want to mock.
*
* @param list<non-empty-string> $methods
*
* @throws CannotUseAddMethodsException
* @throws ReflectionException
* @throws RuntimeException
*
* @return $this
*
* @deprecated https://github.com/sebastianbergmann/phpunit/issues/5320
*/
public function addMethods(array $methods): self
{
EventFacade::emitter()->testTriggeredPhpunitDeprecation(
$this->testCase->valueObjectForEvents(),
'MockBuilder::addMethods() is deprecated and will be removed in PHPUnit 12 without replacement.',
);

if (empty($methods)) {
$this->emptyMethodsArray = true;

return $this;
}

try {
$reflector = new ReflectionClass($this->type);

// @codeCoverageIgnoreStart
/** @phpstan-ignore catch.neverThrown */
} catch (\ReflectionException $e) {
throw new ReflectionException(
$e->getMessage(),
$e->getCode(),
$e,
);
// @codeCoverageIgnoreEnd
}

foreach ($methods as $method) {
if ($reflector->hasMethod($method)) {
throw new CannotUseAddMethodsException($this->type, $method);
}
}

$this->methods = array_merge($this->methods, $methods);

return $this;
}

/**
* Specifies the arguments for the constructor.
*
Expand Down
48 changes: 0 additions & 48 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,54 +63,6 @@ public function testCannotCreateMockObjectWithSpecifiedClassNameWhenClassWithTha
->getMock();
}

#[IgnorePhpunitDeprecations]
#[TestDox('addMethods() can be used to configure an additional method for the mock object class when the original class does not have a method of the same name')]
public function testCanCreateMockObjectForExtendableClassWhileAddingMethodsToIt(): void
{
$double = $this->getMockBuilder(ExtendableClass::class)
->addMethods(['additionalMethod'])
->getMock();

$value = 'value';

$double->method('additionalMethod')->willReturn($value);

$this->assertSame($value, $double->additionalMethod());
}

#[IgnorePhpunitDeprecations]
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class when the original class has a method of the same name')]
public function testCannotCreateMockObjectForExtendableClassAddingMethodToItThatItAlreadyHas(): void
{
$this->expectException(CannotUseAddMethodsException::class);

$this->getMockBuilder(ExtendableClass::class)
->addMethods(['doSomething'])
->getMock();
}

#[IgnorePhpunitDeprecations]
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class multiple times using the same name')]
public function testCannotCreateMockObjectForExtendableClassAddingMultipleMethodsWithSameNameToIt(): void
{
$this->expectException(DuplicateMethodException::class);

$this->getMockBuilder(ExtendableClass::class)
->addMethods(['additionalMethod', 'additionalMethod'])
->getMock();
}

#[IgnorePhpunitDeprecations]
#[TestDox('addMethods() cannot be used to configure an additional method for the mock object class with invalid name')]
public function testCannotCreateMockObjectForExtendableClassAddingMethodToItWithInvalidName(): void
{
$this->expectException(InvalidMethodNameException::class);

$this->getMockBuilder(ExtendableClass::class)
->addMethods(['1234'])
->getMock();
}

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

0 comments on commit 4b851e1

Please sign in to comment.