diff --git a/README.md b/README.md index 52139e5..742c5d5 100644 --- a/README.md +++ b/README.md @@ -90,20 +90,20 @@ class FooTest extends TestCase You can rely on the original mock object implementation to be accessible (in the example below, PHPUnit's): ```php -$this->mock(BarInterface::class) +$this->mock(BarInterface::class, 'bar') ->expects($this->at(0)) ->method('isValid') ->willReturn(true); -$this->mock(BarInterface::class) +$this->mock('bar') ->expects($this->at(1)) ->method('isValid') ->willThrowException(new \Exception()); -var_dump($this->mock(BarInterface::class)->serve()->isValid()); +var_dump($this->mock('bar')->serve()->isValid()); // bool(true) -var_dump($this->mock(BarInterface::class)->serve()->isValid()); +var_dump($this->mock('bar')->serve()->isValid()); // throws \Exception ``` @@ -111,29 +111,29 @@ var_dump($this->mock(BarInterface::class)->serve()->isValid()); ### `mock(string $fqcn, string $alias = null): Proxy` -Creates (if not existing already) a proxy containing a mock object according to selected strategy for the class identified by `$fqcn` and optionally assigns an `$alias` to it. +Creates a proxy containing a mock object (according to the selected strategy) for the class identified by `$fqcn` and optionally assigns an `$alias` to it to be able to get it later: ```php $mock1 = $this->mock(FooInterface::class)->serve(); // Creates the mock for FooInterface. -$mock2 = $this->mock(FooInterface::class)->serve(); // Gets the mock previously created. +$mock2 = $this->mock(FooInterface::class)->serve(); // Gets a different mock. var_dump($mock1 === $mock2); -// bool(true) +// bool(false) ``` -The `$alias` allows you to create different instances of the same `$fqcn`; you will refer to them by the `$alias` from now on. +The `$alias` allows you to store mock instances: ```php -$this->mock(FooInterface::class, 'foo1')->serve(); // Creates a mock for FooInterface. -$this->mock(FooInterface::class, 'foo2')->serve(); // Gets a different mock. +$mock1 = $this->mock(FooInterface::class, 'foo')->serve(); // Creates a mock for FooInterface. +$mock2 = $this->mock('foo')->serve(); // Get the mock previously created. -var_dump($this->mock('foo1') === $this->mock('foo2')); -// bool(false) +var_dump($mock1 === $mock2); +// bool(true) ``` ### `stub(array $methodsWithValues): self` -Accepts an array of method stubs with format `[$methodName => $methodValue]`, where `$methodName` **must** be a string and `$methodValue` can be of any type, including another mock object or an exception instance. +Accepts an array of method stubs with format `[$methodName => $methodValue]`, where `$methodName` **must** be a string and `$methodValue` can be of any type, including another mock object or an exception instance: ```php $actualMock = $this->mock(BarInterface::class)->stub([ diff --git a/src/Moka/Builder/ProxyBuilder.php b/src/Moka/Builder/ProxyBuilder.php index 79021e1..ab2e9c1 100644 --- a/src/Moka/Builder/ProxyBuilder.php +++ b/src/Moka/Builder/ProxyBuilder.php @@ -54,7 +54,13 @@ public function reset() */ public function getProxy(string $fqcn, string $alias = null): Proxy { - $alias = $alias ?: $fqcn; + if ($this->container->has($fqcn)) { + return $this->container->get($fqcn); + } + + if (null === $alias) { + return $this->buildProxy($fqcn); + } if (!$this->container->has($alias)) { $this->container->set($alias, $this->buildProxy($fqcn)); diff --git a/tests/Builder/ProxyBuilderTest.php b/tests/Builder/ProxyBuilderTest.php index 3259401..28312b6 100644 --- a/tests/Builder/ProxyBuilderTest.php +++ b/tests/Builder/ProxyBuilderTest.php @@ -43,7 +43,7 @@ public function testGetProxySuccess() $this->assertInstanceOf(Proxy::class, $proxy1); - $this->assertSame($proxy1, $proxy2); + $this->assertNotSame($proxy1, $proxy2); } public function testGetProxySuccessWithAlias() @@ -56,4 +56,24 @@ public function testGetProxySuccessWithAlias() $this->assertNotSame($proxy1, $proxy2); } + + public function testGetProxySuccessWithSameAlias() + { + $proxy1 = $this->proxyBuilder->getProxy(\stdClass::class, 'foo'); + $proxy2 = $this->proxyBuilder->getProxy(\stdClass::class, 'foo'); + + $this->assertInstanceOf(Proxy::class, $proxy1); + $this->assertInstanceOf(Proxy::class, $proxy2); + + $this->assertSame($proxy1, $proxy2); + } + + public function testGetProxySuccessWithSameAliasOnly() + { + $proxy = $this->proxyBuilder->getProxy(\stdClass::class, 'bar'); + + $this->assertInstanceOf(Proxy::class, $proxy); + + $this->assertSame($proxy, $this->proxyBuilder->getProxy('bar')); + } }