FileIoTest   A
last analyzed

Complexity

Total Complexity 15

Size/Duplication

Total Lines 208
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 208
rs 10
c 0
b 0
f 0
wmc 15
lcom 1
cbo 1

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 testFileReader() 0 9 1
A testFileWriterWithInvalidOptions() 0 4 1
A testFileWriterWithNonCreatableFile() 0 4 1
A testFileWriterWithNonOverwriteableFile() 0 4 1
A testFileWriterWithNonWriteableFile() 0 8 1
A testFileWriterWithCreatedFile() 0 10 1
A testFileWriterWithOverwrittenFile() 0 10 1
A testFileWriterWithRecursiveDirectoryCreation() 0 14 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\Dev\Tests\AbstractTest;
40
    use Apparat\Kernel\Ports\Kernel;
41
    use Apparat\Resource\Infrastructure\Io\File\Reader;
42
    use Apparat\Resource\Infrastructure\Io\File\ReaderWriter;
43
    use Apparat\Resource\Infrastructure\Io\File\Writer;
44
    use Apparat\Resource\Infrastructure\Model\Resource\TextResource;
45
46
    /**
47
     * FileIo tests
48
     *
49
     * @package     Apparat\Resource
50
     * @subpackage  Apparat\Resource\Tests
51
     */
52
    class FileIoTest extends AbstractTest
53
    {
54
        /**
55
         * Example text file
56
         *
57
         * @var string
58
         */
59
        const TXT_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'cc0.txt';
60
        /**
61
         * Example text data
62
         *
63
         * @var string
64
         */
65
        protected $text = null;
66
67
        /**
68
         * Tears down the fixture
69
         */
70
        public function tearDown()
71
        {
72
            putenv('MOCK_IS_READABLE');
73
            putenv('MOCK_IS_WRITEABLE');
74
            parent::tearDown();
75
        }
76
77
        /**
78
         * Test the file reader with an invalid file path
79
         *
80
         * @expectedException \Apparat\Resource\Ports\InvalidReaderArgumentException
81
         * @expectedExceptionCode 1447616824
82
         */
83
        public function testFileReaderWithInvalidFilepath()
84
        {
85
            Kernel::create(Reader::class, [self::TXT_FILE.'_invalid']);
86
        }
87
88
        /**
89
         * Test the file reader with a directory path
90
         *
91
         * @expectedException \Apparat\Resource\Ports\InvalidReaderArgumentException
92
         * @expectedExceptionCode 1447618938
93
         */
94
        public function testFileReaderWithDirectory()
95
        {
96
            Kernel::create(Reader::class, [dirname(self::TXT_FILE)]);
97
        }
98
99
        /**
100
         * Test the file reader with an unreadable file
101
         *
102
         * @expectedException \Apparat\Resource\Ports\InvalidReaderArgumentException
103
         * @expectedExceptionCode 1447617006
104
         */
105
        public function testFileReaderWithUnreadableFile()
106
        {
107
            putenv('MOCK_IS_READABLE=1');
108
            Kernel::create(Reader::class, [self::TXT_FILE]);
109
        }
110
111
        /**
112
         * Test the file reader
113
         */
114
        public function testFileReader()
115
        {
116
            $fileReader = Kernel::create(Reader::class, [self::TXT_FILE]);
117
            $this->assertInstanceOf(Reader::class, $fileReader);
118
            $textResource = Kernel::create(TextResource::class, [$fileReader]);
119
            $inMemoryWriter = Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Writer::class);
120
            $textResource->dump($inMemoryWriter);
121
            $this->assertEquals($this->text, $inMemoryWriter->getData());
122
        }
123
124
        /**
125
         * Test the file writer with invalid writer options
126
         *
127
         * @expectedException \Apparat\Resource\Ports\InvalidWriterArgumentException
128
         * @expectedExceptionCode 1447617559
129
         */
130
        public function testFileWriterWithInvalidOptions()
131
        {
132
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE, pow(2, 10)]);
133
        }
134
135
        /**
136
         * Test the file writer with non-creatable file
137
         *
138
         * @expectedException \Apparat\Resource\Ports\InvalidWriterArgumentException
139
         * @expectedExceptionCode 1447617960
140
         */
141
        public function testFileWriterWithNonCreatableFile()
142
        {
143
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE.'_new', 0]);
144
        }
145
146
        /**
147
         * Test the file writer with non-overwriteable file
148
         *
149
         * @expectedException \Apparat\Resource\Ports\InvalidWriterArgumentException
150
         * @expectedExceptionCode 1447617979
151
         */
152
        public function testFileWriterWithNonOverwriteableFile()
153
        {
154
            Kernel::create(\Apparat\Resource\Infrastructure\Io\File\Writer::class, [self::TXT_FILE, 0]);
155
        }
156
157
        /**
158
         * Test the file writer with non-writeable file
159
         *
160
         * @expectedException \Apparat\Resource\Ports\InvalidWriterArgumentException
161
         * @expectedExceptionCode 1447617979
162
         */
163
        public function testFileWriterWithNonWriteableFile()
164
        {
165
            putenv('MOCK_IS_WRITEABLE=1');
166
            Kernel::create(
167
                \Apparat\Resource\Infrastructure\Io\File\Writer::class,
168
                [self::TXT_FILE, Writer::FILE_OVERWRITE]
169
            );
170
        }
171
172
        /**
173
         * Test the file writer with a newly created file
174
         */
175
        public function testFileWriterWithCreatedFile()
176
        {
177
            $textResource = Kernel::create(
178
                TextResource::class,
179
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
180
            );
181
            $tempFile = $this->createTemporaryFileName();
182
            $textResource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_CREATE]));
183
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
184
        }
185
186
        /**
187
         * Test the file writer with an overwritten file
188
         */
189
        public function testFileWriterWithOverwrittenFile()
190
        {
191
            $textResource = Kernel::create(
192
                TextResource::class,
193
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
194
            );
195
            $tempFile = $this->createTemporaryFile();
196
            $textResource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_OVERWRITE]));
197
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
198
        }
199
200
        /**
201
         * Test the file writer with recursive directory creation
202
         */
203
        public function testFileWriterWithRecursiveDirectoryCreation()
204
        {
205
            $textResource = Kernel::create(
206
                TextResource::class,
207
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
208
            );
209
            $tempFile = $this->createTemporaryFile();
210
            unlink($tempFile);
211
            $this->tmpFiles[] = $tempFile .= DIRECTORY_SEPARATOR.'recursive.txt';
212
            $textResource->dump(
213
                Kernel::create(Writer::class, [$tempFile, Writer::FILE_CREATE | Writer::FILE_CREATE_DIRS])
214
            );
215
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
216
        }
217
218
        /**
219
         * Test the file writer with recursive directory creation
220
         *
221
         * @expectedException \Apparat\Resource\Ports\InvalidWriterArgumentException
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