Completed
Push — master ( 3ce016...a5eae9 )
by Joschi
02:38
created

FileIoTest::tearDown()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
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
            $textReource = Kernel::create(TextResource::class, [$fileReader]);
120
            $inMemoryWriter = Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Writer::class);
121
            $textReource->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
            $textReource = Kernel::create(
179
                TextResource::class,
180
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
181
            );
182
            $tempFile = $this->createTemporaryFileName();
183
            $textReource->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
            $textReource = Kernel::create(
193
                TextResource::class,
194
                [Kernel::create(\Apparat\Resource\Infrastructure\Io\InMemory\Reader::class, [$this->text])]
195
            );
196
            $tempFile = $this->createTemporaryFile();
197
            $textReource->dump(Kernel::create(Writer::class, [$tempFile, Writer::FILE_OVERWRITE]));
198
            $this->assertFileEquals(self::TXT_FILE, $tempFile);
199
        }
200
201
        /**
202
         * Test the file reader/writer
203
         */
204
        public function testFileReaderWriterWithCreatedFile()
205
        {
206
            $tempFile = $this->createTemporaryFileName();
207
            copy(self::TXT_FILE, $tempFile);
208
            $randomAppend = md5(rand());
209
            $fileReaderWriter = Kernel::create(ReaderWriter::class, [$tempFile]);
210
            $textReource = Kernel::create(TextResource::class, [$fileReaderWriter]);
211
            $textReource->appendPart($randomAppend)->dump($fileReaderWriter);
212
            $this->assertStringEqualsFile($tempFile, $this->text.$randomAppend);
213
        }
214
215
        /**
216
         * Sets up the fixture
217
         */
218
        protected function setUp()
219
        {
220
            parent::setUp();
221
            $this->text = file_get_contents(self::TXT_FILE);
222
        }
223
    }
224
}
225
226
namespace Apparat\Resource\Infrastructure\Io\File {
227
228
    /**
229
     * Mocked version of the native is_readable() function
230
     *
231
     * @param $filename
232
     * @return bool
233
     */
234
    function is_readable($filename)
235
    {
236
        return (getenv('MOCK_IS_READABLE') != 1) ? \is_readable($filename) : false;
237
    }
238
239
    /**
240
     * Mocked version of the native is_writeable() function
241
     *
242
     * @param $filename
243
     * @return bool
244
     */
245
    function is_writeable($filename)
246
    {
247
        return (getenv('MOCK_IS_WRITEABLE') != 1) ? \is_writeable($filename) : false;
248
    }
249
}
250