TicketControllerTest   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 149
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 12
eloc 58
dl 0
loc 149
rs 10
c 0
b 0
f 0

9 Methods

Rating   Name   Duplication   Size   Complexity  
A testEnTicketHash() 0 3 1
A testUkTicketHash() 0 3 1
A tearDown() 0 3 1
A testEnCookieLocale() 0 3 1
A loginUser() 0 28 2
A getLangCookie() 0 10 2
A getFileHash() 0 11 2
A testUkCookieLocale() 0 3 1
A setUp() 0 23 1
1
<?php
2
3
namespace Application\Bundle\DefaultBundle\Tests;
4
5
use Application\Bundle\UserBundle\Entity\User;
6
use Doctrine\Common\DataFixtures\Purger\ORMPurger;
7
use Liip\FunctionalTestBundle\Test\WebTestCase;
8
use Symfony\Component\BrowserKit\Client;
9
use Doctrine\ORM\EntityManager;
10
11
/**
12
 * Class TicketControllerTest.
13
 */
14
class TicketControllerTest extends WebTestCase
15
{
16
    const EN_FILE_HASH = 'bb93bfbc18dfa0a8956b6aee7a9466f6';
17
    const UK_FILE_HASH = '7a4916c2839dc31d6c2ad6ea3375a285';
18
    /** @var Client */
19
    protected $client;
20
    /** @var EntityManager */
21
    protected $em;
22
23
    protected $translator;
24
25
    /** set up fixtures */
26
    public function setUp()
27
    {
28
        $connection = $this->getContainer()->get('doctrine')->getConnection();
29
        $this->client = $this->createClient();
30
31
        $connection->exec('SET FOREIGN_KEY_CHECKS=0;');
32
        $connection->exec('DELETE FROM users;');
33
        $connection->exec('DELETE FROM event__tickets;');
34
        $connection->exec('ALTER TABLE event__tickets AUTO_INCREMENT = 1;');
35
        $connection->exec('SET FOREIGN_KEY_CHECKS=1;');
36
        $this->loadFixtures(
37
            [
38
                'Stfalcon\Bundle\EventBundle\DataFixtures\ORM\LoadEventData',
39
                'Application\Bundle\UserBundle\DataFixtures\ORM\LoadUserData',
40
                'Stfalcon\Bundle\EventBundle\DataFixtures\ORM\LoadPaymentData',
41
                'Stfalcon\Bundle\EventBundle\DataFixtures\ORM\LoadTicketData',
42
            ],
43
            null,
44
            'doctrine',
45
            ORMPurger::PURGE_MODE_DELETE
46
        );
47
        $this->em = $this->getContainer()->get('doctrine')->getManager();
48
        $this->translator = $this->getContainer()->get('translator');
49
    }
50
51
    /** destroy */
52
    public function tearDown()
53
    {
54
        parent::tearDown();
55
    }
56
57
    /**
58
     * test en html ticket hash.
59
     */
60
    public function testEnTicketHash()
61
    {
62
        $this->assertEquals(self::EN_FILE_HASH, $this->getFileHash('en'));
63
    }
64
65
    /**
66
     * test uk html ticket hash.
67
     */
68
    public function testUkTicketHash()
69
    {
70
        $this->assertEquals(self::UK_FILE_HASH, $this->getFileHash('uk'));
71
    }
72
73
    /**
74
     * Test uk local in cookie.
75
     */
76
    public function testUkCookieLocale()
77
    {
78
        $this->assertEquals('uk', $this->getLangCookie('uk'));
79
    }
80
81
    /**
82
     * Test en local in cookie.
83
     */
84
    public function testEnCookieLocale()
85
    {
86
        $this->assertEquals('en', $this->getLangCookie('en'));
87
    }
88
89
    /**
90
     * get current local from cookie.
91
     *
92
     * @param string $lang
93
     *
94
     * @return string
95
     */
96
    private function getLangCookie($lang)
97
    {
98
        if (!empty($lang)) {
99
            $this->client->followRedirects();
100
            $this->client->request('GET', sprintf('/%s', $lang));
101
102
            return $this->client->getRequest()->cookies->get('hl');
103
        }
104
105
        return '';
106
    }
107
108
    /**
109
     * get file hash by lang.
110
     *
111
     * @param string $lang
112
     *
113
     * @return string
114
     */
115
    private function getFileHash($lang)
116
    {
117
        if (!empty($lang)) {
118
            $this->loginUser('[email protected]', 'qwerty', $lang);
119
            $this->client->request('GET', sprintf('/%s/event/javaScript-framework-day-2018/ticket/html', $lang));
120
            $content = $this->client->getResponse()->getContent();
121
122
            return md5($content);
123
        }
124
125
        return '';
126
    }
127
128
    /**
129
     * @param string $userName
130
     * @param string $userPass
131
     * @param string $lang
132
     *
133
     * @return User $user
134
     */
135
    private function loginUser($userName, $userPass, $lang)
136
    {
137
        $user = $this->em->getRepository('ApplicationUserBundle:User')->findOneBy(['email' => $userName]);
138
        $this->assertNotNull($user, sprintf('User %s not founded!', $userName));
139
140
        $loginBtnCaption = 'Sign in';
141
        $accountLinkCaption = ' Account';
142
143
        if ('uk' === $lang) {
144
            $loginBtnCaption = 'Увійти';
145
            $accountLinkCaption = ' Кабінет';
146
        }
147
        /* start Login */
148
        $this->client->followRedirects();
149
        $crawler = $this->client->request('GET', $lang.'/login');
150
        $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
151
        $this->assertContains('<button class="btn btn--primary btn--lg form-col__btn" type="submit">'.$loginBtnCaption.'
152
            </button>', $crawler->html());
153
        $form = $crawler->selectButton($loginBtnCaption)->form();
154
        $form['_username'] = $user->getEmail();
155
        $form['_password'] = $userPass;
156
157
        $this->client->submit($form);
158
        /** end Login */
159
        $crawler = $this->client->request('GET', '/');
160
        $this->assertGreaterThan(0, $crawler->filter('a:contains("'.$accountLinkCaption.'")')->count());
161
162
        return $user;
163
    }
164
}
165