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

FileListSqlTest::fileListQueryDataProvider()   B

Complexity

Conditions 4
Paths 1

Size

Total Lines 44
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

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