Skip to content

Commit

Permalink
Closes #3769
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Dec 10, 2020
1 parent 0788048 commit baa0cce
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 136 deletions.
7 changes: 1 addition & 6 deletions .psalm/baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -473,9 +473,6 @@
<code>$this-&gt;type</code>
<code>$this-&gt;type</code>
</ArgumentTypeCoercion>
<DeprecatedMethod occurrences="1">
<code>setMethods</code>
</DeprecatedMethod>
<InvalidReturnStatement occurrences="3">
<code>$object</code>
<code>$object</code>
Expand Down Expand Up @@ -608,9 +605,6 @@
<ArgumentTypeCoercion occurrences="1">
<code>$this-&gt;expectedException</code>
</ArgumentTypeCoercion>
<DeprecatedMethod occurrences="1">
<code>setMethods</code>
</DeprecatedMethod>
<DocblockTypeContradiction occurrences="2">
<code>$this-&gt;backupStaticAttributes === null</code>
<code>$this-&gt;runTestInSeparateProcess === null</code>
Expand Down Expand Up @@ -652,6 +646,7 @@
<code>getMockForTrait</code>
<code>getObjectForTrait</code>
<code>new Differ($header)</code>
<code>onlyMethods</code>
</MissingThrowsDocblock>
<PossiblyNullReference occurrences="4">
<code>passed</code>
Expand Down
1 change: 1 addition & 0 deletions ChangeLog-10.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ All notable changes of the PHPUnit 10.0 release series are documented in this fi

### Removed

* [#3769](https://github.com/sebastianbergmann/phpunit/issues/3769): Remove `MockBuilder::setMethods()` and `MockBuilder::setMethodsExcept()`
* [#3870](https://github.com/sebastianbergmann/phpunit/issues/3870): Drop support for PHP 7.3
* [#4063](https://github.com/sebastianbergmann/phpunit/issues/4063): Remove `assertNotIsReadable()`
* [#4066](https://github.com/sebastianbergmann/phpunit/issues/4066): Remove `assertNotIsWritable()`
Expand Down
36 changes: 0 additions & 36 deletions src/Framework/MockObject/MockBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*/
namespace PHPUnit\Framework\MockObject;

use function array_diff;
use function array_merge;
use PHPUnit\Framework\TestCase;
use ReflectionClass;
Expand Down Expand Up @@ -157,24 +156,6 @@ public function getMockForTrait(): MockObject
return $object;
}

/**
* Specifies the subset of methods to mock. Default is to mock none of them.
*
* @deprecated https://github.com/sebastianbergmann/phpunit/pull/3687
*
* @return $this
*/
public function setMethods(?array $methods = null): self
{
if ($methods === null) {
$this->methods = $methods;
} else {
$this->methods = array_merge($this->methods ?? [], $methods);
}

return $this;
}

/**
* Specifies the subset of methods to mock, requiring each to exist in the class.
*
Expand Down Expand Up @@ -258,23 +239,6 @@ public function addMethods(array $methods): self
return $this;
}

/**
* Specifies the subset of methods to not mock. Default is to mock all of them.
*
* @deprecated https://github.com/sebastianbergmann/phpunit/pull/3687
*
* @throws ReflectionException
*/
public function setMethodsExcept(array $methods = []): self
{
return $this->setMethods(
array_diff(
$this->generator->getClassMethods($this->type),
$methods
)
);
}

/**
* Specifies the arguments for the constructor.
*
Expand Down
32 changes: 1 addition & 31 deletions src/Framework/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
use const PATHINFO_FILENAME;
use const PHP_EOL;
use const PHP_URL_PATH;
use function array_filter;
use function array_flip;
use function array_keys;
use function array_merge;
Expand Down Expand Up @@ -1587,41 +1586,12 @@ protected function createConfiguredMock(string $originalClassName, array $config
*/
protected function createPartialMock(string $originalClassName, array $methods): MockObject
{
try {
$reflector = new ReflectionClass($originalClassName);
// @codeCoverageIgnoreStart
} catch (ReflectionException $e) {
throw new Exception(
$e->getMessage(),
(int) $e->getCode(),
$e
);
}
// @codeCoverageIgnoreEnd

$mockedMethodsThatDontExist = array_filter(
$methods,
static function (string $method) use ($reflector) {
return !$reflector->hasMethod($method);
}
);

if ($mockedMethodsThatDontExist) {
$this->addWarning(
sprintf(
'createPartialMock() called with method(s) %s that do not exist in %s. This will not be allowed in future versions of PHPUnit.',
implode(', ', $mockedMethodsThatDontExist),
$originalClassName
)
);
}

return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->disableOriginalClone()
->disableArgumentCloning()
->disallowMockingUnknownTypes()
->setMethods(empty($methods) ? null : $methods)
->onlyMethods($methods)
->getMock();
}

Expand Down
61 changes: 0 additions & 61 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,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 @@ -132,58 +123,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
4 changes: 2 additions & 2 deletions tests/unit/Framework/TestCaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -943,8 +943,8 @@ public function testCreatePartialMockWithFakeMethods(): void

$test->run();

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

public function testCreatePartialMockWithRealMethods(): void
Expand Down

0 comments on commit baa0cce

Please sign in to comment.