Skip to content

Commit

Permalink
Merge pull request #1 from WeareJH/retrieve-child-deps
Browse files Browse the repository at this point in the history
Allow to retrieve child mocks after creating an object
  • Loading branch information
AydinHassan committed Jun 13, 2017
2 parents 51173f8 + 965d0ea commit a1d0cf1
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 7 deletions.
39 changes: 39 additions & 0 deletions README.md
Expand Up @@ -52,3 +52,42 @@ class ExampleTest extends TestCase

```

Note that you can pass your own arguments to the object you want to create, in-case you want to pass real instances
or your own mocks. You pass them as a key value array where the key is the object constructor's parameter name you want
to override and the value is the actual value.

`getObject` will read the object constructor parameters and automatically create mock objects using the class type of the
parameter. It will then create the object you asked for passing in all the mocks via the constructor.

You can also retrieve one of mocked dependencies after the object was created like so:

```php
<?php

use PHPUnit\Framework\TestCase;
use Magento\Framework\App\Action\Context;
use Magento\Framework\App\Request\Http;
use Magento\Framework\Message\ManagerInterface;
use Jh\UnitTestHelpers\ObjectHelper;

/**
* @author email@example.com
*/
class ExampleTest extends TestCase
{
use ObjectHelper;


public function setup()
{

$context = $this->getObject(Context::class);

$request = $this->retrieveChildMock(Context::class, 'request');
}
}
```

Where `request` is the constructor parameter name in `Context::class` - this method will return you the object prophecy
so you can create expectations on it.

33 changes: 26 additions & 7 deletions src/ObjectHelper.php
Expand Up @@ -7,12 +7,29 @@
*/
trait ObjectHelper
{
private $mockRegistry = [];

public function getObject(string $className, array $arguments = [])
{
$constructArguments = $this->getConstructorArguments($className, $arguments);
return new $className(...array_values($constructArguments));
}

public function retrieveChildMock(string $className, string $parameterName)
{
if (!isset($this->mockRegistry[$className][$parameterName])) {
throw new \RuntimeException(
sprintf(
'No object parameter named: "%s" was created for object: "%s"',
$parameterName,
$className
)
);
}

return $this->mockRegistry[$className][$parameterName];
}

private function getConstructorArguments(string $className, array $arguments = [])
{
$constructArguments = [];
Expand All @@ -38,16 +55,18 @@ private function getConstructorArguments(string $className, array $arguments = [

if ($parameter->getClass()) {
$argClassName = $parameter->getClass()->getName();
$object = $this->getMockObject($argClassName, $arguments);
$object = $this->prophesize($argClassName);

//store this dep for later so we can retrieve it
if (!isset($this->mockRegistry[$className])) {
$this->mockRegistry[$className] = [];
}

$this->mockRegistry[$className][$parameterName] = $object;
}

$constructArguments[$parameterName] = null === $object ? $defaultValue : $object;
$constructArguments[$parameterName] = null === $object ? $defaultValue : $object->reveal();
}
return $constructArguments;
}

private function getMockObject(string $className, $arguments)
{
return $this->prophesize($className)->reveal();
}
}

0 comments on commit a1d0cf1

Please sign in to comment.