Completed
Push — master ( 851d6f...c63a9b )
by Christian
02:28
created

validationScenarioDataProvider()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 26

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 38
rs 8.8571
cc 1
eloc 26
nc 1
nop 0
1
<?php
2
3
namespace uuf6429\ElderBrother\Action;
4
5
use Symfony\Component\Console\Input;
6
use Symfony\Component\Console\Output;
7
use uuf6429\ElderBrother\Change;
8
9
class PhpCsFixerOldTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @param array           $fileContents
13
     * @param null|array      $expectedFileContents
14
     * @param null|\Exception $expectedException
15
     *
16
     * @throws \Exception
17
     *
18
     * @dataProvider validationScenarioDataProvider
19
     */
20
    public function testValidationScenario($fileContents, $expectedFileContents = null, $expectedException = null)
21
    {
22
        $createdFiles = [];
23
24
        try {
25
            $createdFiles = $this->createFiles($fileContents);
26
27
            $fileList = new Change\FileList(
28
                'cache' . mt_rand(),
29
                function () use ($createdFiles) {
30
                    return $createdFiles;
31
                }
32
            );
33
34
            $binFile = realpath(__DIR__ . '/../../../../vendor/friendsofphp/php-cs-fixer/php-cs-fixer');
35
            $this->assertNotFalse($binFile, 'PHP-CS-Fixer executable could not be located (cwd: ' . getcwd() . ').');
36
37
            $configFile = tempnam(sys_get_temp_dir(), 'pcc');
38
            $this->assertNotFalse(
39
                file_put_contents(
40
                    $configFile,
41
                    '<?php return Symfony\CS\Config\Config::create()'
42
                    . '->level(Symfony\CS\FixerInterface::SYMFONY_LEVEL)'
43
                    . '->fixers(["linefeed"]);'
44
                ),
45
                'PHP-CS-Fixer config file could not be saved: ' . $configFile
46
            );
47
48
            $action = new PhpCsFixerOld($fileList, $binFile, $configFile, false);
49
50
            try {
51
                $action->execute($this->getInputMock(), $this->getOutputMock());
52
53
                if (!is_null($expectedFileContents)) {
54
                    $this->assertEquals(
55
                        $expectedFileContents,
56
                        array_combine(
57
                            array_keys($fileContents),
58
                            array_map(
59
                                function ($file) {
60
                                    return file($file, FILE_IGNORE_NEW_LINES);
61
                                },
62
                                $createdFiles
63
                            )
64
                        )
65
                    );
66
                }
67
68
                $this->assertNull($expectedException, 'No exception should be thrown.');
69
            } catch (\Exception $ex) {
70
                if (!$expectedException) {
71
                    throw $ex;
72
                }
73
74
                // replace virtual filenames with fake filenames
75
                $message = str_replace(
76
                    $createdFiles,
77
                    array_keys($fileContents),
78
                    $ex->getMessage()
79
                );
80
81
                // do some asserting :)
82
                $this->assertSame(get_class($expectedException), get_class($ex));
83
                $this->assertSame($expectedException->getMessage(), $message);
84
            }
85
86
            $this->removeFiles($createdFiles);
87
        } catch (\Exception $ex) {
88
            $this->removeFiles($createdFiles);
89
            throw $ex;
90
        }
91
    }
92
93
    /**
94
     * @return array
95
     */
96
    public function validationScenarioDataProvider()
97
    {
98
        return [
99
            'no files should not cause exception' => [
100
                '$fileContents' => [],
101
                '$expectedFileContents' => [],
102
                '$expectedException' => null,
103
            ],
104
            'a file with an inline class and method' => [
105
                '$fileContents' => [
106
                    'file1.php' => '<?php class Test {public function sayHello(){echo "Hello!";}} ',
107
                ],
108
                '$expectedFileContents' => [
109
                    'file1.php' => [
110
                        '<?php class Test',
111
                        '{',
112
                        '    public function sayHello()',
113
                        '    {',
114
                        '        echo \'Hello!\';',
115
                        '    }',
116
                        '}',
117
                    ],
118
                ],
119
                '$expectedException' => null,
120
            ],
121
            'a file with a syntax error' => [
122
                '$fileContents' => [
123
                    'file2.php' => '<?php *e(cho Test1"; ',
124
                ],
125
                '$expectedFileContents' => [
126
                    'file2.php' => [
127
                        '<?php *e(cho Test1"; ',
128
                    ],
129
                ],
130
                '$expectedException' => null,
131
            ],
132
        ];
133
    }
134
135
    /**
136
     * @return Input\InputInterface
137
     */
138
    protected function getInputMock()
139
    {
140
        return $this->getMockBuilder(Input\InputInterface::class)
141
            ->getMock();
142
    }
143
144
    /**
145
     * @return Output\OutputInterface
146
     */
147
    protected function getOutputMock()
148
    {
149
        return new Output\NullOutput();
150
    }
151
152
    /**
153
     * @param array<string, string> $fileContents
154
     *
155
     * @return string[]
156
     */
157
    protected function createFiles($fileContents)
158
    {
159
        return array_values(
160
            array_map(
161
                function ($index, $content) {
162
                    $filename = tempnam(sys_get_temp_dir(), $index);
163
                    file_put_contents($filename, $content);
164
165
                    return $filename;
166
                },
167
                array_keys($fileContents),
168
                $fileContents
169
            )
170
        );
171
    }
172
173
    /**
174
     * @param string[] $files
175
     */
176
    protected function removeFiles($files)
177
    {
178
        foreach ($files as $file) {
179
            unlink($file);
180
        }
181
    }
182
}
183