Passed
Push — master ( 450d0c...ced8c3 )
by Maurício
07:19
created

TriggersTest::testAddTrigger()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 56
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 34
dl 0
loc 56
rs 9.376
c 0
b 0
f 0
cc 1
nc 1
nop 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* vim: set expandtab sw=4 ts=4 sts=4: */
3
/**
4
 * Selenium TestCase for table related tests
5
 *
6
 * @package    PhpMyAdmin-test
7
 * @subpackage Selenium
8
 */
9
declare(strict_types=1);
10
11
namespace PhpMyAdmin\Tests\Selenium\Database;
12
13
use PhpMyAdmin\Tests\Selenium\TestBase;
14
15
/**
16
 * TriggersTest class
17
 *
18
 * @package    PhpMyAdmin-test
19
 * @subpackage Selenium
20
 * @group      selenium
21
 */
22
class TriggersTest extends TestBase
23
{
24
    /**
25
     * Setup the browser environment to run the selenium test case
26
     *
27
     * @return void
28
     */
29
    protected function setUp()
30
    {
31
        parent::setUp();
32
        $this->dbQuery(
33
            "CREATE TABLE `test_table` ("
34
            . " `id` int(11) NOT NULL AUTO_INCREMENT,"
35
            . " `val` int(11) NOT NULL,"
36
            . " PRIMARY KEY (`id`)"
37
            . ")"
38
        );
39
40
        $this->dbQuery(
41
            "CREATE TABLE `test_table2` ("
42
            . " `id` int(11) NOT NULL AUTO_INCREMENT,"
43
            . " `val` int(11) NOT NULL,"
44
            . " PRIMARY KEY (`id`)"
45
            . ")"
46
        );
47
        $this->dbQuery(
48
            "INSERT INTO `test_table2` (val) VALUES (2);"
49
        );
50
51
        $this->login();
52
53
        $this->navigateDatabase($this->database_name);
54
    }
55
56
    /**
57
     * Creates procedure for tests
58
     *
59
     * @return void
60
     */
61
    private function _triggerSQL()
62
    {
63
        $this->dbQuery(
64
            "CREATE TRIGGER `test_trigger` "
65
            . "AFTER INSERT ON `test_table` FOR EACH ROW"
66
            . " UPDATE `" . $this->database_name
67
            . "`.`test_table2` SET val = val + 1"
68
        );
69
    }
70
71
    /**
72
     * Create a Trigger
73
     *
74
     * @return void
75
     *
76
     * @group large
77
     */
78
    public function testAddTrigger()
79
    {
80
        $this->expandMore();
81
        $this->waitForElement('partialLinkText', "Triggers")->click();
82
        $this->waitAjax();
83
84
        $this->waitForElement('partialLinkText', "Add trigger")->click();
85
        $this->waitAjax();
86
87
        $this->waitForElement('className', "rte_form");
88
89
        $this->byName("item_name")->sendKeys("test_trigger");
90
91
        $this->selectByLabel(
92
            $this->byName("item_table"),
93
            'test_table'
94
        );
95
96
        $this->selectByLabel(
97
            $this->byName("item_timing"),
98
            'AFTER'
99
        );
100
101
        $this->selectByLabel(
102
            $this->byName("item_event"),
103
            'INSERT'
104
        );
105
106
        $proc = "UPDATE " . $this->database_name . ".`test_table2` SET val=val+1";
107
        $this->typeInTextArea($proc);
108
109
        $this->byXPath("//button[contains(., 'Go')]")->click();
110
111
        $ele = $this->waitForElement(
0 ignored issues
show
Unused Code introduced by
The assignment to $ele is dead and can be removed.
Loading history...
112
            'xpath',
113
            "//div[@class='success' and contains(., "
114
            . "'Trigger `test_trigger` has been created')]"
115
        );
116
117
        $this->assertTrue(
118
            $this->isElementPresent(
119
                'xpath',
120
                "//td[contains(., 'test_trigger')]"
121
            )
122
        );
123
124
        $result = $this->dbQuery(
125
            "SHOW TRIGGERS FROM `" . $this->database_name . "`;"
126
        );
127
        $this->assertEquals(1, $result->num_rows);
128
129
        // test trigger
130
        $this->dbQuery("INSERT INTO `test_table` (val) VALUES (1);");
131
        $result = $this->dbQuery("SELECT val FROM `test_table2`;");
132
        $row = $result->fetch_assoc();
133
        $this->assertEquals(3, $row['val']);
134
    }
135
136
    /**
137
     * Test for editing Triggers
138
     *
139
     * @return void
140
     *
141
     * @group large
142
     */
143
    public function testEditTriggers()
144
    {
145
        $this->expandMore();
146
147
        $this->_triggerSQL();
148
        $this->waitForElement('partialLinkText', "Triggers")->click();
149
        $this->waitAjax();
150
151
        $this->waitForElement(
152
            'xpath',
153
            "//legend[contains(., 'Triggers')]"
154
        );
155
156
        $this->byPartialLinkText("Edit")->click();
157
158
        $this->waitForElement('className', "rte_form");
159
        $proc = "UPDATE " . $this->database_name . ".`test_table2` SET val=val+10";
160
        $this->typeInTextArea($proc);
161
162
        $this->byXPath("//button[contains(., 'Go')]")->click();
163
164
        $ele = $this->waitForElement(
0 ignored issues
show
Unused Code introduced by
The assignment to $ele is dead and can be removed.
Loading history...
165
            'xpath',
166
            "//div[@class='success' and contains(., "
167
            . "'Trigger `test_trigger` has been modified')]"
168
        );
169
170
        // test trigger
171
        $this->dbQuery("INSERT INTO `test_table` (val) VALUES (1);");
172
        $result = $this->dbQuery("SELECT val FROM `test_table2`;");
173
        $row = $result->fetch_assoc();
174
        $this->assertEquals(12, $row['val']);
175
    }
176
177
    /**
178
     * Test for dropping Trigger
179
     *
180
     * @return void
181
     *
182
     * @group large
183
     */
184
    public function testDropTrigger()
185
    {
186
        $this->expandMore();
187
188
        $this->_triggerSQL();
189
        $ele = $this->waitForElement('partialLinkText', "Triggers");
190
        $ele->click();
191
192
        $this->waitForElement(
193
            'xpath',
194
            "//legend[contains(., 'Triggers')]"
195
        );
196
197
        $this->byPartialLinkText("Drop")->click();
198
        $this->waitForElement(
199
            'cssSelector',
200
            "button.submitOK"
201
        )->click();
202
203
        $this->waitAjaxMessage();
204
205
        // test trigger
206
        $this->dbQuery("INSERT INTO `test_table` (val) VALUES (1);");
207
        $result = $this->dbQuery("SELECT val FROM `test_table2`;");
208
        $row = $result->fetch_assoc();
209
        $this->assertEquals(2, $row['val']);
210
211
        $result = $this->dbQuery(
212
            "SHOW TRIGGERS FROM `" . $this->database_name . "`;"
213
        );
214
        $this->assertEquals(0, $result->num_rows);
215
    }
216
}
217