Skip to content

Commit

Permalink
onlyMethods/addMethods - blank arrays fix
Browse files Browse the repository at this point in the history
  • Loading branch information
DFoxinator committed Aug 2, 2019
1 parent e77a49b commit ae94283
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .psalm/baseline.xml
Expand Up @@ -198,7 +198,7 @@
<PossiblyInvalidPropertyAssignmentValue occurrences="1">
<code>$type</code>
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyNullPropertyAssignmentValue occurrences="1">
<PossiblyNullPropertyAssignmentValue occurrences="3">
<code>null</code>
</PossiblyNullPropertyAssignmentValue>
<ArgumentTypeCoercion occurrences="2">
Expand Down
12 changes: 12 additions & 0 deletions src/Framework/MockObject/MockBuilder.php
Expand Up @@ -207,6 +207,12 @@ public function setMethods(array $methods = null): self
*/
public function onlyMethods(array $methods): self
{
if (!$methods) {
$this->methods = null;

return $this;
}

if ($this->alreadyUsedMockMethodConfiguration) {
throw new RuntimeException(
\sprintf(
Expand Down Expand Up @@ -254,6 +260,12 @@ public function onlyMethods(array $methods): self
*/
public function addMethods(array $methods): self
{
if (!$methods) {
$this->methods = null;

return $this;
}

if ($this->alreadyUsedMockMethodConfiguration) {
throw new RuntimeException(
\sprintf(
Expand Down
18 changes: 18 additions & 0 deletions tests/unit/Framework/MockObject/MockBuilderTest.php
Expand Up @@ -79,6 +79,15 @@ public function testOnlyMethodsWithExistingMethodNames(): void
$this->assertTrue($mock->anotherMockableMethod());
}

public function testOnlyMethodsWithBlankArray(): void
{
$mock = $this->getMockBuilder(Mockable::class)
->onlyMethods([])
->getMock();

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

public function testAddMethodsWithNonExistentMethodNames(): void
{
$this->expectException(RuntimeException::class);
Expand All @@ -98,6 +107,15 @@ public function testAddMethodsWithExistingMethodNames(): void
$this->assertTrue($mock->anotherMockableMethod());
}

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

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

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

0 comments on commit ae94283

Please sign in to comment.