Completed
Pull Request — master (#30)
by Christian
02:15
created

FileListSqlTest::setUpBeforeClass()   B

Complexity

Conditions 3
Paths 3

Size

Total Lines 91
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 91
rs 8.518
c 0
b 0
f 0
cc 3
eloc 28
nc 3
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
3
namespace uuf6429\ElderBrother\Change;
4
5
use SqlParser\Statements\TransactionStatement;
6
use uuf6429\ElderBrother\BaseProjectTest;
7
use SqlParser\Statements\InsertStatement;
8
9
class FileListSqlTest extends BaseProjectTest
10
{
11
    public static function setUpBeforeClass()
12
    {
13
        parent::setUpBeforeClass();
14
15
        /** @noinspection SqlResolve */
16
        /** @noinspection SqlNoDataSourceInspection */
17
        foreach (
18
            [
19
                'src/Acme/Combinator.php' => '<?php namespace Acme; class Combinator {}',
20
                'src/Acme/Comparator.php' => '<?php namespace Acme; class Comparator {}',
21
                'README' => 'Please read me!',
22
                'sql/AA-000-init.sql' => <<<'SQL'
23
                    SET NAMES utf8;
24
                    SET TIME_ZONE='+00:00';
25
                    SET character set 'utf8';
26
                    DROP DATABASE IF EXISTS world;
27
                    CREATE DATABASE world;
28
SQL
29
                ,
30
                'sql/EB-000-schema.sql' => <<<'SQL'
31
                    CREATE TABLE `City` (
32
                      `ID` int(11) NOT NULL,
33
                      `Name` char(35) NOT NULL DEFAULT '',
34
                      `CountryCode` char(3) NOT NULL DEFAULT '',
35
                      `District` char(20) NOT NULL DEFAULT '',
36
                      `Population` int(11) NOT NULL DEFAULT '0'
37
                    ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
38
SQL
39
                ,
40
                'sql/EB-001-data.sql' => <<<'SQL'
41
                    INSERT INTO `City` (`ID`, `Name`, `CountryCode`, `District`, `Population`) VALUES
42
                    (1, 'Kabul', 'AFG', 'Kabol', 1780000),
43
                    (2, 'Qandahar', 'AFG', 'Qandahar', 237500),
44
                    (3, 'Herat', 'AFG', 'Herat', 186800),
45
                    (4, 'Mazar-e-Sharif', 'AFG', 'Balkh', 127800),
46
                    (5, 'Amsterdam', 'NLD', 'Noord-Holland', 731200),
47
                    (6, 'Rotterdam', 'NLD', 'Zuid-Holland', 593321),
48
                    (7, 'Haag', 'NLD', 'Zuid-Holland', 440900),
49
                    (8, 'Utrecht', 'NLD', 'Utrecht', 234323),
50
                    (9, 'Eindhoven', 'NLD', 'Noord-Brabant', 201843);
51
SQL
52
                ,
53
                'sql/EB-002-keys-and-fix.sql' => <<<SQL
54
                    ALTER TABLE `City` ADD PRIMARY KEY (`ID`);
55
                    UPDATE `City` SET `Name` = "کندهار‎" WHERE `ID` = 2;
56
SQL
57
                ,
58
                'sql/EB-003-more-data.sql' => <<<'SQL'
59
                    INSERT INTO `City` (`ID`, `Name`, `CountryCode`, `District`, `Population`) VALUES
60
                    (10,'Tilburg','NLD','Noord-Brabant',193238),
61
                    (11,'Groningen','NLD','Groningen',172701),
62
                    (12,'Breda','NLD','Noord-Brabant',160398),
63
                    (13,'Apeldoorn','NLD','Gelderland',153491);
64
SQL
65
                ,
66
                'sql/EB-004-much-more-data.sql' => <<<'SQL'
67
                    BEGIN;
68
                    INSERT INTO `City` (`ID`, `Name`, `CountryCode`, `District`, `Population`) VALUES
69
                    (14,'Nijmegen','NLD','Gelderland',152463),
70
                    (15,'Enschede','NLD','Overijssel',149544),
71
                    (16,'Haarlem','NLD','Noord-Holland',148772),
72
                    (17,'Almere','NLD','Flevoland',142465),
73
                    (18,'Arnhem','NLD','Gelderland',138020),
74
                    (19,'Zaanstad','NLD','Noord-Holland',135621),
75
                    (20,'´s-Hertogenbosch','NLD','Noord-Brabant',129170),
76
                    (21,'Amersfoort','NLD','Utrecht',126270),
77
                    (22,'Maastricht','NLD','Limburg',122087),
78
                    (23,'Dordrecht','NLD','Zuid-Holland',119811),
79
                    (24,'Leiden','NLD','Zuid-Holland',117196),
80
                    (25,'Haarlemmermeer','NLD','Noord-Holland',110722),
81
                    (26,'Zoetermeer','NLD','Zuid-Holland',110214),
82
                    (27,'Emmen','NLD','Drenthe',105853),
83
                    (28,'Zwolle','NLD','Overijssel',105819),
84
                    (29,'Ede','NLD','Gelderland',101574),
85
                    (30,'Delft','NLD','Zuid-Holland',95268),
86
                    (31,'Heerlen','NLD','Limburg',95052),
87
                    (32,'Alkmaar','NLD','Noord-Holland',92713),
88
                    (33,'Willemstad','ANT','Curaçao',2345),
89
                    (34,'Tirana','ALB','Tirana',270000),
90
                    (35,'Alger','DZA','Alger',2168000);
91
                    COMMIT;
92
SQL
93
                ,
94
            ] as $filename => $content
95
        ) {
96
            if (!is_dir(dirname($filename))) {
97
                mkdir(dirname($filename), 0755, true);
98
            }
99
            file_put_contents($filename, $content);
100
        }
101
    }
102
103
    /**
104
     * @param string[] $expectedItems
105
     * @param \Closure $itemsProvider
106
     *
107
     * @dataProvider fileListQueryDataProvider
108
     */
109
    public function testFileListQuery($expectedItems, $itemsProvider)
110
    {
111
        $baseDir = getcwd();
112
        $actualItems = array_map(
113
            function ($file) use ($baseDir) {
114
                return str_replace([$baseDir . DIRECTORY_SEPARATOR, '\\'], ['', '/'], $file);
115
            },
116
            $itemsProvider()
117
        );
118
        sort($actualItems);
119
120
        $this->assertEquals($expectedItems, $actualItems);
121
        $this->assertSame($expectedItems, $actualItems);
122
    }
123
124
    /**
125
     * @return array
126
     */
127
    public function fileListQueryDataProvider()
128
    {
129
        return [
130
            'sql that inserts more then 5 records' => [
131
                '$expectedItems' => [
132
                    'sql/EB-001-data.sql',
133
                    'sql/EB-004-much-more-data.sql',
134
                ],
135
                '$itemsProvider' => function () {
136
                    return FullChangeSet::get()->filter(
137
                        function (FileInfo $file) {
138
                            $checkStatements = function ($checkStatements, $statements) {
139
                                foreach ($statements as $statement) {
140
                                    if ($statement instanceof TransactionStatement) {
141
                                        if ($checkStatements($checkStatements, $statement->statements)) {
142
                                            return true;
143
                                        }
144
                                    } elseif ($statement instanceof InsertStatement) {
145
                                        if (count($statement->values) > 5) {
146
                                            return true;
147
                                        }
148
                                    }
149
                                }
150
151
                                return false;
152
                            };
153
154
                            return $checkStatements($checkStatements, $file->getSqlParser()->statements);
155
                        }
156
                    )->toArray();
157
                },
158
            ],
159
160
            'sql with ddl' => [
161
                '$expectedItems' => [
162
                    'sql/EB-000-schema.sql',
163
                    'sql/EB-002-keys-and-fix.sql',
164
                ],
165
                '$itemsProvider' => function () {
166
                    return FullChangeSet::get()->sqlWithDDL()->toArray();
167
                },
168
            ],
169
            'sql with ddl and dml' => [
170
                '$expectedItems' => [
171
                    'sql/EB-002-keys-and-fix.sql',
172
                ],
173
                '$itemsProvider' => function () {
174
                    return FullChangeSet::get()->sqlWithDDL()->sqlWithDML()->toArray();
175
                },
176
            ],
177
            'sql without ddl and dml' => [
178
                '$expectedItems' => [
179
                    'sql/EB-002-keys-and-fix.sql',
180
                ],
181
                '$itemsProvider' => function () {
182
                    return FullChangeSet::get()->sqlWithoutDDL()->sqlWithoutDML()->toArray();
183
                },
184
            ],
185
            'sql with tcl' => [
186
                '$expectedItems' => [
187
                    'sql/EB-004-much-more-data.sql'
188
                ],
189
                '$itemsProvider' => function () {
190
                    return FullChangeSet::get()->sqlWithTCL()->toArray();
191
                },
192
            ],
193
        ];
194
    }
195
}
196