Completed
Push — master ( 871885...5b41a4 )
by Joschi
02:58
created

FileIoTest   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 207
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 8
Bugs 1 Features 0
Metric Value
wmc 15
c 8
b 1
f 0
lcom 1
cbo 2
dl 0
loc 207
rs 10

15 Methods

Rating   Name   Duplication   Size   Complexity  
A tearDown() 0 6 1
A testFileReaderWithInvalidFilepath() 0 4 1
A testFileReaderWithDirectory() 0 4 1
A testFileReaderWithUnreadableFile() 0 5 1
A testFileWriterWithInvalidOptions() 0 4 1
A testFileWriterWithNonCreatableFile() 0 4 1
A testFileWriterWithNonOverwriteableFile() 0 4 1
A testFileWriterWithNonWriteableFile() 0 8 1
A testFileReader() 0 9 1
A testFileWriterWithCreatedFile() 0 10 1
A testFileWriterWithOverwrittenFile() 0 10 1
A testFileWriterWithRecursiveDirectoryCreation() 0 13 1
A testFileWriterWithoutDirectoryCreation() 0 12 1
A testFileReaderWriterWithCreatedFile() 0 10 1
A setUp() 0 5 1
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\Ports\Kernel;
40
    use Apparat\Kernel\Tests\AbstractTest;
41
    use Apparat\Resource\Infrastructure\Io\File\InvalidArgumentException;
42
    use Apparat\Resource\Infrastructure\Io\File\Reader;
43
    use Apparat\Resource\Infrastructure\Io\File\ReaderWriter;
44
    use Apparat\Resource\Infrastructure\Io\File\Writer;
45
    use Apparat\Resource\Infrastructure\Model\Resource\TextResource;
46
47
    /**
48
     * FileIo tests
49
     *
50
     * @package     Apparat\Resource
51
     * @subpackage  Apparat\Resource\Tests
52
     */
53
    class FileIoTest extends AbstractTest
54
    {
55
        /**
56
         * Example text file
57
         *
58
         * @var string
59
         */
60
        const TXT_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'cc0.txt';
61
        /**
62
         * Example text data
63
         *
64
         * @var string
65
         */
66
        protected $text = null;
67
68
        /**
69
         * Tears down the fixture
70
         */
71
        public function tearDown()
72
        {
73
            putenv('MOCK_IS_READABLE');
74
            putenv('MOCK_IS_WRITEABLE');
75
            parent::tearDown();
76
        }
77
78
        /**
79
         * Test the file reader with an invalid file path
80
         *
81
         * @expectedException InvalidArgumentException
82
         * @expectedExceptionCode 1447616824
83
         */
84
        public function testFileReaderWithInvalidFilepath()
85
        {
86
            Kernel::create(Reader::class, [self::TXT_FILE.'_invalid']);
87
        }
88
89
        /**
90
         * Test the file reader with a directory path
91
         *
92
         * @expectedException InvalidArgumentException
93
         * @expectedExceptionCode 1447618938
94
         */
95
        public function testFileReaderWithDirectory()
96
        {
97
            Kernel::create(Reader::class, [dirname(self::TXT_FILE)]);
98
        }
99
100
        /**
101
         * Test the file reader with an unreadable file
102
         *
103
         * @expectedException InvalidArgumentException
104
         * @expectedExceptionCode 1447617006
105
         */
106
        public function testFileReaderWithUnreadableFile()
107
        {
108
            putenv('MOCK_IS_READABLE=1');
109
            Kernel::create(Reader::class, [self::TXT_FILE]);
110
        }
111
112
        /**
113
         * Test the file reader
114
         */
115
        public function testFileReader()
116
        {
117
            $fileReader = Kernel::create(Reader::class, [self::TXT_FILE]);
118
            $this->assertInstanceOf(Reader::class, $fileReader);
119
            $textResource = Kernel::create(TextResource::class, [$fileReader]);
120
            $inMemoryWriter = Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Writer::class);
121
            $textResource->dump($inMemoryWriter);
122
            $this->assertEquals($this->text, $inMemoryWriter->getData());
123
        }
124
125
        /**
126
         * Test the file writer with invalid writer options
127
         *
128
         * @expectedException InvalidArgumentException
129
         * @expectedExceptionCode 1447617559
130
         */
131
        public function testFileWriterWithInvalidOptions()
132
        {
133
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE, pow(2, 10)]);
134
        }
135
136
        /**
137
         * Test the file writer with non-creatable file
138
         *
139
         * @expectedException InvalidArgumentException
140
         * @expectedExceptionCode 1447617960
141
         */
