Passed
Pull Request — master (#11)
by Victor
01:49
created

IntegrationTestCase.php$0 ➔ createContainer()   A

Complexity

Conditions 2

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
rs 9.52
c 0
b 0
f 0
cc 2

3 Methods

Rating   Name   Duplication   Size   Complexity  
A IntegrationTestCase.php$1 ➔ __construct() 0 3 1
A IntegrationTestCase.php$0 ➔ get() 0 9 2
A IntegrationTestCase.php$0 ➔ has() 0 3 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Shoot\Shoot\Tests\Integration;
5
6
use Exception;
7
use LogicException;
8
use PHPUnit\Framework\MockObject\MockObject;
9
use PHPUnit\Framework\TestCase;
10
use Psr\Container\ContainerInterface;
11
use Psr\Container\NotFoundExceptionInterface;
12
use Psr\Http\Message\ServerRequestInterface;
13
use Shoot\Shoot\Installer;
14
use Shoot\Shoot\Middleware\PresenterMiddleware;
15
use Shoot\Shoot\Pipeline;
16
use Twig_Environment as Environment;
17
use Twig_Loader_Filesystem as FilesystemLoader;
18
19
abstract class IntegrationTestCase extends TestCase
20
{
21
    /** @var mixed */
22
    private $container = [];
23
24
    /** @var Pipeline */
25
    protected $pipeline;
26
27
    /** @var ServerRequestInterface|MockObject */
28
    protected $request;
29
30
    /** @var string */
31
    protected $templateDirectory = '';
32
33
    /** @var Environment */
34
    private $twig;
35
36
    /**
37
     * @return void
38
     */
39
    protected function setUp()
40
    {
41
        if (empty($this->templateDirectory)) {
42
            throw new LogicException('Template directory is not set in ' . static::class);
43
        }
44
45
        $container = $this->createContainer();
46
        $pipeline = new Pipeline([new PresenterMiddleware($container)]);
47
        $installer = new Installer($pipeline);
48
49
        $loader = new FilesystemLoader([realpath($this->templateDirectory)]);
50
        $twig = new Environment($loader, ['cache' => false, 'strict_variables' => true]);
51
        $twig = $installer->install($twig);
52
53
        $this->pipeline = $pipeline;
54
        $this->request = $this->createMock(ServerRequestInterface::class);
55
        $this->twig = $twig;
56
57
        parent::setUp();
58
    }
59
60
    /**
61
     * @param string  $template
62
     * @param mixed[] $variables
63
     *
64
     * @return string[]
65
     */
66
    final protected function renderTemplate(string $template, array $variables = []): array
67
    {
68
        return $this->pipeline->withRequest($this->request, function () use ($template, $variables): array {
69
            $output = $this->twig->render($template, $variables);
70
            $output = trim($output);
71
            $output = explode(PHP_EOL, $output);
72
            $output = array_map('trim', $output);
73
74
            return $output;
75
        });
76
    }
77
78
    /**
79
     * @param string $id
80
     * @param object $service
81
     *
82
     * @return void
83
     */
84
    final protected function addToContainer(string $id, $service)
85
    {
86
        if (isset($this->container[$id])) {
87
            throw new LogicException("Service with ID '{$id}' already exists in container");
88
        }
89
90
        $this->container[$id] = $service;
91
    }
92
93
    /**
94
     * @return ContainerInterface
95
     */
96
    private function createContainer(): ContainerInterface
97
    {
98
        return new class($this->container) implements ContainerInterface
99
        {
100
            private $container;
101
102
            public function __construct(array $container)
103
            {
104
                $this->container = $container;
105
            }
106
107
            public function get($id)
108
            {
109
                if (!$this->has($id)) {
110
                    throw new class extends Exception implements NotFoundExceptionInterface
111
                    {
112
                    };
113
                }
114
115
                return $this->container[$id] ?? new $id();
116
            }
117
118
            public function has($id): bool
119
            {
120
                return isset($this->container[$id]) || class_exists($id);
121
            }
122
        };
123
    }
124
}
125