Skip to content

Commit

Permalink
Only allow new mock methods to configure once
Browse files Browse the repository at this point in the history
  • Loading branch information
DFoxinator committed May 15, 2019
1 parent a58c6d9 commit 29f04eb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
17 changes: 17 additions & 0 deletions src/Framework/MockObject/MockBuilder.php
Expand Up @@ -83,6 +83,11 @@ final class MockBuilder
*/
private $generator;

/**
* @var bool
*/
private $alreadyUsedMockMethodConfiguration = false;

/**
* @param array|string $type
*/
Expand Down Expand Up @@ -189,6 +194,12 @@ public function setMethods(array $methods = null): self
*/
public function onlyMethods(array $methods): self
{
if ($this->alreadyUsedMockMethodConfiguration) {
throw new RuntimeException('Can\'t use onlyMethods after already configuring mock methods on the mock.');
}

$this->alreadyUsedMockMethodConfiguration = true;

$reflection = new \ReflectionClass($this->type);

foreach ($methods as $method) {
Expand Down Expand Up @@ -217,6 +228,12 @@ public function onlyMethods(array $methods): self
*/
public function addMethods(array $methods): self
{
if ($this->alreadyUsedMockMethodConfiguration) {
throw new RuntimeException('Can\'t use addMethods after already configuring mock methods on the mock.');
}

$this->alreadyUsedMockMethodConfiguration = true;

$reflection = new \ReflectionClass($this->type);

foreach ($methods as $method) {
Expand Down
20 changes: 20 additions & 0 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Expand Up @@ -108,6 +108,26 @@ public function testEmptyMethodExceptionsToMockCanBeSpecified(): void
$this->assertNull($mock->anotherMockableMethod());
}

public function testNotAbleToUseAddMethodsAfterOnlyMethods(): void
{
$this->expectException(RuntimeException::class);

$this->getMockBuilder(Mockable::class)
->onlyMethods(['mockableMethod'])
->addMethods(['mockableMethodWithFakeMethod'])
->getMock();
}

public function testNotAbleToUseOnlyMethodsAfterAddMethods(): void
{
$this->expectException(RuntimeException::class);

$this->getMockBuilder(Mockable::class)
->addMethods(['mockableMethodWithFakeMethod'])
->onlyMethods(['mockableMethod'])
->getMock();
}

public function testByDefaultDoesNotPassArgumentsToTheConstructor(): void
{
$mock = $this->getMockBuilder(Mockable::class)->getMock();
Expand Down

0 comments on commit 29f04eb

Please sign in to comment.