From f4659b290574c53aa8f5cc605ea8c8d648c72bf1 Mon Sep 17 00:00:00 2001 From: David Fox Date: Fri, 2 Aug 2019 14:58:12 -0400 Subject: [PATCH] onlyMethods/addMethods - blank arrays fix --- src/Framework/MockObject/MockBuilder.php | 12 ++++++++++++ .../Framework/MockObject/MockBuilderTest.php | 18 ++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/src/Framework/MockObject/MockBuilder.php b/src/Framework/MockObject/MockBuilder.php index 73a34a4e3f4..8c884a45fd0 100644 --- a/src/Framework/MockObject/MockBuilder.php +++ b/src/Framework/MockObject/MockBuilder.php @@ -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( @@ -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( diff --git a/tests/unit/Framework/MockObject/MockBuilderTest.php b/tests/unit/Framework/MockObject/MockBuilderTest.php index f7034ac8981..12ff2e19717 100644 --- a/tests/unit/Framework/MockObject/MockBuilderTest.php +++ b/tests/unit/Framework/MockObject/MockBuilderTest.php @@ -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); @@ -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)