IOHelperTest::testGetCommonPathPrefixForNoFiles()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * Test case
5
 *
6
 * Copyright (c) 2007-2010, Mayflower GmbH
7
 * All rights reserved.
8
 *
9
 * Redistribution and use in source and binary forms, with or without
10
 * modification, are permitted provided that the following conditions
11
 * are met:
12
 *
13
 *   * Redistributions of source code must retain the above copyright
14
 *     notice, this list of conditions and the following disclaimer.
15
 *
16
 *   * Redistributions in binary form must reproduce the above copyright
17
 *     notice, this list of conditions and the following disclaimer in
18
 *     the documentation and/or other materials provided with the
19
 *     distribution.
20
 *
21
 *   * Neither the name of Mayflower GmbH nor the names of his
22
 *     contributors may be used to endorse or promote products derived
23
 *     from this software without specific prior written permission.
24
 *
25
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
28
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
29
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
30
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
32
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
35
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
 * POSSIBILITY OF SUCH DAMAGE.
37
 *
38
 * @category PHP_CodeBrowser
39
 *
40
 * @author Simon Kohlmeyer <[email protected]
41
 *
42
 * @copyright 2007-2010 Mayflower GmbH
43
 *
44
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
45
 *
46
 * @version SVN: $Id$
47
 *
48
 * @link http://www.phpunit.de/
49
 *
50
 * @since File available since  0.1.0
51
 */
52
53
namespace PHPCodeBrowser\Tests\Helper;
54
55
use PHPCodeBrowser\Helper\IOHelper;
56
use PHPCodeBrowser\Tests\AbstractTestCase;
57
58
/**
59
 * IOHelperTest
60
 *
61
 * @category PHP_CodeBrowser
62
 *
63
 * @author Simon Kohlmeyer <[email protected]>
64
 *
65
 * @copyright 2007-2010 Mayflower GmbH
66
 *
67
 * @license http://www.opensource.org/licenses/bsd-license.php  BSD License
68
 *
69
 * @version Release: @package_version@
70
 *
71
 * @link http://www.phpunit.de/
72
 *
73
 * @since Class available since  0.1.0
74
 */
75
class IOHelperTest extends AbstractTestCase
76
{
77
    /**
78
     * The IOHelper object under test.
79
     *
80
     * @var IOHelper
81
     */
82
    protected $ioHelper;
83
84
    /**
85
     * (non-PHPDoc)
86
     *
87
     * @see AbstractTestCase::setUp()
88
     */
89
    protected function setUp(): void
90
    {
91
        parent::setUp();
92
93
        $this->ioHelper = new IOHelper();
94
    }
95
96
    /**
97
     * Test createFile function without creating a path
98
     *
99
     * @return void
100
     */
101
    public function testFileCreation(): void
102
    {
103
        $filename = self::$testOutputDir.'/tmpfile';
104
        $content  = 'Lorem ipsum';
105
106
        if (\file_exists($filename)) {
107
            \unlink($filename);
108
        }
109
110
        $this->ioHelper->createFile($filename, $content);
111
        static::assertFileExists($filename);
112
        static::assertEquals($content, \file_get_contents($filename));
113
114
        \unlink($filename);
115
    }
116
117
    /**
118
     * Test createFile function with creating a path
119
     *
120
     * @return void
121
     */
122
    public function testCreationOfFileWithPath(): void
123
    {
124
        $dirName  = self::$testOutputDir.'/tmpdir';
125
        $filename = $dirName.'/tmpfile';
126
        $content  = 'Lorem ipsum';
127
128
        if (\file_exists($filename)) {
129
            \unlink($filename);
130
            \rmdir($dirName);
131
        } elseif (\file_exists($dirName)) {
132
            \rmdir($dirName);
133
        }
134
135
        $this->ioHelper->createFile($filename, $content);
136
        static::assertFileExists($dirName);
137
        static::assertFileExists($filename);
138
        static::assertEquals($content, \file_get_contents($filename));
139
140
        \unlink($filename);
141
        \rmdir($dirName);
142
    }
143
144
    /**
145
     * Test deleteFile function
146
     *
147
     * @return void
148
     */
149
    public function testFileDeletion(): void
150
    {
151
        $filename = self::$testOutputDir.'/tmpfile';
152
153
        if (!\file_exists($filename)) {
154
            \file_put_contents($filename, 'Lorem ipsum');
155
        }
156
157
        $this->ioHelper->deleteFile($filename);
158
        static::assertFileDoesNotExist($filename);
159
    }
160
161
    /**
162
     * Test deleteDirectory function
163
     *
164
     * @return void
165
     */
166
    public function testDirectoryDeletion(): void
167
    {
168
        $dir    = self::$testOutputDir.'/dir';
169
        $file   = $dir.'/file';
170
        $subDir = $dir.'/subDir';
171
172
        \mkdir($dir);
173
        \mkdir($subDir);
174
        \touch($file);
175
176
        $this->ioHelper->deleteDirectory($dir);
177
        static::assertFileDoesNotExist($dir);
178
    }
179
180
    /**
181
     * Test copyFile function
182
     *
183
     * @return void
184
     */
185
    public function testCopyFile(): void
186
    {
187
        $srcFile = self::$testOutputDir.'/tmpfile';
188
        $dstDir  = self::$testOutputDir.'/tmpdir';
189
        $dstFile = $dstDir.'/tmpfile';
190
        $content = 'Lorem ipsum';
191
192
        if (\file_exists($srcFile)) {
193
            \unlink($srcFile);
194
        }
195
196
        if (\file_exists($dstFile)) {
197
            \rmdir($dstFile);
198
        }
199
200
        \file_put_contents($srcFile, $content);
201
202
        $this->ioHelper->copyFile($srcFile, $dstDir);
203
        static::assertFileExists($srcFile);
204
        static::assertFileExists($dstDir);
205
        static::assertFileExists($dstFile);
206
        static::assertEquals($content, \file_get_contents($dstFile));
207
        static::assertEquals($content, \file_get_contents($srcFile));
208
209
        \unlink($dstFile);
210
        \rmdir($dstDir);
211
        \unlink($srcFile);
212
    }
213
214
    /**
215
     * Test loadFile function for non-existent file.
216
     *
217
     * @return void
218
     */
219
    public function testLoadFileWithNonexistentFile(): void
220
    {
221
        $this->expectException(\Exception::class);
222
223
        $sourceFile = self::$testOutputDir.'/doesNotExist';
224
225
        if (\file_exists($sourceFile)) {
226
            \unlink(self::$testOutputDir.'/doesNotExist');
227
        }
228
229
        $this->ioHelper->loadFile($sourceFile);
230
    }
231
232
    /**
233
     * Test copyFile function for non-existent source file
234
     *
235
     * @return void
236
     */
237
    public function testCopyFileNonExisting(): void
238
    {
239
        $this->expectException(\Exception::class);
240
241
        $file   = self::$testOutputDir.'/tmpfile';
242
        $dstDir = self::$testOutputDir.'/tmpdir';
243
244
        if (\file_exists($file)) {
245
            \unlink($file);
246
        }
247
248
        $this->ioHelper->copyFile($file, $dstDir);
249
    }
250
251
    /**
252
     * Test getCommonPathPrefix with empty file list.
253
     *
254
     * @return void
255
     */
256
    public function testGetCommonPathPrefixForNoFiles(): void
257
    {
258
        static::assertEquals(
259
            '/',
260
            $this->ioHelper::getCommonPathPrefix([])
261
        );
262
    }
263
}
264