142
        public function testFileWriterWithNonCreatableFile()
143
        {
144
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE.'_new', 0]);
145
        }
146
147
        /**
148
         * Test the file writer with non-overwriteable file
149
         *
150
         * @expectedException InvalidArgumentException
151
         * @expectedExceptionCode 1447617979
152
         */
153
        public function testFileWriterWithNonOverwriteableFile()
154
        {
155
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE, 0]);
156
        }
157
158
        /**
159
         * Test the file writer with non-writeable file
160
         *
161
         * @expectedException InvalidArgumentException
162
         * @expectedExceptionCode 1447617979
163
         */
164
        public function testFileWriterWithNonWriteableFile()
165
        {
166
            putenv('MOCK_IS_WRITEABLE=1');
167
            Kernel::create(
168
                \Apparat\Resource\Infrastructure\Io\File\Writer::class,
169
                [self::TXT_FILE, Writer::FILE_OVERWRITE]
170
            );
171
        }
172
173
        /**
174
         * Test the file writer with a newly created file
175
         */
176
        public function testFileWriterWithCreatedFile()
177
        {
178
            $textResource = Kernel::create(
179
                TextResource::class,
180
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
181
            );
182
            $tempFile = $this->createTemporaryFileName();
183
            $textResource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_CREATE]));
184
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
185
        }
186
187
        /**
188
         * Test the file writer with an overwritten file
189
         */
190
        public function testFileWriterWithOverwrittenFile()
191
        {
192
            $textResource = Kernel::create(
193
                TextResource::class,
194
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
195
            );
196
            $tempFile = $this->createTemporaryFile();
197
            $textResource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_OVERWRITE]));
198
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
199
        }
200
201
        /**
202
         * Test the file writer with recursive directory creation
203
         */
204
        public function testFileWriterWithRecursiveDirectoryCreation()
205
        {
206
            $textResource = Kernel::create(
207
                TextResource::class,
208
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
209
            );
210
            $tempFile = $this->createTemporaryFile();
211
            unlink($tempFile);
212
            $this->tmpFiles[] = $tempFile .= DIRECTORY_SEPARATOR.'recursive.txt';
213
            $textResource->dump(Kernel::create(Writer::class,
214
                [$tempFile, Writer::FILE_CREATE | Writer::FILE_CREATE_DIRS]));
215
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
216
        }
217
218
        /**
219
         * Test the file writer with recursive directory creation
220
         *
221
         * @expectedException InvalidArgumentException
222
         * @expectedExceptionCode 1461448384
223
         */
224
        public function testFileWriterWithoutDirectoryCreation()
225
        {
226
            $textResource = Kernel::create(
227
                TextResource::class,
228
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
229
            );
230
            $tempFile = $this->createTemporaryFile();
231
            unlink($tempFile);
232
            $this->tmpFiles[] = $tempFile .= DIRECTORY_SEPARATOR.'recursive.txt';
233
            $textResource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_CREATE]));
234
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
235
        }
236
237
        /**
238
         * Test the file reader/writer
239
         */
240
        public function testFileReaderWriterWithCreatedFile()
241
        {
242
            $tempFile = $this->createTemporaryFileName();
243
            copy(self::TXT_FILE, $tempFile);
244
            $randomAppend = md5(rand());
245
            $fileReaderWriter = Kernel::create(ReaderWriter::class, [$tempFile]);
246
            $textResource = Kernel::create(TextResource::class, [$fileReaderWriter]);
247
            $textResource->appendPart($randomAppend)->dump($fileReaderWriter);
248
            $this->assertStringEqualsFile($tempFile, $this->text.$randomAppend);
249
        }
250
251
        /**
252
         * Sets up the fixture
253
         */
254
        protected function setUp()
255
        {
256
            parent::setUp();
257
            $this->text = file_get_contents(self::TXT_FILE);
258
        }
259
    }
260
}
261
262
namespace Apparat\Resource\Infrastructure\Io\File {
263
264
    /**
265
     * Mocked version of the native is_readable() function
266
     *
267
     * @param $filename
268
     * @return bool
269
     */
270
    function is_readable($filename)
271
    {
272
        return (getenv('MOCK_IS_READABLE') != 1) ? \is_readable($filename) : false;
273
    }
274
275
    /**
276
     * Mocked version of the native is_writeable() function
277
     *
278
     * @param $filename
279
     * @return bool
280
     */
281
    function is_writeable($filename)
282
    {
283
        return (getenv('MOCK_IS_WRITEABLE') != 1) ? \is_writeable($filename) : false;
284
    }
285
}
286