Skip to content

Commit fd44689

Browse files
Merge pull request #4 from precariouspanther/pre-create-set-mock
Add ability to set prophecies to the MockInjector container that the …
2 parents c5e680a + 79d63cf commit fd44689

2 files changed

Lines changed: 24 additions & 8 deletions

File tree

src/MockingContainerInterface.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,12 @@ public function getMock($mockClassName);
2626
* @return ObjectProphecy[]
2727
*/
2828
public function getAllMocks();
29+
30+
/**
31+
* Set a prophecy to be used by the injector rather than creating on demand.
32+
* @param string $mockClassName
33+
* @param ObjectProphecy $mock
34+
* @return void
35+
*/
36+
public function setMock(string $mockClassName, ObjectProphecy $mock);
2937
}

src/ProphecyMockingContainer.php

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace Bigcommerce\MockInjector;
34

45
use Prophecy\Exception\Prediction\AggregateException;
@@ -11,6 +12,7 @@ class ProphecyMockingContainer implements MockingContainerInterface
1112
* @var Prophet
1213
*/
1314
private $prophet;
15+
1416
/**
1517
* Collection of mocks we've auto-created keyed by their FQCN
1618
* @var ObjectProphecy[]
@@ -43,7 +45,7 @@ public function checkPredictions()
4345
*/
4446
public function get($id)
4547
{
46-
return $this->createOrGetMock($id)->reveal();
48+
return $this->createOrGetMock($id)->reveal();
4749
}
4850

4951
/**
@@ -59,19 +61,25 @@ public function has($id)
5961
/**
6062
* Fetch one of the mocks that was auto-created by the MockInjector to construct objects used in the current test,
6163
* so that you can set expectations or configure mock methods.
64+
* Alternatively, will create and return a mock which the injector will use (useful for setting expectations
65+
* on constructor behaviours prior to calling injector create)
6266
* @param string $mockClassName FQCN of the dependency we mocked
6367
* @return ObjectProphecy
64-
* @throws \InvalidArgumentException
6568
*/
6669
public function getMock($mockClassName)
6770
{
68-
if (!isset($this->mocks[$mockClassName])) {
69-
throw new \InvalidArgumentException(
70-
"The MockInjector did not create a '$mockClassName' mock so it can not be retrieved."
71-
);
72-
}
71+
return $this->createOrGetMock($mockClassName);
72+
}
7373

74-
return $this->mocks[$mockClassName];
74+
/**
75+
* Set a prophecy to be used by the injector rather than creating on demand.
76+
* @param string $mockClassName
77+
* @param ObjectProphecy $mock
78+
* @return void
79+
*/
80+
public function setMock(string $mockClassName, ObjectProphecy $mock)
81+
{
82+
$this->mocks[$mockClassName] = $mock;
7583
}
7684

7785
/**

0 commit comments

Comments
 (0)