Skip to content

Commit

Permalink
Add warning to MockBuilder::setMethods() about deprecated method
Browse files Browse the repository at this point in the history
  • Loading branch information
adrmrn authored and sebastianbergmann committed Sep 25, 2022
1 parent c9da1dc commit 5d4feca
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 61 deletions.
22 changes: 22 additions & 0 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,8 @@ public function getMockForTrait(): MockObject
*/
public function setMethods(?array $methods = null): self
{
$this->createWarning('setMethods() and setMethodsExcept() methods are deprecated and will be removed in PHPUnit 10. Refactor your code to use addMethods() and onlyMethods() instead.');

if ($methods === null) {
$this->methods = $methods;
} else {
Expand Down Expand Up @@ -513,4 +515,24 @@ public function disableAutoReturnValueGeneration(): self

return $this;
}

/**
* @codeCoverageIgnore
*/
private function createWarning(string $warning): void
{
foreach (debug_backtrace() as $step) {
if (!isset($step['object']) || !$step['object'] instanceof TestCase) {
continue;
}

if ($step['function'] === 'createPartialMock') {
return;
}

$step['object']->addWarning($warning);

return;
}
}
}
12 changes: 12 additions & 0 deletions tests/_files/TestWithDifferentStatuses.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,16 @@ public function testWithCreatePartialMockPassesNoWarning(): void

$this->assertNull($mock->mockableMethod());
}

public function testWithSetMethodsWarning(): void
{
$mockBuilder = $this->getMockBuilder(Mockable::class);
$mockBuilder->setMethods(['mockableMethod', 'anotherMockableMethod']);
}

public function testWithSetMethodsExceptWarning(): void
{
$mockBuilder = $this->getMockBuilder(Mockable::class);
$mockBuilder->setMethodsExcept(['mockableMethod', 'anotherMockableMethod']);
}
}
61 changes: 0 additions & 61 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,6 @@ public function testMethodsToMockCanBeSpecified(): void
$this->assertTrue($mock->anotherMockableMethod());
}

public function testSetMethodsAllowsNonExistentMethodNames(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->setMethods(['mockableMethodWithCrazyName'])
->getMock();

$this->assertNull($mock->mockableMethodWithCrazyName());
}

public function testOnlyMethodsWithNonExistentMethodNames(): void
{
$this->expectException(CannotUseOnlyMethodsException::class);
Expand Down Expand Up @@ -139,58 +130,6 @@ public function testAbleToUseOnlyMethodsAfterAddMethods(): void
$this->assertNull($mock->mockableMethod());
}

public function testAbleToUseSetMethodsAfterOnlyMethods(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->onlyMethods(['mockableMethod'])
->setMethods(['mockableMethodWithCrazyName'])
->getMock();

$this->assertNull($mock->mockableMethodWithCrazyName());
}

public function testAbleToUseSetMethodsAfterAddMethods(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->addMethods(['notAMethod'])
->setMethods(['mockableMethodWithCrazyName'])
->getMock();

$this->assertNull($mock->mockableMethodWithCrazyName());
}

public function testAbleToUseAddMethodsAfterSetMethods(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->setMethods(['mockableMethod'])
->addMethods(['mockableMethodWithFakeMethod'])
->getMock();

$this->assertNull($mock->mockableMethod());
$this->assertNull($mock->mockableMethodWithFakeMethod());
}

public function testAbleToUseOnlyMethodsAfterSetMethods(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->setMethods(['mockableMethodWithFakeMethod'])
->onlyMethods(['mockableMethod'])
->getMock();

$this->assertNull($mock->mockableMethod());
$this->assertNull($mock->mockableMethodWithFakeMethod());
}

public function testAbleToUseAddMethodsAfterSetMethodsWithNull(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->setMethods()
->addMethods(['mockableMethodWithFakeMethod'])
->getMock();

$this->assertNull($mock->mockableMethodWithFakeMethod());
}

public function testByDefaultDoesNotPassArgumentsToTheConstructor(): void
{
$mock = $this->getMockBuilder(Mockable::class)->getMock();
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/Framework/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1317,6 +1317,26 @@ public function testSetDependencyInput(): void
$this->assertCount(1, $result);
}

public function testMockBuilderSetMethodsWarning(): void
{
$test = new TestWithDifferentStatuses('testWithSetMethodsWarning');

$test->run();

$this->assertSame(BaseTestRunner::STATUS_WARNING, $test->getStatus());
$this->assertFalse($test->hasFailed());
}

public function testMockBuilderSetMethodsExceptWarning(): void
{
$test = new TestWithDifferentStatuses('testWithSetMethodsExceptWarning');

$test->run();

$this->assertSame(BaseTestRunner::STATUS_WARNING, $test->getStatus());
$this->assertFalse($test->hasFailed());
}

/**
* @return array<string, array>
*/
Expand Down

0 comments on commit 5d4feca

Please sign in to comment.