ContainerAwareTestCase::getSession()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Dealer\Test\PhpUnit\Base;
4
5
use Symfony\Component\DependencyInjection\Container;
6
use Symfony\Component\DependencyInjection\ContainerBuilder;
7
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
8
use Thelia\Core\HttpFoundation\Request;
9
use Thelia\Core\HttpFoundation\Session\Session;
10
use Thelia\Core\Security\SecurityContext;
11
use Thelia\Core\Translation\Translator;
12
use Thelia\Log\Tlog;
13
14
/**
15
 * Class ContainerAwareTestCase
16
 * @package Dealer\Test
17
 */
18
abstract class ContainerAwareTestCase extends \PHPUnit_Framework_TestCase
19
{
20
    protected $import;
21
22
    /** @var ContainerBuilder */
23
    protected $container;
24
25
    /** @var  Session */
26
    protected $session;
27
28
    public function __construct()
29
    {
30
        parent::__construct();
31
32
        Tlog::getNewInstance();
33
34
        $this->session = $this->getSession();
35
        $this->getContainer();
36
37
        $this->initializeRequest();
38
    }
39
40
    public function getContainer()
41
    {
42
        if (null == $this->container) {
43
            $this->container = new \Symfony\Component\DependencyInjection\ContainerBuilder();
44
        }
45
46
        $this->container->set("thelia.translator", new Translator(new Container()));
47
48
        $this->initializeRequest();
49
50
51
        $dispatcher = $this->getMock("Symfony\Component\EventDispatcher\EventDispatcherInterface");
52
        $this->container->set("event_dispatcher", $dispatcher);
53
54
        $this->buildContainer($this->container);
55
    }
56
57
    public function initializeRequest($attributes = [], $query = [], $request = [])
58
    {
59
        $request = new Request(
60
            $query,
61
            $request,
62
            $attributes
63
        );
64
        $request->setSession($this->getSession());
65
        $this->container->set("request", $request);
66
        $this->container->set("thelia.securitycontext", new SecurityContext($request));
67
    }
68
69
    public function getSession()
70
    {
71
        return new Session(new MockArraySessionStorage());
72
    }
73
74
    /**
75
     * Use this method to build the container with the services that you need.
76
     */
77
    abstract protected function buildContainer(ContainerBuilder $container);
78
}