Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

onlyMethods/addMethods - blank arrays fix #3781

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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