Completed
Push — master ( 70f26a...90e34c )
by Tomáš
05:03
created

AbstractContainerAwareTestCase::getContainer()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace Zenify\ModularLatteFilters\Tests\PHPUnit;
6
7
use Nette\DI\Container;
8
use PHPUnit\Framework\TestCase;
9
use ReflectionClass;
10
use Zenify\ModularLatteFilters\Tests\ContainerFactory;
11
12
13
abstract class AbstractContainerAwareTestCase extends TestCase
14
{
15
16
	/**
17
	 * @var Container[]
18
	 */
19
	private static $containers = [];
20
21
	/**
22
	 * @return object
23
	 */
24
	protected function getServiceByType(string $class)
25
	{
26
		return $this->getContainer()
27
			->getByType($class);
28
	}
29
30
31
	private function getContainer() : Container
32
	{
33
		if (isset(self::$containers[$this->getTestDirectory()])) {
34
			return self::$containers[$this->getTestDirectory()];
35
		}
36
37
		$container = (new ContainerFactory)->createWithConfig($this->getTestDirectory() . '/config/config.neon');
38
39
		self::$containers[$this->getTestDirectory()] = $container;
40
41
		return $container;
42
	}
43
44
45
	private function getTestDirectory() : string
46
	{
47
		$testFilename = (new ReflectionClass($this))->getFileName();
48
		return dirname($testFilename);
49
	}
50
51
}
52