From 6bb593c4b09de63f6f006ef37ffa13f7b54f5efc Mon Sep 17 00:00:00 2001 From: Alberto Villa Date: Mon, 19 Jun 2017 18:02:09 +0200 Subject: [PATCH 1/6] Add method to return strategy mock object type --- src/Moka/Strategy/AbstractMockingStrategy.php | 28 +++++++++++++++++-- .../Strategy/MockingStrategyInterface.php | 8 ++++++ .../IncompleteMockingStrategyTest.php | 4 +-- tests/Strategy/MockeryMockingStrategyTest.php | 2 -- tests/Strategy/MockingStrategyTestCase.php | 15 ++++------ tests/Strategy/PHPUnitMockingStrategyTest.php | 2 -- .../Strategy/ProphecyMockingStrategyTest.php | 1 - 7 files changed, 40 insertions(+), 20 deletions(-) diff --git a/src/Moka/Strategy/AbstractMockingStrategy.php b/src/Moka/Strategy/AbstractMockingStrategy.php index 83e2c62..67570a9 100644 --- a/src/Moka/Strategy/AbstractMockingStrategy.php +++ b/src/Moka/Strategy/AbstractMockingStrategy.php @@ -69,6 +69,18 @@ public function get($mock) return $this->doGet($mock); } + /** + * @return string + * + * @throws NotImplementedException + */ + public function getMockType(): string + { + $this->verifyMockType(); + + return $this->mockType; + } + /** * @param string $fqcn */ @@ -85,9 +97,7 @@ final protected function setMockType(string $fqcn) */ final protected function checkMockType($mock) { - if (!$this->mockType) { - throw new NotImplementedException(); - } + $this->verifyMockType(); if (!is_a($mock, $this->mockType)) { throw new InvalidArgumentException( @@ -118,4 +128,16 @@ abstract protected function doDecorate($mock, StubSet $stubs); * @return object */ abstract protected function doGet($mock); + + /** + * @return void + * + * @throws NotImplementedException + */ + private function verifyMockType() + { + if (!$this->mockType) { + throw new NotImplementedException('Mock type was not defined'); + } + } } diff --git a/src/Moka/Strategy/MockingStrategyInterface.php b/src/Moka/Strategy/MockingStrategyInterface.php index 502876e..6c9ad20 100644 --- a/src/Moka/Strategy/MockingStrategyInterface.php +++ b/src/Moka/Strategy/MockingStrategyInterface.php @@ -5,6 +5,7 @@ use Moka\Exception\InvalidArgumentException; use Moka\Exception\MockNotCreatedException; +use Moka\Exception\NotImplementedException; use Moka\Stub\StubSet; /** @@ -38,4 +39,11 @@ public function decorate($mock, StubSet $stubs); * @throws MockNotCreatedException */ public function get($mock); + + /** + * @return string + * + * @throws NotImplementedException + */ + public function getMockType(): string; } diff --git a/tests/Strategy/IncompleteMockingStrategyTest.php b/tests/Strategy/IncompleteMockingStrategyTest.php index 4520456..b913aa9 100644 --- a/tests/Strategy/IncompleteMockingStrategyTest.php +++ b/tests/Strategy/IncompleteMockingStrategyTest.php @@ -9,11 +9,11 @@ class IncompleteMockingStrategyTest extends TestCase { - public function testCheckMockTypeFailure() + public function testGetMockTypeFailure() { $strategy = new IncompleteMockingStrategy(); $this->expectException(NotImplementedException::class); - $strategy->get(new \stdClass()); + $strategy->getMockType(); } } diff --git a/tests/Strategy/MockeryMockingStrategyTest.php b/tests/Strategy/MockeryMockingStrategyTest.php index 6c01079..1212418 100644 --- a/tests/Strategy/MockeryMockingStrategyTest.php +++ b/tests/Strategy/MockeryMockingStrategyTest.php @@ -3,7 +3,6 @@ namespace Tests\Strategy; -use Mockery\MockInterface; use Moka\Exception\MockNotCreatedException; use Moka\Strategy\MockeryMockingStrategy; use Tests\TestClass; @@ -15,7 +14,6 @@ public function __construct($name = null, array $data = [], $dataName = '') parent::__construct($name, $data, $dataName); $this->setStrategy(new MockeryMockingStrategy()); - $this->setMockType(MockInterface::class); } public function testBuildEmptyFQCNFailure() diff --git a/tests/Strategy/MockingStrategyTestCase.php b/tests/Strategy/MockingStrategyTestCase.php index c5e0f89..4f3c845 100644 --- a/tests/Strategy/MockingStrategyTestCase.php +++ b/tests/Strategy/MockingStrategyTestCase.php @@ -17,11 +17,6 @@ abstract class MockingStrategyTestCase extends TestCase */ protected $strategy; - /** - * @var string - */ - protected $mockType; - /** * @var StubSet */ @@ -40,7 +35,7 @@ public function testBuildSuccess() { $mock = $this->strategy->build(TestClass::class); - $this->assertInstanceOf($this->mockType, $mock); + $this->assertInstanceOf($this->strategy->getMockType(), $mock); } public function testDecorateSuccess() @@ -74,13 +69,13 @@ public function testGetFailure() $this->strategy->get(new \stdClass()); } - protected function setStrategy(MockingStrategyInterface $strategy) + public function testGetMockTypeSuccess() { - $this->strategy = $strategy; + $this->assertInternalType('string', $this->strategy->getMockType()); } - protected function setMockType(string $fqcn) + protected function setStrategy(MockingStrategyInterface $strategy) { - $this->mockType = $fqcn; + $this->strategy = $strategy; } } diff --git a/tests/Strategy/PHPUnitMockingStrategyTest.php b/tests/Strategy/PHPUnitMockingStrategyTest.php index b6be72e..458ed38 100644 --- a/tests/Strategy/PHPUnitMockingStrategyTest.php +++ b/tests/Strategy/PHPUnitMockingStrategyTest.php @@ -5,7 +5,6 @@ use Moka\Exception\MockNotCreatedException; use Moka\Strategy\PHPUnitMockingStrategy; -use PHPUnit_Framework_MockObject_MockObject as MockObject; use Tests\TestClass; class PHPUnitMockingStrategyTest extends MockingStrategyTestCase @@ -15,7 +14,6 @@ public function __construct($name = null, array $data = [], $dataName = '') parent::__construct($name, $data, $dataName); $this->setStrategy(new PHPUnitMockingStrategy()); - $this->setMockType(MockObject::class); } public function testBuildEmptyFQCNFailure() diff --git a/tests/Strategy/ProphecyMockingStrategyTest.php b/tests/Strategy/ProphecyMockingStrategyTest.php index 7ab3a2a..b9c0af5 100644 --- a/tests/Strategy/ProphecyMockingStrategyTest.php +++ b/tests/Strategy/ProphecyMockingStrategyTest.php @@ -17,7 +17,6 @@ public function __construct($name = null, array $data = [], $dataName = '') parent::__construct($name, $data, $dataName); $this->setStrategy(new ProphecyMockingStrategy()); - $this->setMockType(ObjectProphecy::class); } public function testGetCustomMockFailure() From d2b65f5654ad56c1b1954d51dfbb1fa55945cfba Mon Sep 17 00:00:00 2001 From: Alberto Villa Date: Mon, 19 Jun 2017 18:09:08 +0200 Subject: [PATCH 2/6] Verify multiple calls to method stubs --- tests/Strategy/MockingStrategyTestCase.php | 11 ++++++++++- tests/TestClass.php | 5 +++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tests/Strategy/MockingStrategyTestCase.php b/tests/Strategy/MockingStrategyTestCase.php index 4f3c845..979fdcd 100644 --- a/tests/Strategy/MockingStrategyTestCase.php +++ b/tests/Strategy/MockingStrategyTestCase.php @@ -27,6 +27,7 @@ public function setUp() // Mocking a StubSet is way too difficult. $this->stubs = StubFactory::fromArray([ 'isValid' => false, + 'getInt' => 3, 'throwException' => new \Exception() ]); } @@ -38,7 +39,7 @@ public function testBuildSuccess() $this->assertInstanceOf($this->strategy->getMockType(), $mock); } - public function testDecorateSuccess() + public function testDecorateSingleCallSuccess() { $mock = $this->strategy->build(TestClass::class); $this->strategy->decorate($mock, $this->stubs); @@ -48,6 +49,14 @@ public function testDecorateSuccess() $this->strategy->get($mock)->throwException(); } + public function testDecorateMultipleCallsSuccess() + { + $mock = $this->strategy->build(TestClass::class); + $this->strategy->decorate($mock, $this->stubs); + $this->assertSame(3, $this->strategy->get($mock)->getInt()); + $this->assertSame(3, $this->strategy->get($mock)->getInt()); + } + public function testDecorateFailure() { $this->expectException(InvalidArgumentException::class); diff --git a/tests/TestClass.php b/tests/TestClass.php index 8125b23..62fda06 100644 --- a/tests/TestClass.php +++ b/tests/TestClass.php @@ -10,6 +10,11 @@ public function isValid(): bool return true; } + public function getInt(): int + { + return 1; + } + public function throwException() { } From c68723262608889f027dc610dacab1b588c4c084 Mon Sep 17 00:00:00 2001 From: Alberto Villa Date: Mon, 19 Jun 2017 18:56:07 +0200 Subject: [PATCH 3/6] Add some tests for mock FQCN value --- src/Moka/Strategy/AbstractMockingStrategy.php | 4 +-- tests/Strategy/MockeryMockingStrategyTest.php | 29 +++++++++++++++ tests/Strategy/MockingStrategyTestCase.php | 6 ++++ tests/Strategy/PHPUnitMockingStrategyTest.php | 14 ++++++++ .../Strategy/ProphecyMockingStrategyTest.php | 36 +++++++++++++++++-- 5 files changed, 84 insertions(+), 5 deletions(-) diff --git a/src/Moka/Strategy/AbstractMockingStrategy.php b/src/Moka/Strategy/AbstractMockingStrategy.php index 67570a9..d9bf3bf 100644 --- a/src/Moka/Strategy/AbstractMockingStrategy.php +++ b/src/Moka/Strategy/AbstractMockingStrategy.php @@ -32,7 +32,7 @@ public function build(string $fqcn) } catch (\Throwable $exception) { throw new MockNotCreatedException( sprintf( - 'Cannot create mock object for FQCN %s: %s', + 'Cannot create mock object for FQCN "%s": %s', $fqcn, $exception->getMessage() ) @@ -102,7 +102,7 @@ final protected function checkMockType($mock) if (!is_a($mock, $this->mockType)) { throw new InvalidArgumentException( sprintf( - 'Mock must be of type %s, %s given', + 'Mock must be of type "%s", "%s" given', $this->mockType, gettype($mock) ) diff --git a/tests/Strategy/MockeryMockingStrategyTest.php b/tests/Strategy/MockeryMockingStrategyTest.php index 1212418..28aceae 100644 --- a/tests/Strategy/MockeryMockingStrategyTest.php +++ b/tests/Strategy/MockeryMockingStrategyTest.php @@ -30,6 +30,20 @@ public function testBuildParseFailure() $this->strategy->build('foo bar'); } + public function testBuildMultipleFQCNSuccess() + { + $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + + $this->assertTrue(true); + } + + public function testBuildMultipleFakeFQCNFailure() + { + $this->expectException(MockNotCreatedException::class); + + $this->strategy->build('foo, bar'); + } + public function testCallMissingMethodFailure() { $mock = $this->strategy->build(TestClass::class); @@ -37,4 +51,19 @@ public function testCallMissingMethodFailure() $this->expectException(\Throwable::class); $this->strategy->get($mock)->getSelf(); } + + public function testGetFakeFQCNSuccess() + { + $mock = $this->strategy->build('foo'); + + $this->assertTrue(is_a($this->strategy->get($mock), 'foo')); + } + + public function testGetMultipleFQCNPartialSuccess() + { + $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + + $this->assertFalse(is_a($this->strategy->get($mock), TestClass::class)); + $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + } } diff --git a/tests/Strategy/MockingStrategyTestCase.php b/tests/Strategy/MockingStrategyTestCase.php index 979fdcd..f51efe0 100644 --- a/tests/Strategy/MockingStrategyTestCase.php +++ b/tests/Strategy/MockingStrategyTestCase.php @@ -39,6 +39,12 @@ public function testBuildSuccess() $this->assertInstanceOf($this->strategy->getMockType(), $mock); } + public function testBuildFakeFQCNSuccess() + { + $this->strategy->build('foo'); + + $this->assertTrue(true); + } public function testDecorateSingleCallSuccess() { $mock = $this->strategy->build(TestClass::class); diff --git a/tests/Strategy/PHPUnitMockingStrategyTest.php b/tests/Strategy/PHPUnitMockingStrategyTest.php index 458ed38..1f73357 100644 --- a/tests/Strategy/PHPUnitMockingStrategyTest.php +++ b/tests/Strategy/PHPUnitMockingStrategyTest.php @@ -30,10 +30,24 @@ public function testBuildParseFailure() $this->strategy->build('foo bar'); } + public function testBuildMultipleFQCNFailure() + { + $this->expectException(MockNotCreatedException::class); + + $this->strategy->build('foo, bar'); + } + public function testCallMissingMethodSuccess() { $mock = $this->strategy->build(TestClass::class); $this->assertInstanceOf(TestClass::class, $this->strategy->get($mock)->getSelf()); } + + public function testGetFakeFQCNSuccess() + { + $mock = $this->strategy->build('foo'); + + $this->assertTrue(is_a($this->strategy->get($mock), 'foo')); + } } diff --git a/tests/Strategy/ProphecyMockingStrategyTest.php b/tests/Strategy/ProphecyMockingStrategyTest.php index b9c0af5..7cd7eaa 100644 --- a/tests/Strategy/ProphecyMockingStrategyTest.php +++ b/tests/Strategy/ProphecyMockingStrategyTest.php @@ -19,11 +19,11 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->setStrategy(new ProphecyMockingStrategy()); } - public function testGetCustomMockFailure() + public function testBuildMultipleFQCNSuccess() { - $this->expectException(MockNotCreatedException::class); + $this->strategy->build('foo, bar'); - $this->strategy->get(new ObjectProphecy(new LazyDouble(new Doubler()))); + $this->assertTrue(true); } public function testCallMissingMethodFailure() @@ -33,4 +33,34 @@ public function testCallMissingMethodFailure() $this->expectException(\Throwable::class); $this->strategy->get($mock)->getSelf(); } + + public function testGetCustomMockFailure() + { + $this->expectException(MockNotCreatedException::class); + + $this->strategy->get(new ObjectProphecy(new LazyDouble(new Doubler()))); + } + + public function testGetFakeFQCNFailure() + { + $mock = $this->strategy->build('foo'); + + $this->assertFalse(is_a($this->strategy->get($mock), 'foo')); + } + + public function testGetMultipleFQCNPartialSuccess() + { + $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + + $this->assertFalse(is_a($this->strategy->get($mock), TestClass::class)); + $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + } + + public function testGetMultipleFakeFQCNFailure() + { + $mock = $this->strategy->build('foo, bar'); + + $this->assertFalse(is_a($this->strategy->get($mock), 'foo')); + $this->assertFalse(is_a($this->strategy->get($mock), 'bar')); + } } From 1e368907d5833e238c63b05d401bfa7827dcf5e9 Mon Sep 17 00:00:00 2001 From: Alberto Villa Date: Mon, 19 Jun 2017 19:17:58 +0200 Subject: [PATCH 4/6] Test abstracts and interfaces --- tests/AbstractTestClass.php | 26 +++++++++++++++++++ tests/Strategy/MockeryMockingStrategyTest.php | 13 ++++++++-- tests/Strategy/MockingStrategyTestCase.php | 23 +++++++++++++--- tests/Strategy/PHPUnitMockingStrategyTest.php | 3 ++- .../Strategy/ProphecyMockingStrategyTest.php | 4 +-- tests/TestClass.php | 20 +------------- tests/TestInterface.php | 15 +++++++++++ 7 files changed, 77 insertions(+), 27 deletions(-) create mode 100644 tests/AbstractTestClass.php create mode 100644 tests/TestInterface.php diff --git a/tests/AbstractTestClass.php b/tests/AbstractTestClass.php new file mode 100644 index 0000000..77425bc --- /dev/null +++ b/tests/AbstractTestClass.php @@ -0,0 +1,26 @@ +strategy->build(TestClass::class . ', ' . \stdClass::class); + $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); - $this->assertTrue(true); + $this->assertInstanceOf($this->strategy->getMockType(), $mock); } public function testBuildMultipleFakeFQCNFailure() @@ -59,6 +60,14 @@ public function testGetFakeFQCNSuccess() $this->assertTrue(is_a($this->strategy->get($mock), 'foo')); } + public function testGetMultipleClassInterfaceSuccess() + { + $mock = $this->strategy->build(\stdClass::class . ', ' . TestInterface::class); + + $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + $this->assertTrue(is_a($this->strategy->get($mock), TestInterface::class)); + } + public function testGetMultipleFQCNPartialSuccess() { $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); diff --git a/tests/Strategy/MockingStrategyTestCase.php b/tests/Strategy/MockingStrategyTestCase.php index f51efe0..b77b652 100644 --- a/tests/Strategy/MockingStrategyTestCase.php +++ b/tests/Strategy/MockingStrategyTestCase.php @@ -8,7 +8,9 @@ use Moka\Strategy\MockingStrategyInterface; use Moka\Stub\StubSet; use PHPUnit\Framework\TestCase; +use Tests\AbstractTestClass; use Tests\TestClass; +use Tests\TestInterface; abstract class MockingStrategyTestCase extends TestCase { @@ -26,30 +28,45 @@ public function setUp() { // Mocking a StubSet is way too difficult. $this->stubs = StubFactory::fromArray([ - 'isValid' => false, + 'isTrue' => false, 'getInt' => 3, 'throwException' => new \Exception() ]); } - public function testBuildSuccess() + public function testBuildClassSuccess() { $mock = $this->strategy->build(TestClass::class); $this->assertInstanceOf($this->strategy->getMockType(), $mock); } + public function testBuildInterfaceSuccess() + { + $mock = $this->strategy->build(TestInterface::class); + + $this->assertInstanceOf($this->strategy->getMockType(), $mock); + } + + public function testBuildAbstractClassSuccess() + { + $mock = $this->strategy->build(AbstractTestClass::class); + + $this->assertInstanceOf($this->strategy->getMockType(), $mock); + } + public function testBuildFakeFQCNSuccess() { $this->strategy->build('foo'); $this->assertTrue(true); } + public function testDecorateSingleCallSuccess() { $mock = $this->strategy->build(TestClass::class); $this->strategy->decorate($mock, $this->stubs); - $this->assertSame(false, $this->strategy->get($mock)->isValid()); + $this->assertSame(false, $this->strategy->get($mock)->isTrue()); $this->expectException(\Exception::class); $this->strategy->get($mock)->throwException(); diff --git a/tests/Strategy/PHPUnitMockingStrategyTest.php b/tests/Strategy/PHPUnitMockingStrategyTest.php index 1f73357..586015b 100644 --- a/tests/Strategy/PHPUnitMockingStrategyTest.php +++ b/tests/Strategy/PHPUnitMockingStrategyTest.php @@ -6,6 +6,7 @@ use Moka\Exception\MockNotCreatedException; use Moka\Strategy\PHPUnitMockingStrategy; use Tests\TestClass; +use Tests\TestInterface; class PHPUnitMockingStrategyTest extends MockingStrategyTestCase { @@ -41,7 +42,7 @@ public function testCallMissingMethodSuccess() { $mock = $this->strategy->build(TestClass::class); - $this->assertInstanceOf(TestClass::class, $this->strategy->get($mock)->getSelf()); + $this->assertInstanceOf(TestInterface::class, $this->strategy->get($mock)->getSelf()); } public function testGetFakeFQCNSuccess() diff --git a/tests/Strategy/ProphecyMockingStrategyTest.php b/tests/Strategy/ProphecyMockingStrategyTest.php index 7cd7eaa..f4630d8 100644 --- a/tests/Strategy/ProphecyMockingStrategyTest.php +++ b/tests/Strategy/ProphecyMockingStrategyTest.php @@ -21,9 +21,9 @@ public function __construct($name = null, array $data = [], $dataName = '') public function testBuildMultipleFQCNSuccess() { - $this->strategy->build('foo, bar'); + $mock = $this->strategy->build('foo, bar'); - $this->assertTrue(true); + $this->assertInstanceOf($this->strategy->getMockType(), $mock); } public function testCallMissingMethodFailure() diff --git a/tests/TestClass.php b/tests/TestClass.php index 62fda06..d43c7b6 100644 --- a/tests/TestClass.php +++ b/tests/TestClass.php @@ -3,24 +3,6 @@ namespace Tests; -class TestClass +class TestClass extends AbstractTestClass { - public function isValid(): bool - { - return true; - } - - public function getInt(): int - { - return 1; - } - - public function throwException() - { - } - - public function getSelf(): self - { - return $this; - } } diff --git a/tests/TestInterface.php b/tests/TestInterface.php new file mode 100644 index 0000000..2e6186c --- /dev/null +++ b/tests/TestInterface.php @@ -0,0 +1,15 @@ + Date: Mon, 19 Jun 2017 19:19:52 +0200 Subject: [PATCH 5/6] Fix namespace --- tests/Stub/StubSetTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Stub/StubSetTest.php b/tests/Stub/StubSetTest.php index 6c021af..5ee1c85 100644 --- a/tests/Stub/StubSetTest.php +++ b/tests/Stub/StubSetTest.php @@ -1,7 +1,7 @@ Date: Tue, 20 Jun 2017 08:38:16 +0200 Subject: [PATCH 6/6] Randomize class names to prevent side effects Meanwhile, avoid relying on builtin class names such as stdClass, as they may be used internally by engines to build mocks. --- tests/{TestClass.php => BarTestClass.php} | 2 +- tests/FooTestClass.php | 8 +++++ tests/Proxy/ProxyTest.php | 8 ++--- tests/Strategy/MockeryMockingStrategyTest.php | 24 +++++++------ tests/Strategy/MockingStrategyTestCase.php | 21 ++++++----- tests/Strategy/PHPUnitMockingStrategyTest.php | 11 +++--- .../Strategy/ProphecyMockingStrategyTest.php | 35 ++++++++++++------- 7 files changed, 68 insertions(+), 41 deletions(-) rename tests/{TestClass.php => BarTestClass.php} (54%) create mode 100644 tests/FooTestClass.php diff --git a/tests/TestClass.php b/tests/BarTestClass.php similarity index 54% rename from tests/TestClass.php rename to tests/BarTestClass.php index d43c7b6..948fde5 100644 --- a/tests/TestClass.php +++ b/tests/BarTestClass.php @@ -3,6 +3,6 @@ namespace Tests; -class TestClass extends AbstractTestClass +class BarTestClass extends AbstractTestClass { } diff --git a/tests/FooTestClass.php b/tests/FooTestClass.php new file mode 100644 index 0000000..5b9e004 --- /dev/null +++ b/tests/FooTestClass.php @@ -0,0 +1,8 @@ +willReturn($mock); $this->proxy = new Proxy( - TestClass::class, + FooTestClass::class, $this->mockingStrategy ); } @@ -56,10 +56,10 @@ public function testStubAndDecorateSuccess() public function testServeSuccess() { $this->mockingStrategy->method('get')->willReturn( - $this->getMockBuilder(TestClass::class)->getMock() + $this->getMockBuilder(FooTestClass::class)->getMock() ); - $this->assertInstanceOf(TestClass::class, $this->proxy->serve()); + $this->assertInstanceOf(FooTestClass::class, $this->proxy->serve()); } public function testServeFailure() diff --git a/tests/Strategy/MockeryMockingStrategyTest.php b/tests/Strategy/MockeryMockingStrategyTest.php index bc0dc99..feebf9d 100644 --- a/tests/Strategy/MockeryMockingStrategyTest.php +++ b/tests/Strategy/MockeryMockingStrategyTest.php @@ -5,7 +5,8 @@ use Moka\Exception\MockNotCreatedException; use Moka\Strategy\MockeryMockingStrategy; -use Tests\TestClass; +use Tests\BarTestClass; +use Tests\FooTestClass; use Tests\TestInterface; class MockeryMockingStrategyTest extends MockingStrategyTestCase @@ -33,7 +34,7 @@ public function testBuildParseFailure() public function testBuildMultipleFQCNSuccess() { - $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + $mock = $this->strategy->build(FooTestClass::class . ', ' . BarTestClass::class); $this->assertInstanceOf($this->strategy->getMockType(), $mock); } @@ -42,12 +43,12 @@ public function testBuildMultipleFakeFQCNFailure() { $this->expectException(MockNotCreatedException::class); - $this->strategy->build('foo, bar'); + $this->strategy->build($this->getRandomFQCN() . ', ' . $this->getRandomFQCN()); } public function testCallMissingMethodFailure() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->expectException(\Throwable::class); $this->strategy->get($mock)->getSelf(); @@ -55,24 +56,25 @@ public function testCallMissingMethodFailure() public function testGetFakeFQCNSuccess() { - $mock = $this->strategy->build('foo'); + $fqcn = $this->getRandomFQCN(); + $mock = $this->strategy->build($fqcn); - $this->assertTrue(is_a($this->strategy->get($mock), 'foo')); + $this->assertTrue(is_a($this->strategy->get($mock), $fqcn)); } public function testGetMultipleClassInterfaceSuccess() { - $mock = $this->strategy->build(\stdClass::class . ', ' . TestInterface::class); + $mock = $this->strategy->build(FooTestClass::class . ', ' . TestInterface::class); - $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + $this->assertTrue(is_a($this->strategy->get($mock), FooTestClass::class)); $this->assertTrue(is_a($this->strategy->get($mock), TestInterface::class)); } public function testGetMultipleFQCNPartialSuccess() { - $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + $mock = $this->strategy->build(FooTestClass::class . ', ' . BarTestClass::class); - $this->assertFalse(is_a($this->strategy->get($mock), TestClass::class)); - $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + $this->assertFalse(is_a($this->strategy->get($mock), FooTestClass::class)); + $this->assertTrue(is_a($this->strategy->get($mock), BarTestClass::class)); } } diff --git a/tests/Strategy/MockingStrategyTestCase.php b/tests/Strategy/MockingStrategyTestCase.php index b77b652..6888b68 100644 --- a/tests/Strategy/MockingStrategyTestCase.php +++ b/tests/Strategy/MockingStrategyTestCase.php @@ -9,7 +9,7 @@ use Moka\Stub\StubSet; use PHPUnit\Framework\TestCase; use Tests\AbstractTestClass; -use Tests\TestClass; +use Tests\FooTestClass; use Tests\TestInterface; abstract class MockingStrategyTestCase extends TestCase @@ -36,7 +36,7 @@ public function setUp() public function testBuildClassSuccess() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->assertInstanceOf($this->strategy->getMockType(), $mock); } @@ -57,14 +57,14 @@ public function testBuildAbstractClassSuccess() public function testBuildFakeFQCNSuccess() { - $this->strategy->build('foo'); + $mock = $this->strategy->build($this->getRandomFQCN()); - $this->assertTrue(true); + $this->assertInstanceOf($this->strategy->getMockType(), $mock); } public function testDecorateSingleCallSuccess() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->strategy->decorate($mock, $this->stubs); $this->assertSame(false, $this->strategy->get($mock)->isTrue()); @@ -74,7 +74,7 @@ public function testDecorateSingleCallSuccess() public function testDecorateMultipleCallsSuccess() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->strategy->decorate($mock, $this->stubs); $this->assertSame(3, $this->strategy->get($mock)->getInt()); $this->assertSame(3, $this->strategy->get($mock)->getInt()); @@ -89,9 +89,9 @@ public function testDecorateFailure() public function testGetSuccess() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); - $this->assertInstanceOf(TestClass::class, $this->strategy->get($mock)); + $this->assertInstanceOf(FooTestClass::class, $this->strategy->get($mock)); } public function testGetFailure() @@ -110,4 +110,9 @@ protected function setStrategy(MockingStrategyInterface $strategy) { $this->strategy = $strategy; } + + protected function getRandomFQCN() + { + return 'foo_' . rand(); + } } diff --git a/tests/Strategy/PHPUnitMockingStrategyTest.php b/tests/Strategy/PHPUnitMockingStrategyTest.php index 586015b..5bc5248 100644 --- a/tests/Strategy/PHPUnitMockingStrategyTest.php +++ b/tests/Strategy/PHPUnitMockingStrategyTest.php @@ -5,7 +5,7 @@ use Moka\Exception\MockNotCreatedException; use Moka\Strategy\PHPUnitMockingStrategy; -use Tests\TestClass; +use Tests\FooTestClass; use Tests\TestInterface; class PHPUnitMockingStrategyTest extends MockingStrategyTestCase @@ -35,20 +35,21 @@ public function testBuildMultipleFQCNFailure() { $this->expectException(MockNotCreatedException::class); - $this->strategy->build('foo, bar'); + $this->strategy->build($this->getRandomFQCN() . ', ' . $this->getRandomFQCN()); } public function testCallMissingMethodSuccess() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->assertInstanceOf(TestInterface::class, $this->strategy->get($mock)->getSelf()); } public function testGetFakeFQCNSuccess() { - $mock = $this->strategy->build('foo'); + $fqcn = $this->getRandomFQCN(); + $mock = $this->strategy->build($fqcn); - $this->assertTrue(is_a($this->strategy->get($mock), 'foo')); + $this->assertTrue(is_a($this->strategy->get($mock), $fqcn)); } } diff --git a/tests/Strategy/ProphecyMockingStrategyTest.php b/tests/Strategy/ProphecyMockingStrategyTest.php index f4630d8..b63ff1b 100644 --- a/tests/Strategy/ProphecyMockingStrategyTest.php +++ b/tests/Strategy/ProphecyMockingStrategyTest.php @@ -8,7 +8,8 @@ use Prophecy\Doubler\Doubler; use Prophecy\Doubler\LazyDouble; use Prophecy\Prophecy\ObjectProphecy; -use Tests\TestClass; +use Tests\FooTestClass; +use Tests\TestInterface; class ProphecyMockingStrategyTest extends MockingStrategyTestCase { @@ -19,16 +20,23 @@ public function __construct($name = null, array $data = [], $dataName = '') $this->setStrategy(new ProphecyMockingStrategy()); } + public function testBuildEmptyFQCNSuccess() + { + $mock = $this->strategy->build(''); + + $this->assertInstanceOf($this->strategy->getMockType(), $mock); + } + public function testBuildMultipleFQCNSuccess() { - $mock = $this->strategy->build('foo, bar'); + $mock = $this->strategy->build($this->getRandomFQCN() . ', ' . $this->getRandomFQCN()); $this->assertInstanceOf($this->strategy->getMockType(), $mock); } public function testCallMissingMethodFailure() { - $mock = $this->strategy->build(TestClass::class); + $mock = $this->strategy->build(FooTestClass::class); $this->expectException(\Throwable::class); $this->strategy->get($mock)->getSelf(); @@ -43,24 +51,27 @@ public function testGetCustomMockFailure() public function testGetFakeFQCNFailure() { - $mock = $this->strategy->build('foo'); + $fqcn = $this->getRandomFQCN(); + $mock = $this->strategy->build($fqcn); - $this->assertFalse(is_a($this->strategy->get($mock), 'foo')); + $this->assertFalse(is_a($this->strategy->get($mock), $fqcn)); } - public function testGetMultipleFQCNPartialSuccess() + public function testGetMultipleClassInterfaceFailure() { - $mock = $this->strategy->build(TestClass::class . ', ' . \stdClass::class); + $mock = $this->strategy->build(FooTestClass::class . ', ' . TestInterface::class); - $this->assertFalse(is_a($this->strategy->get($mock), TestClass::class)); - $this->assertTrue(is_a($this->strategy->get($mock), \stdClass::class)); + $this->assertFalse(is_a($this->strategy->get($mock), FooTestClass::class)); + $this->assertFalse(is_a($this->strategy->get($mock), TestInterface::class)); } public function testGetMultipleFakeFQCNFailure() { - $mock = $this->strategy->build('foo, bar'); + $fqcn1 = $this->getRandomFQCN(); + $fqcn2 = $this->getRandomFQCN(); + $mock = $this->strategy->build($fqcn1 . ', ' . $fqcn2); - $this->assertFalse(is_a($this->strategy->get($mock), 'foo')); - $this->assertFalse(is_a($this->strategy->get($mock), 'bar')); + $this->assertFalse(is_a($this->strategy->get($mock), $fqcn1)); + $this->assertFalse(is_a($this->strategy->get($mock), $fqcn2)); } }