AbstractContainerAwareTestCase::getTestDirectory()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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