-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAutoMockingTest.php
More file actions
65 lines (57 loc) · 2.15 KB
/
Copy pathAutoMockingTest.php
File metadata and controls
65 lines (57 loc) · 2.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace Bigcommerce\MockInjector;
use PHPUnit\Framework\TestCase;
use Prophecy\PhpUnit\ProphecyTrait;
use Prophecy\Prophecy\MethodProphecy;
use Prophecy\Prophet;
/**
* Configured PHPUnit TestCase providing auto-mocking of dependency injection components using the
* BigCommerce Injector.
* Either extend it, or copy its setUp/tearDown methods to your own test cases. "MockInjector" is self contained.
* @package Bigcommerce\MockInjector
*/
abstract class AutoMockingTest extends TestCase
{
use ProphecyTrait;
/** @var MockInjector */
protected $injector;
/** @var Prophet */
private $mockingContainerProphet;
/**
* Sets up the fixture, for example, open a network connection.
* This method is called before a test is executed.
*/
protected function setUp(): void
{
parent::setUp();
$this->mockingContainerProphet = new Prophet();
$this->injector = new MockInjector(new ProphecyMockingContainer($this->mockingContainerProphet));
}
/**
* Create an instance of $className and automatically mock all its constructor dependencies.
* @param string $className The FQCN of the class we are creating
* @param array $parameters Any parameters to pass to the constructor. Can be keyed by type, name or position.
* @return object
*/
protected function createWithMocks($className, $parameters = [])
{
return $this->injector->create($className, $parameters);
}
/**
* Tears down the fixture, for example, close a network connection.
* This method is called after a test is executed.
*/
protected function tearDown(): void
{
parent::tearDown();
$this->injector->checkPredictions();
foreach ($this->mockingContainerProphet->getProphecies() as $objectProphecy) {
foreach ($objectProphecy->getMethodProphecies() as $methodProphecies) {
/** @var MethodProphecy[] $methodProphecies */
foreach ($methodProphecies as $methodProphecy) {
$this->addToAssertionCount(count($methodProphecy->getCheckedPredictions()));
}
}
}
}
}