Passed
Push — master ( 3ac11f...811690 )
by Maurício
08:10
created

ReplaceControllerTest::testReplace()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 81
Code Lines 65

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 65
c 2
b 0
f 0
nc 1
nop 0
dl 0
loc 81
rs 8.7636

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
3
declare(strict_types=1);
4
5
namespace PhpMyAdmin\Tests\Controllers\Table;
6
7
use PhpMyAdmin\CheckUserPrivileges;
8
use PhpMyAdmin\ConfigStorage\Relation;
9
use PhpMyAdmin\ConfigStorage\RelationCleanup;
10
use PhpMyAdmin\ConfigStorage\RelationParameters;
11
use PhpMyAdmin\Controllers\Sql\SqlController;
12
use PhpMyAdmin\Controllers\Table\ChangeController;
13
use PhpMyAdmin\Controllers\Table\ReplaceController;
14
use PhpMyAdmin\FileListing;
15
use PhpMyAdmin\Http\ServerRequest;
16
use PhpMyAdmin\InsertEdit;
17
use PhpMyAdmin\Operations;
18
use PhpMyAdmin\Sql;
19
use PhpMyAdmin\Template;
20
use PhpMyAdmin\Tests\AbstractTestCase;
21
use PhpMyAdmin\Tests\Stubs\ResponseRenderer;
22
use PhpMyAdmin\Transformations;
23
use Psr\Container\ContainerInterface;
24
25
/**
26
 * @covers \PhpMyAdmin\Controllers\Table\ReplaceController
27
 */
