Skip to content

Commit

Permalink
Merge pull request #24 from facile-it/feature/unique-proxy-mock
Browse files Browse the repository at this point in the history
Use a mt_rand to concat fqcn when alias is empty
  • Loading branch information
Giuffre authored Jun 26, 2017
2 parents 98ca466 + d90c931 commit 5fa1cd4
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
26 changes: 13 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,50 +90,50 @@ 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
```

## <a name='reference'></a>Reference

### `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([
Expand Down
8 changes: 7 additions & 1 deletion src/Moka/Builder/ProxyBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
22 changes: 21 additions & 1 deletion tests/Builder/ProxyBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function testGetProxySuccess()

$this->assertInstanceOf(Proxy::class, $proxy1);

$this->assertSame($proxy1, $proxy2);
$this->assertNotSame($proxy1, $proxy2);
}

public function testGetProxySuccessWithAlias()
Expand All @@ -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'));
}
}

0 comments on commit 5fa1cd4

Please sign in to comment.