TextTest   A
last analyzed

Complexity

Total Complexity 18

Size/Duplication

Total Lines 216
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
dl 0
loc 216
rs 10
c 0
b 0
f 0
wmc 18
lcom 1
cbo 1

18 Methods

Rating   Name   Duplication   Size   Complexity  
A testTextResource() 0 6 1
A testTextResourceReader() 0 5 1
A testTextResourceLoad() 0 6 1
A testTextResourceInvalidMethod() 0 5 1
A testTextResourceSetPart() 0 7 1
A testTextResourceSet() 0 7 1
A testTextResourceGet() 0 5 1
A testTextResourceAppendPart() 0 8 1
A testTextResourceAppend() 0 8 1
A testTextResourcePrependPart() 0 8 1
A testTextResourcePrepend() 0 8 1
A testTextResourceInvalidPathIdentifier() 0 5 1
A testTextResourceAppendSubparts() 0 5 1
A testTextResourcePrependSubparts() 0 5 1
A testTextResourceMimeTypeSubparts() 0 5 1
A testTextResourceDump() 0 8 1
A testTextResourceReaderWriter() 0 8 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\Domain\Model\Part\InvalidArgumentException;
42
use Apparat\Resource\Domain\Model\Resource\RuntimeException;
43
use Apparat\Resource\Infrastructure\Io\InMemory\Reader;
44
use Apparat\Resource\Infrastructure\Io\InMemory\ReaderWriter;
45
use Apparat\Resource\Infrastructure\Io\InMemory\Writer;
46
use Apparat\Resource\Infrastructure\Model\Part\TextPart;
47
use Apparat\Resource\Infrastructure\Model\Resource\TextResource;
48
49
/**
50
 * Text file tests
51
 *
52
 * @package     Apparat\Resource
53
 * @subpackage  Apparat\Resource\Tests
54
 */
