Skip to content

Commit

Permalink
Closes #5321
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Apr 17, 2024
1 parent 682bbbe commit b17c1e8
Show file tree
Hide file tree
Showing 3 changed files with 0 additions and 132 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\ClassAlreadyExistsException;
use PHPUnit\Framework\MockObject\Generator\ClassIsEnumerationException;
Expand Down Expand Up @@ -157,55 +156,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
53 changes: 0 additions & 53 deletions tests/unit/Framework/MockObject/Creation/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@
use PHPUnit\Framework\Attributes\Medium;
use PHPUnit\Framework\Attributes\TestDox;
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)]
#[Group('test-doubles')]
#[Group('test-doubles/creation')]
#[Group('test-doubles/mock-object')]
Expand All @@ -42,56 +39,6 @@ public function testCanCreateMockObjectWithSpecifiedClassName(): void
$this->assertSame($className, $double::class);
}

#[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 testCannotCreateMockObjectForExtendableClassAddingMethodsToItThatItAlreadyHas(): void
{
$this->expectException(CannotUseAddMethodsException::class);

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

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

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

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

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

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

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

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

0 comments on commit b17c1e8

Please sign in to comment.