Completed
Pull Request — dev (#6)
by Arnaud
07:28 queued 04:28
created

Client::doRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 18
rs 9.4285
cc 3
eloc 13
nc 4
nop 1
1
<?php
2
3
namespace LAG\AdminBundle\Tests;
4
5
use AppKernel;
6
use Symfony\Bundle\FrameworkBundle\Client as BaseClient;
7
use Symfony\Component\HttpFoundation\Request;
8
use Symfony\Component\HttpFoundation\Response;
9
10
class Client extends BaseClient
11
{
12
    protected static $connection;
13
14
    protected $requested = true;
15
16
    public static function initClient()
17
    {
18
        $kernel = new AppKernel('test', true);
19
        $kernel->loadClassCache();
20
        $client = new self($kernel);
21
22
        return $client;
23
    }
24
25
    /**
26
     * @see http://alexandre-salome.fr/blog/Symfony2-Isolation-Of-Tests
27
     *
28
     * @param Request $request A Request instance
29
     *
30
     * @return Response A Response instance
31
     */
32
    public function doRequest($request)
33
    {
34
        if ($this->requested) {
35
            $this->getKernel()->shutdown();
36
            $this->getKernel()->boot();
37
        }
38
        if (null === self::$connection) {
39
            self::$connection = $this->getContainer()->get('doctrine.dbal.default_connection');
40
        } else {
41
            $this->getContainer()->set('doctrine.dbal.default_connection', self::$connection);
42
        }
43
        $this->requested = true;
44
        self::$connection->beginTransaction();
45
        $response = $this->getKernel()->handle($request);
46
        self::$connection->rollback();
47
48
        return $response;
49
    }
50
}
51