55
class TextTest extends AbstractTest
56
{
57
    /**
58
     * Example text file
59
     *
60
     * @var string
61
     */
62
    const TXT_FILE = __DIR__.DIRECTORY_SEPARATOR.'Fixture'.DIRECTORY_SEPARATOR.'cc0.txt';
63
    /**
64
     * Example text data
65
     *
66
     * @var string
67
     */
68
    protected $text = null;
69
70
    /**
71
     * Test the text file constructor
72
     */
73
    public function testTextResource()
74
    {
75
        $textReource = Kernel::create(TextResource::class, [null]);
76
        $this->assertInstanceOf(TextResource::class, $textReource);
77
        $this->assertEquals(TextPart::MEDIA_TYPE, $textReource->getMediaTypePart());
78
    }
79
80
    /**
81
     * Test the text file constructor with reader
82
     */
83
    public function testTextResourceReader()
84
    {
85
        $textReource = Kernel::create(TextResource::class, [Kernel::create(Reader::class, [$this->text])]);
86
        $this->assertEquals($this->text, $textReource->getPart());
87
    }
88
89
    /**
90
     * Test the text file constructor with explicit loading
91
     */
92
    public function testTextResourceLoad()
93
    {
94
        $textReource = Kernel::create(TextResource::class, [null]);
95
        $textReource->load(Kernel::create(Reader::class, [$this->text]));
96
        $this->assertEquals($this->text, $textReource->getPart());
97
    }
98
99
    /**
100
     * Test invalid file method
101
     *
102
     * @expectedException RuntimeException
103
     * @expectedExceptionCode 1447450449
104
     */
105
    public function testTextResourceInvalidMethod()
106
    {
107
        $textReource = Kernel::create(TextResource::class, [null]);
108
        $textReource->undefinedMethod();
109
    }
110
111
    /**
112
     * Test setting the content part of a text file
113
     */
114
    public function testTextResourceSetPart()
115
    {
116
        $randomSet = md5(rand());
117
        $textReource = Kernel::create(TextResource::class, [null]);
118
        $textReource->setPart($randomSet);
119
        $this->assertEquals($randomSet, $textReource->getPart());
120
    }
121
122
    /**
123
     * Test setting the content of a text file
124
     */
125
    public function testTextResourceSet()
126
    {
127
        $randomSet = md5(rand());
128
        $textReource = Kernel::create(TextResource::class, [null]);
129
        $textReource->set($randomSet);
130
        $this->assertEquals($randomSet, $textReource->getPart());
131
    }
132
133
    /**
134
     * Test getting the content of a text file
135
     */
136
    public function testTextResourceGet()
137
    {
138
        $textReource = Kernel::create(TextResource::class, [Kernel::create(Reader::class, [$this->text])]);
139
        $this->assertEquals($this->text, $textReource->get());
140
    }
141
142
    /**
143
     * Test setting and appending content to a text file part
144
     */
145
    public function testTextResourceAppendPart()
146
    {
147
        $randomSet = md5(rand());
148
        $randomAppend = md5(rand());
149
        $textReource = Kernel::create(TextResource::class, [null]);
150
        $textReource->setPart($randomSet)->appendPart($randomAppend);
151
        $this->assertEquals($randomSet.$randomAppend, $textReource->getPart());
152
    }
153
154
    /**
155
     * Test appending content to a text file
156
     */
157
    public function testTextResourceAppend()
158
    {
159
        $randomSet = md5(rand());
160
        $randomAppend = md5(rand());
161
        $textReource = Kernel::create(TextResource::class, [null]);
162
        $textReource->set($randomSet)->append($randomAppend);
163
        $this->assertEquals($randomSet.$randomAppend, $textReource->get());
164
    }
165
166
    /**
167
     * Test prepending content to a text file part
168
     */
169
    public function testTextResourcePrependPart()
170
    {
171
        $randomSet = md5(rand());
172
        $randomPrepend = md5(rand());
173
        $textReource = Kernel::create(TextResource::class, [null]);
174
        $textReource->setPart($randomSet)->prependPart($randomPrepend);
175
        $this->assertEquals($randomPrepend.$randomSet, $textReource->getPart());
176
    }
177
178
    /**
179
     * Test appending content to a text file
180
     */
181
    public function testTextResourcePrepend()
182
    {
183
        $randomSet = md5(rand());
184
        $randomPrepend = md5(rand());
185
        $textReource = Kernel::create(TextResource::class, [null]);
186
        $textReource->set($randomSet)->prepend($randomPrepend);
187
        $this->assertEquals($randomPrepend.$randomSet, $textReource->get());
188
    }
189
190
    /**
191
     * Test an invalid path identifier
192
     *
193
     * @expectedException InvalidArgumentException
194
     * @expectedExceptionCode 1447364401
195
     */
196
    public function testTextResourceInvalidPathIdentifier()
197
    {
198
        $textReource = Kernel::create(TextResource::class, [null]);
199
        $textReource->getPart('-');
200
    }
201
202
    /**
203
     * Test appending to the content of a text file with unallowed subparts
204
     *
205
     * @expectedException InvalidArgumentException
206
     * @expectedExceptionCode 1447365624
207
     */
208
    public function testTextResourceAppendSubparts()
209
    {
210
        $textReource = Kernel::create(TextResource::class, [null]);
211
        $textReource->appendPart(md5(rand()), 'a/b/c');
212
    }
213
214
    /**
215
     * Test prepending to the content of a text file with unallowed subparts
216
     *
217
     * @expectedException InvalidArgumentException
218
     * @expectedExceptionCode 1447365624
219
     */
220
    public function testTextResourcePrependSubparts()
221
    {
222
        $textReource = Kernel::create(TextResource::class, [null]);
223
        $textReource->prependPart(md5(rand()), 'a/b/c');
224
    }
225
226
    /**
227
     * Test getting the media type of a text file with unallowed subparts
228
     *
229
     * @expectedException InvalidArgumentException
230
     * @expectedExceptionCode 1447365624
231
     */
232
    public function testTextResourceMimeTypeSubparts()
233
    {
234
        $textReource = Kernel::create(TextResource::class, [null]);
235
        $textReource->getMediaTypePart('a/b/c');
236
    }
237
238
    /**
239
     * Test dumping the contents of a text file
240
     */
241
    public function testTextResourceDump()
242
    {
243
        $randomSet = md5(rand());
244
        $writer = Kernel::create(Writer::class);
245
        $textReource = Kernel::create(TextResource::class, [null]);
246
        $textReource->setPart($randomSet)->dump($writer);
247
        $this->assertEquals($randomSet, $writer->getData());
248
    }
249
250
    /**
251
     * Test the in-memory universal reader / writer
252
     */
253
    public function testTextResourceReaderWriter()
254
    {
255
        $randomAppend = md5(rand());
256
        $readerWriter = Kernel::create(ReaderWriter::class, [$this->text]);
257
        $textReource = Kernel::create(TextResource::class, [$readerWriter]);
258
        $textReource->appendPart($randomAppend)->dump($readerWriter);
259
        $this->assertEquals($this->text.$randomAppend, $readerWriter->getData());
260
    }
261
262
    /**
263
     * Sets up the fixture
264
     */
265
    protected function setUp()
266
    {
267
        parent::setUp();
268
        $this->text = file_get_contents(self::TXT_FILE);
269
    }
270
}
271