28
class ReplaceControllerTest extends AbstractTestCase
29
{
30
    protected function setUp(): void
31
    {
32
        parent::setUp();
33
        $GLOBALS['server'] = 1;
34
        $GLOBALS['showtable'] = null;
35
        $GLOBALS['PMA_PHP_SELF'] = 'index.php';
36
        $GLOBALS['db'] = 'my_db';
37
        $GLOBALS['table'] = 'test_tbl';
38
39
        $GLOBALS['cfg']['Server']['user'] = 'user';
40
        $GLOBALS['cfg']['Server']['DisableIS'] = false;
41
42
        $_SESSION['relation'] = [];
43
        $_SESSION['relation'][$GLOBALS['server']] = RelationParameters::fromArray([
44
            'table_coords' => 'table_name',
45
            'displaywork' => true,
46
            'db' => 'information_schema',
47
            'table_info' => 'table_info',
48
            'relwork' => true,
49
            'relation' => 'relation',
50
            'mimework' => true,
51
            'commwork' => true,
52
            'column_info' => 'column_info',
53
            'pdf_pages' => 'pdf_pages',
54
            'bookmarkwork' => true,
55
            'bookmark' => 'bookmark',
56
            'uiprefswork' => true,
57
            'table_uiprefs' => 'table_uiprefs',
58
        ])->toArray();
59
    }
60
61
    public function testReplace(): void
62
    {
63
        $GLOBALS['urlParams'] = [];
64
        $_POST['db'] = $GLOBALS['db'];
65
        $_POST['table'] = $GLOBALS['table'];
66
        $_POST['ajax_request'] = 'true';
67
        $_POST['sql_query'] = '';
68
        $_POST['clause_is_unique'] = 1;
69
        $_POST['where_clause'] = [
70
            '`test`.`ser` = 2',
71
            '`test`.`ser` = 1',
72
        ];
73
        $_POST['rel_fields_list'] = '';
74
        $_POST['do_transformations'] = true;
75
        $_POST['transform_fields_list'] = '0%5Bvc%5D=sss%20s%20s&1%5Bvc%5D=zzff%20s%20sf%0A';
76
        $_POST['relational_display'] = 'K';
77
        $_POST['goto'] = 'index.php?route=/sql';
78
        $_POST['submit_type'] = 'save';
79
        $_POST['fields'] = [
80
            'multi_edit' => [
81
                0 => ['zzff s sf'],
82
                1 => ['sss s s'],
83
            ],
84
        ];
85
        $_POST['fields_name'] = [
86
            'multi_edit' => [
87
                0 => ['vc'],
88
                1 => ['vc'],
89
            ],
90
        ];
91
        $_POST['fields_null'] = [
92
            'multi_edit' => [
93
                0 => [],
94
                1 => [],
95
            ],
96
        ];
97
98
        $dummyDbi = $this->createDbiDummy();
99
        $dbi = $this->createDatabaseInterface($dummyDbi);
100
        $relation = new Relation($dbi);
101
        $transformations = new Transformations();
102
        $template = new Template();
103
        $response = new ResponseRenderer();
104
        $replaceController = new ReplaceController(
105
            $response,
106
            $template,
107
            new InsertEdit($dbi, $relation, $transformations, new FileListing(), $template),
108
            $transformations,
109
            $relation,
110
            $dbi
111
        );
112
113
        $request = $this->createStub(ServerRequest::class);
114
        $sqlController = new SqlController(
115
            $response,
116
            $template,
117
            new Sql(
118
                $dbi,
119
                $relation,
120
                new RelationCleanup($dbi, $relation),
121
                new Operations($dbi, $relation),
122
                $transformations,
123
                $template
124
            ),
125
            new CheckUserPrivileges($dbi),
126
            $dbi
127
        );
128
        $GLOBALS['containerBuilder'] = $this->createStub(ContainerInterface::class);
129
        $GLOBALS['containerBuilder']->method('get')->willReturn($sqlController);
130
131
        $GLOBALS['goto'] = 'index.php?route=/sql';
132
        $dummyDbi->addSelectDb('my_db');
133
        $dummyDbi->addSelectDb('my_db');
134
        $replaceController($request);
135
        $output = $response->getHTMLResult();
136
        $this->assertAllSelectsConsumed();
137
        $this->assertStringContainsString(
138
            'class="icon ic_s_success"> Showing rows 0 -  1 (2 total, Query took',
139
            $output
140
        );
141
        $this->assertStringContainsString('SELECT * FROM `test_tbl`', $output);
142
    }
143
144
    public function testIsInsertRow(): void
145
    {
146
        $GLOBALS['urlParams'] = [];
147
        $GLOBALS['goto'] = 'index.php?route=/sql';
148
        $_POST['insert_rows'] = 5;
149
        $_POST['sql_query'] = 'SELECT 1';
150
        $GLOBALS['cfg']['InsertRows'] = 2;
151
        $GLOBALS['cfg']['Server']['host'] = 'host.tld';
152
        $GLOBALS['cfg']['Server']['verbose'] = '';
153
154
        $dummyDbi = $this->createDbiDummy();
155
        $dbi = $this->createDatabaseInterface($dummyDbi);
156
        $GLOBALS['dbi'] = $dbi;
157
        $relation = new Relation($dbi);
158
        $transformations = new Transformations();
159
        $template = new Template();
160
        $response = new ResponseRenderer();
161
        $insertEdit = new InsertEdit($dbi, $relation, $transformations, new FileListing(), $template);
162
        $replaceController = new ReplaceController(
163
            $response,
164
            $template,
165
            $insertEdit,
166
            $transformations,
167
            $relation,
168
            $dbi
169
        );
170
171
        $request = $this->createStub(ServerRequest::class);
172
        $changeController = new ChangeController($response, $template, $insertEdit, $relation);
173
        $GLOBALS['containerBuilder'] = $this->createStub(ContainerInterface::class);
174
        $GLOBALS['containerBuilder']->method('get')->willReturn($changeController);
175
176
        $dummyDbi->addSelectDb('my_db');
177
        $dummyDbi->addSelectDb('my_db');
178
        $dummyDbi->addResult('SHOW TABLES LIKE \'test_tbl\';', [['test_tbl']]);
179
        $dummyDbi->addResult('SELECT * FROM `my_db`.`test_tbl` LIMIT 1;', []);
180
        $dummyDbi->addSelectDb('my_db');
181
182
        $replaceController($request);
183
        $output = $response->getHTMLResult();
184
        $this->assertAllSelectsConsumed();
185
        $this->assertEquals(5, $GLOBALS['cfg']['InsertRows']);
186
        $this->assertStringContainsString(
187
            '<form id="continueForm" method="post" '
188
            . 'action="index.php?route=/table/replace&lang=en" name="continueForm">',
189
            $output
190
        );
191
        $this->assertStringContainsString(
192
            'Continue insertion with         <input type="number" '
193
            . 'name="insert_rows" id="insert_rows" value="5" min="1">',
194
            $output
195
        );
196
    }
197
}
198