PollWebTestCase::clickLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
/*
3
 * Copyright 2014-2017 Arnaud Bienvenu
4
 *
5
 * This file is part of Kyela.
6
7
 * Kyela is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published by
9
 * the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
12
 * Kyela is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with Kyela.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace Abienvenu\KyelaBundle\Tests\Controller;
23
24
use Symfony\Bundle\FrameworkBundle\Client;
25
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
26
use Symfony\Component\DomCrawler\Crawler;
27
use Symfony\Component\Translation\TranslatorInterface;
28
29
class PollWebTestCase extends WebTestCase
30
{
31
    /** @var Client */
32
    protected static $client;
33
    /** @var TranslatorInterface */
34
    protected static $translator;
35
36
    /**
37
     * WebTestCase setup (automitically called by framework before any test)
38
     */
39
    public static function setUpBeforeClass()
40
    {
41
        // Create a new client to browse the application
42
        self::$client = static::createClient();
43
        self::$translator = self::$client->getContainer()->get('translator');
44
    }
45
46
    /**
47
     * Clicks a link
48
     *
49
     * @param Crawler $crawler
50
     * @param string $where Translation key of the link text
51
     *
52
     * @return Crawler
53
     */
54
    public static function clickLink(Crawler $crawler, $where)
55
    {
56
        $link = $crawler->selectLink(self::$translator->trans($where));
57
        return self::$client->click($link->link());
58
    }
59
60
    /**
61
     * Clicks a button
62
     *
63
     * @param Crawler $crawler
64
     * @param string $button Translation key of the button name
65
     * @param string $formName Name of the template
66
     * @param array $formData Form data
67
     *
68
     * @return Crawler
69
     */
70
    public static function submitForm(Crawler $crawler, $button, $formName = "", $formData = [])
71
    {
72
        $crawler = $crawler->selectButton(self::$translator->trans($button));
73
        $data = [];
74
        foreach ($formData as $key => $value)
75
        {
76
            $data['abienvenu_kyelabundle_' . $formName . "[$key]"] = $value;
77
        }
78
        $form = $crawler->form($data);
79
        self::$client->submit($form);
80
        return self::$client->followRedirect();
81
    }
82
83
    /**
84
     * Checks an element is present in the page
85
     *
86
     * @param Crawler $crawler
87
     * @param string $filter CSS filter
88
     * @param integer $count number of expected occurences
89
     *
90
     * @return Crawler
91
     */
92
    public function checkElement(Crawler $crawler, $filter, $count = 1)
93
    {
94
        $crawler = $crawler->filter($filter);
95
        $this->assertEquals($count, $crawler->count(), "Wrong count of element $filter");
96
        return $crawler;
97
    }
98
99
    /**
100
     * Creates a poll
101
     *
102
     * @return Crawler
103
     */
104
    public static function createPollEntry()
105
    {
106
        $route = self::$client->getContainer()->get('router')->generate('poll_new');
107
        $crawler = self::$client->request('GET', $route);
108
        if (!$crawler)
109
        {
110
            throw new \Exception("Could not create crawler");
111
        }
112
        return self::submitForm($crawler, 'create', 'newpoll');
113
    }
114
115
    /**
116
     * Deletes a poll
117
     *
118
     * @param Crawler $crawler
119
     *
120
     * @return Crawler
121
     */
122
    public static function deletePollEntry(Crawler $crawler)
123
    {
124
        $crawler = self::clickLink($crawler, 'edit.poll');
125
        return self::submitForm($crawler, 'delete');
126
    }
127
}
128