Skip to content

Commit

Permalink
Closes #5321
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 27, 2024
1 parent ff2ab81 commit 1e3b1c0
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 155 deletions.

This file was deleted.

50 changes: 0 additions & 50 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
use function assert;
use function debug_backtrace;
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 @@ -158,55 +157,6 @@ public function onlyMethods(array $methods): self
return $this;
}

/**
* Specifies methods that don't exist in the class which you want to mock.
*
* @psalm-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
} 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
76 changes: 0 additions & 76 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,14 @@
use PHPUnit\Framework\Attributes\IgnorePhpunitDeprecations;
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\Attributes\TestDox;
use PHPUnit\Framework\MockObject\Generator\CannotUseAddMethodsException;
use PHPUnit\Framework\MockObject\Generator\DuplicateMethodException;
use PHPUnit\Framework\MockObject\Generator\InvalidMethodNameException;
use PHPUnit\Framework\MockObject\Generator\NameAlreadyInUseException;
use PHPUnit\Framework\TestCase;
use PHPUnit\TestFixture\MockObject\AbstractClass;
use PHPUnit\TestFixture\MockObject\ExtendableClass;
use PHPUnit\TestFixture\MockObject\InterfaceWithReturnTypeDeclaration;
use PHPUnit\TestFixture\MockObject\TraitWithConcreteAndAbstractMethod;

#[CoversClass(MockBuilder::class)]
#[CoversClass(CannotUseAddMethodsException::class)]
#[CoversClass(DuplicateMethodException::class)]
#[CoversClass(InvalidMethodNameException::class)]
#[CoversClass(NameAlreadyInUseException::class)]
Expand Down Expand Up @@ -64,78 +60,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();
}

#[IgnorePhpunitDeprecations]
#[TestDox('getMockForAbstractClass() can be used to create a mock object for an abstract class')]
public function testCreatesMockObjectForAbstractClassAndAllowsConfigurationOfAbstractMethods(): void
{
$double = $this->getMockBuilder(AbstractClass::class)
->getMockForAbstractClass();

$double->expects($this->once())->method('doSomethingElse')->willReturn(true);

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

#[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());
}

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

0 comments on commit 1e3b1c0

Please sign in to comment.