Skip to content

Commit a1d0cf1

Browse files
authored
Merge pull request #1 from WeareJH/retrieve-child-deps
Allow to retrieve child mocks after creating an object
2 parents 51173f8 + 965d0ea commit a1d0cf1

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

README.md

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,42 @@ class ExampleTest extends TestCase
5252

5353
```
5454

55+
Note that you can pass your own arguments to the object you want to create, in-case you want to pass real instances
56+
or your own mocks. You pass them as a key value array where the key is the object constructor's parameter name you want
57+
to override and the value is the actual value.
58+
59+
`getObject` will read the object constructor parameters and automatically create mock objects using the class type of the
60+
parameter. It will then create the object you asked for passing in all the mocks via the constructor.
61+
62+
You can also retrieve one of mocked dependencies after the object was created like so:
63+
64+
```php
65+
<?php
66+
67+
use PHPUnit\Framework\TestCase;
68+
use Magento\Framework\App\Action\Context;
69+
use Magento\Framework\App\Request\Http;
70+
use Magento\Framework\Message\ManagerInterface;
71+
use Jh\UnitTestHelpers\ObjectHelper;
72+
73+
/**
74+
* @author email@example.com
75+
*/
76+
class ExampleTest extends TestCase
77+
{
78+
use ObjectHelper;
79+
80+
81+
public function setup()
82+
{
83+
84+
$context = $this->getObject(Context::class);
85+
86+
$request = $this->retrieveChildMock(Context::class, 'request');
87+
}
88+
}
89+
```
90+
91+
Where `request` is the constructor parameter name in `Context::class` - this method will return you the object prophecy
92+
so you can create expectations on it.
93+

src/ObjectHelper.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,29 @@
77
*/
88
trait ObjectHelper
99
{
10+
private $mockRegistry = [];
11+
1012
public function getObject(string $className, array $arguments = [])
1113
{
1214
$constructArguments = $this->getConstructorArguments($className, $arguments);
1315
return new $className(...array_values($constructArguments));
1416
}
1517

18+
public function retrieveChildMock(string $className, string $parameterName)
19+
{
20+
if (!isset($this->mockRegistry[$className][$parameterName])) {
21+
throw new \RuntimeException(
22+
sprintf(
23+
'No object parameter named: "%s" was created for object: "%s"',
24+
$parameterName,
25+
$className
26+
)
27+
);
28+
}
29+
30+
return $this->mockRegistry[$className][$parameterName];
31+
}
32+
1633
private function getConstructorArguments(string $className, array $arguments = [])
1734
{
1835
$constructArguments = [];
@@ -38,16 +55,18 @@ private function getConstructorArguments(string $className, array $arguments = [
3855

3956
if ($parameter->getClass()) {
4057
$argClassName = $parameter->getClass()->getName();
41-
$object = $this->getMockObject($argClassName, $arguments);
58+
$object = $this->prophesize($argClassName);
59+
60+
//store this dep for later so we can retrieve it
61+
if (!isset($this->mockRegistry[$className])) {
62+
$this->mockRegistry[$className] = [];
63+
}
64+
65+
$this->mockRegistry[$className][$parameterName] = $object;
4266
}
4367

44-
$constructArguments[$parameterName] = null === $object ? $defaultValue : $object;
68+
$constructArguments[$parameterName] = null === $object ? $defaultValue : $object->reveal();
4569
}
4670
return $constructArguments;
4771
}
48-
49-
private function getMockObject(string $className, $arguments)
50-
{
51-
return $this->prophesize($className)->reveal();
52-
}
5372
}

0 commit comments

Comments
 (0)