Completed
Pull Request — dev (#27)
by
unknown
03:07
created

AbstractController::__destruct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
namespace Tests\AppBundle\Controller;
4
5
use Doctrine\ORM\EntityManager;
6
use Symfony\Bundle\FrameworkBundle\Client;
7
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
8
use Symfony\Component\DependencyInjection\ContainerInterface;
9
10
abstract class AbstractController extends WebTestCase
11
{
12
    protected static $options = [
13
        'environment' => 'test',
14
        'debug' => true,
15
    ];
16
    /**
17
     * @var ContainerInterface
18
     */
19
    protected $container;
20
    /**
21
     * @var EntityManager
22
     */
23
    protected $em;
24
    /**
25
     * @var Client
26
     */
27
    protected $client;
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function __construct($name = null, array $data = [], $dataName = '')
32
    {
33
        exec('./bin/console doctrine:database:create --env=test');
34
        exec('./bin/console doctrine:schema:create --env=test');
35
        exec('./bin/console hautelook:fixtures:load -n --env=test');
36
37
        self::bootKernel(self::$options);
38
39
        $this->container = static::$kernel->getContainer();
40
41
        $this->em = static::$kernel->getContainer()->get('doctrine')->getManager();
42
        parent::__construct($name, $data, $dataName);
43
    }
44
45
    public function __destruct()
46
    {
47
        exec('./bin/console doctrine:database:drop --force --env=test');
48
    }
49
50
    /**
51
     * @param string $path
52
     * @param string $method
53
     * @param int $expectedStatusCode
54
     *
55
     * @return \Symfony\Component\DomCrawler\Crawler
56
     */
57
    protected function request($path, $method = 'GET', $expectedStatusCode = 200)
58
    {
59
        $client = $this->getClient();
60
        $crawler = $client->request($method, $path);
61
        $this->assertEquals(
62
            $expectedStatusCode,
63
            $client->getResponse()->getStatusCode(),
64
            sprintf(
65
                'We expected that uri "%s" will return %s status code, but had received %d',
66
                $path,
67
                $expectedStatusCode,
68
                $client->getResponse()->getStatusCode()
69
            )
70
        );
71
        return $crawler;
72
    }
73
74
    /**
75
     * @param array $options
76
     * @param array $server
77
     * @return Client
78
     */
79
    protected function getClient(array $options = array(), array $server = array())
80
    {
81
        if (!$this->client) {
82
            $this->client = static::createClient($options, $server);
83
        }
84
        return $this->client;
85
    }
86
87
    protected function logIn($username = '[email protected]', $password = 'admin')
88
    {
89
        $client = $this->getClient();
90
        $crawler = $client->request('GET', '/login');
91
        $form = $crawler->selectButton('Login')->form();
92
        $client->submit($form, ['_username' => $username, '_password' => $password]);
93
        return $client;
94
    }
95
}
96