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

AbstractContainerAwareTestCase   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 4
lcom 1
cbo 3
dl 0
loc 39
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getServiceByType() 0 5 1
A getContainer() 0 12 2
A getTestDirectory() 0 5 1
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