Completed
Push — master ( 5b41a4...63949e )
by Joschi
04:17
created

ApiTest.php ➔ rename()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
3
/**
4
 * apparat-resource
5
 *
6
 * @category    Apparat
7
 * @package     Apparat\Resource
8
 * @subpackage  Apparat\Resource\Tests
9
 * @author      Joschi Kuphal <[email protected]> / @jkphl
10
 * @copyright   Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
11
 * @license     http://opensource.org/licenses/MIT The MIT License (MIT)
12
 */
13
14
/***********************************************************************************
15
 *  The MIT License (MIT)
16
 *
17
 *  Copyright © 2016 Joschi Kuphal <[email protected]> / @jkphl
18
 *
19
 *  Permission is hereby granted, free of charge, to any person obtaining a copy of
20
 *  this software and associated documentation Fixture (the "Software"), to deal in
21
 *  the Software without restriction, including without limitation the rights to
22
 *  use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
23
 *  the Software, and to permit persons to whom the Software is furnished to do so,
24
 *  subject to the following conditions:
25
 *
26
 *  The above copyright notice and this permission notice shall be included in all
27
 *  copies or substantial portions of the Software.
28
 *
29
 *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
30
 *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
31
 *  FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
32
 *  COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
33
 *  IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
34
 *  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35
 ***********************************************************************************/
