-
Notifications
You must be signed in to change notification settings - Fork 6
/
UnitTestCase.php
113 lines (95 loc) · 3.56 KB
/
UnitTestCase.php
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace TestTools\TestCase;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
/**
* This base class for unit tests provides an integrated DI container
* for more productive testing. The service instances are not cached
* between tests, but the container itself is which significantly improves test
* performance (about 5 to 10 times faster).
*
* USAGE: Simply create a config.yml (optionally config.local.yml for local
* modifications) in your base test directory, for example
*
* Component/FooBarClient/Tests/config.yml
*
* NOTE: The config parameter "fixture.path" is automatically set based on the
* test class filename and path to avoid conflicts/dependencies between
* different tests and enforce a consistent naming scheme.
*
* IMPORTANT: Configuration details such as server URLs or login credentials
* must be valid for development environments only, since all service / database
* requests should be replaced by fixtures after the corresponding tests were
* running for the first time. If a test fails on Jenkins/staging/integration
* because of invalid URLs or credentials in config.yml, you must make sure that
* all code that accesses external resources is using fixtures (or mock objects)
* and that all fixtures are checked in properly.
*
* You can use TextTools\Fixture\FileFixture to easily make your
* existing classes work with file based fixtures:
*
* http://martinfowler.com/bliki/SelfInitializingFake.html
*
* Documentation of the Symfony dependency injection component:
*
* http://symfony.com/doc/current/components/dependency_injection/introduction.html
*
* @author Michael Mayer <michael@lastzero.net>
* @package TestTools
* @license MIT
*/
abstract class UnitTestCase extends TestCase
{
use FixturePathTrait;
/**
* @var array
*/
private static $containers = array();
/**
* @var ContainerBuilder
*/
private $container;
/**
* @var YamlFileLoader
*/
private $loader;
protected function setContainer(ContainerBuilder $container)
{
$this->container = $container;
}
protected function configureContainer()
{
$this->container->setParameter('fixture.path', $this->getFixturePath());
$this->container->setParameter('base.path', $this->getBasePath());
}
protected function getContainer()
{
if (isset(self::$containers[$this->getTestBasePath()])) {
// Use cached container (creating a new container from yml is too slow)
$this->setContainer(self::$containers[$this->getTestBasePath()]);
$this->container->reset();
$this->configureContainer();
} elseif (!$this->container) {
// Create new container
$this->setContainer(new ContainerBuilder());
$this->configureContainer();
$locator = new FileLocator($this->getTestBasePath());
$this->loader = new YamlFileLoader($this->container, $locator);
$this->loader->load('config.yml');
try {
$this->loader->load('config.local.yml');
} catch (\InvalidArgumentException $e) {
// No local config found
}
// Cache container
self::$containers[$this->getTestBasePath()] = $this->container;
}
return $this->container;
}
protected function get($service)
{
return $this->getContainer()->get($service);
}
}