36
37
namespace Apparat\Resource\Tests {
38
39
    use Apparat\Kernel\Tests\AbstractTest;
40
    use Apparat\Resource\Infrastructure\Io\InMemory\Writer;
41
    use Apparat\Resource\Infrastructure\Service\RuntimeException;
42
    use Apparat\Resource\Ports\InvalidArgumentException;
43
    use Apparat\Resource\Ports\Tools;
44
45
    /**
46
     * I/O handler test
47
     *
48
     * @package     Apparat\Resource
49
     * @subpackage  Apparat\Resource\Tests
50
     */
51
    class ApiTest extends AbstractTest
52
    {
53
        /**
54
         * Example text file
55
         *
56
         * @var string
57
         */
58
        const TXT_FILE = __DIR__ . DIRECTORY_SEPARATOR . 'Fixture' . DIRECTORY_SEPARATOR . 'cc0.txt';
59
60
        /**
61
         * Test invalid reader stream wrapper while copying
62
         *
63
         * @expectedException InvalidArgumentException
64
         * @expectedExceptionCode 1448493550
65
         */
66
        public function testCopyInvalidReaderStreamWrapper()
67
        {
68
            Tools::copy('foo://bar');
69
        }
70
71
        /**
72
         * Test invalid writer stream wrapper while copying
73
         *
74
         * @expectedException InvalidArgumentException
75
         * @expectedExceptionCode 1448493564
76
         */
77
        public function testCopyInvalidWriterStreamWrapper()
78
        {
79
            Tools::copy('file://' . self::TXT_FILE)->toTarget('foo://bar');
80
        }
81
82
        /**
83
         * Test copying a string to a file
84
         */
85
        public function testCopyStringToFile()
86
        {
87
            $tempFile = $this->createTemporaryFileName();
88
            $randomString = md5(rand());
89
            Tools::copy($randomString)->toTarget('file://' . $tempFile);
90
            $this->assertStringEqualsFile($tempFile, $randomString);
91
        }
92
93
        /**
94
         * Test copying a file to a file
95
         */
96
        public function testCopyFileToFile()
97
        {
98
            $tempFile = $this->createTemporaryFileName();
99
            Tools::copy('file://' . self::TXT_FILE)->toTarget('file://' . $tempFile);
100
            $this->assertFileEquals($tempFile, self::TXT_FILE);
101
        }
102
103
        /**
104
         * Test error while copying a file to a file
105
         *
106
         * @expectedException RuntimeException
107
         * @expectedExceptionCode 1448569381
108
         */
109
        public function testCopyFileToFileError()
110
        {
111
            putenv('MOCK_COPY=1');
112
            $tempFile = $this->createTemporaryFileName();
113
            Tools::copy('file://' . self::TXT_FILE)->toTarget('file://' . $tempFile);
114
            $this->assertFileEquals($tempFile, self::TXT_FILE);
115
            putenv('MOCK_COPY');
116
        }
117
118
        /**
119
         * Test copying a string to a string
120
         */
121
        public function testCopyStringToString()
122
        {
123
            $randomString = md5(rand());
124
            /** @var Writer $writer */
125
            $writer = Tools::copy($randomString)->toTarget('');
126
            $this->assertInstanceOf(Writer::class, $writer);
127
            $this->assertEquals($randomString, $writer->getData());
128
        }
129
130
        /**
131
         * Test copying a file to a string
132
         */
133
        public function testCopyFileToString()
134
        {
135
            /** @var Writer $writer */
136
            $writer = Tools::copy('file://' . self::TXT_FILE)->toTarget('');
137
            $this->assertInstanceOf(Writer::class, $writer);
138
            $this->assertStringEqualsFile(self::TXT_FILE, $writer->getData());
139
        }
140
141
        /**
142
         * Test invalid reader stream wrapper while moving
143
         *
144
         * @expectedException InvalidArgumentException
145
         * @expectedExceptionCode 1448493550
146
         */
147
        public function testMoveInvalidReaderStreamWrapper()
148
        {
149
            Tools::move('foo://bar');
150
        }
151
152
        /**
153
         * Test invalid writer stream wrapper while moving
154
         *
155
         * @expectedException InvalidArgumentException
156
         * @expectedExceptionCode 1448493564
157
         */
158
        public function testMoveInvalidWriterStreamWrapper()
159
        {
160
            Tools::move('file://' . self::TXT_FILE)->toTarget('foo://bar');
161
        }
162
163
        /**
164
         * Test moving a string to a file
165
         */
166
        public function testMoveStringToFile()
167
        {
168
            $tempFile = $this->createTemporaryFileName();
169
            $randomString = md5(rand());
170
            Tools::move($randomString)->toTarget('file://' . $tempFile);
171
            $this->assertStringEqualsFile($tempFile, $randomString);
172
        }
173
174
        /**
175
         * Test moving a file to a file
176
         */
177
        public function testMoveFileToFile()
178
        {
179
            $srcFile = $this->createTemporaryFileName();
180
            copy(self::TXT_FILE, $srcFile);
181
            $tempFile = $this->createTemporaryFileName();
182
            Tools::move('file://' . $srcFile)->toTarget('file://' . $tempFile);
183
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
184
        }
185
186
        /**
187
         * Test error while moving a file to a file
188
         *
189
         * @expectedException RuntimeException
190
         * @expectedExceptionCode 1448571473
191
         */
192
        public function testMoveFileToFileError()
193
        {
194
            putenv('MOCK_MOVE=1');
195
            $tempFile = $this->createTemporaryFileName();
196
            Tools::move('file://' . self::TXT_FILE)->toTarget('file://' . $tempFile);
197
            $this->assertFileEquals($tempFile, self::TXT_FILE);
198
            putenv('MOCK_MOVE');
199
        }
200
201
        /**
202
         * Test moving a string to a string
203
         */
204
        public function testMoveStringToString()
205
        {
206
            $randomString = md5(rand());
207
            /** @var Writer $writer */
208
            $writer = Tools::move($randomString)->toTarget('');
209
            $this->assertInstanceOf(Writer::class, $writer);
210
            $this->assertEquals($randomString, $writer->getData());
211
        }
212
213
        /**
214
         * Test moving a file to a string
215
         */
216
        public function testMoveFileToString()
217
        {
218
            $srcFile = $this->createTemporaryFileName();
219
            copy(self::TXT_FILE, $srcFile);
220
            /** @var Writer $writer */
221
            $writer = Tools::move('file://' . $srcFile)->toTarget('');
222
            $this->assertInstanceOf(Writer::class, $writer);
223
            $this->assertStringEqualsFile(self::TXT_FILE, $writer->getData());
224
        }
225
226
        /**
227
         * Test deleting a file
228
         */
229
        public function testDeleteFile()
230
        {
231
            $srcFile = $this->createTemporaryFileName();
232
            copy(self::TXT_FILE, $srcFile);
233
            $this->assertEquals(true, Tools::delete('file://' . $srcFile));
234
            $this->assertFileNotExists($srcFile);
235
        }
236
237
        /**
238
         * Test error while deleting a file
239
         *
240
         * @expectedException RuntimeException
241
         * @expectedExceptionCode 1448574428
242
         */
243
        public function testDeleteFileError()
244
        {
245
            putenv('MOCK_UNLINK=1');
246
            Tools::delete('file://' . self::TXT_FILE);
247
            putenv('MOCK_UNLINK');
248
        }
249
250
        /**
251
         * Test invalid reader stream wrapper while deleting a file
252
         *
253
         * @expectedException InvalidArgumentException
254
         * @expectedExceptionCode 1448493550
255
         */
256
        public function testDeleteInvalidReaderStreamWrapper()
257
        {
258
            Tools::delete('foo://bar');
259
        }
260
    }
261
}
262
263
namespace Apparat\Resource\Infrastructure\Service {
264
265
    /**
266
     * Mocked version of the native copy() function
267
     *
268
     * @param string $source Source file
269
     * @param string $dest Destination file
270
     * @return bool
271
     */
272
    function copy($source, $dest)
273
    {
274
        return (getenv('MOCK_COPY') != 1) ? \copy($source, $dest) : false;
275
    }
276
277
    /**
278
     * Mocked version of the native rename() function
279
     *
280
     * @param string $source Source file
281
     * @param string $dest Destination file
282
     * @return bool
283
     */
284
    function rename($source, $dest)
285
    {
286
        return (getenv('MOCK_MOVE') != 1) ? \rename($source, $dest) : false;
287
    }
288
289
    /**
290
     * Mocked version of the native unlink() function
291
     *
292
     * @param string $filename File name
293
     * @return bool
294
     */
295
    function unlink($filename)
296
    {
297
        return (getenv('MOCK_UNLINK') != 1) ? \unlink($filename) : false;
298
    }
299
}
300