PassageDataParserTest::createParserContextMock()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 0
1
<?php
2
/* Copyright (C) 2017 Michael Giesler
3
 *
4
 * This file is part of Dembelo.
5
 *
6
 * Dembelo is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Affero General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * Dembelo is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Affero General Public License 3 for more details.
15
 *
16
 * You should have received a copy of the GNU Affero General Public License 3
17
 * along with Dembelo. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace AdminBundle\Service\TwineImport;
20
21
use DembeloMain\Document\Textnode;
22
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
23
use Doctrine\ODM\MongoDB\DocumentManager;
24
use PHPUnit\Framework\TestCase;
25
26
/**
27
 * Class PassageDataParserTest
28
 */
29
class PassageDataParserTest extends TestCase
30
{
31
    /**
32
     * @var \PHPUnit_Framework_MockObject_MockObject|TextNodeRepositoryInterface
33
     */
34
    private $textnodeRepositoryMock;
35
36
    /**
37
     * @var PassageDataParser
38
     */
39
    private $parser;
40
41
    /**
42
     * @var \PHPUnit_Framework_MockObject_MockObject|DocumentManager
43
     */
44
    private $documentManagerMock;
45
46
    /**
47
     * @return void
48
     */
49
    public function setUp(): void
50
    {
51
        $this->textnodeRepositoryMock = $this->createMock(TextNodeRepositoryInterface::class);
52
        $this->documentManagerMock = $this->createMock(DocumentManager::class);
53
        $this->parser = new PassageDataParser($this->textnodeRepositoryMock, $this->documentManagerMock);
54
    }
55
56
    /**
57
     * @return void
58
     */
59
    public function testStartElementForNewNonAccessTextnode(): void
60
    {
61
        $elementName = 'someTagName';
62
        $attributes = [
63
            'pid' => 1,
64
            'tags' => 'ID:someId',
65
            'name' => 'someName',
66
        ];
67
        $parserContext = $this->createParserContextMock();
68
        $parserContext->expects(self::once())
0 ignored issues
show
Bug introduced by
The method expects() does not exist on AdminBundle\Service\TwineImport\ParserContext. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

68
        $parserContext->/** @scrutinizer ignore-call */ 
69
                        expects(self::once())

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
69
            ->method('isTwineText')
70
            ->willReturn(false);
71
        $parserContext->expects(self::once())
72
            ->method('setTwineText')
73
            ->with(true);
74
        $parserContext->expects(self::any())
75
            ->method('getTextnodeMapping')
76
            ->willReturn([]);
77
        $parserContext->expects(self::any())
78
            ->method('getTwineStartnodeId')
79
            ->willReturn(-1);
80
        $parserContext->expects(self::never())
81
            ->method('setAccessSet');
82
        $parserContext->expects(self::any())
83
            ->method('setCurrentTextnode')
84
            ->willReturnCallback(function (Textnode $textnode) {
85
                self::assertEquals('someId', $textnode->getTwineId());
86
                self::assertFalse($textnode->getAccess());
87
                self::assertEquals('someName', $textnode->getMetadata()['Titel']);
88
            });
89
90
        $this->textnodeRepositoryMock->expects(self::once())
91
            ->method('findByTwineId')
92
            ->willReturn(null);
93
94
        $this->parser->setParserContext($parserContext);
95
        $this->parser->startElement($elementName, $attributes);
96
    }
97
98
    /**
99
     * @return void
100
     */
101
    public function testStartElementForOldNonAccessTextnode(): void
102
    {
103
        $elementName = 'someTagName';
104
        $attributes = [
105
            'pid' => 1,
106
            'tags' => 'ID:someId',
107
            'name' => 'someName',
108
        ];
109
110
        /* @var $textnodeMock \PHPUnit_Framework_MockObject_MockObject|Textnode */
111
        $textnodeMock = $this->createMock(Textnode::class);
112
        $textnodeMock->expects(self::once())
113
            ->method('setAccess')
114
            ->with(false);
115
116
        $parserContext = $this->createParserContextMock();
117
        $parserContext->expects(self::once())
118
            ->method('isTwineText')
119
            ->willReturn(false);
120
        $parserContext->expects(self::once())
121
            ->method('setTwineText')
122
            ->with(true);
123
        $parserContext->expects(self::any())
124
            ->method('getTextnodeMapping')
125
            ->willReturn([]);
126
        $parserContext->expects(self::any())
127
            ->method('getTwineStartnodeId')
128
            ->willReturn(-1);
129
        $parserContext->expects(self::never())
130
            ->method('setAccessSet');
131
        $parserContext->expects(self::any())
132
            ->method('setCurrentTextnode')
133
            ->willReturnCallback(function (Textnode $textnode) use ($textnodeMock) {
134
                self::assertSame($textnodeMock, $textnode);
135
            });
136
137
        $this->textnodeRepositoryMock->expects(self::once())
138
            ->method('findByTwineId')
139
            ->willReturn($textnodeMock);
140
141
        $this->parser->setParserContext($parserContext);
142
        $this->parser->startElement($elementName, $attributes);
143
    }
144
145
    /**
146
     * @return void
147
     */
148
    public function testStartElementForOldAccessTextnode(): void
149
    {
150
        $elementName = 'someTagName';
151
        $attributes = [
152
            'pid' => 1,
153
            'tags' => 'ID:someId',
154
            'name' => 'someName',
155
        ];
156
157
        /* @var $textnodeMock \PHPUnit_Framework_MockObject_MockObject|Textnode */
158
        $textnodeMock = $this->createMock(Textnode::class);
159
        $textnodeMock->expects(self::once())
160
            ->method('setAccess')
161
            ->with(true);
162
163
        $parserContext = $this->createParserContextMock();
164
        $parserContext->expects(self::once())
165
            ->method('isTwineText')
166
            ->willReturn(false);
167
        $parserContext->expects(self::once())
168
            ->method('setTwineText')
169
            ->with(true);
170
        $parserContext->expects(self::any())
171
            ->method('getTextnodeMapping')
172
            ->willReturn([]);
173
        $parserContext->expects(self::any())
174
            ->method('getTwineStartnodeId')
175
            ->willReturn(1);
176
        $parserContext->expects(self::once())
177
            ->method('setAccessSet')
178
            ->with(true);
179
        $parserContext->expects(self::any())
180
            ->method('setCurrentTextnode')
181
            ->willReturnCallback(function (Textnode $textnode) use ($textnodeMock) {
182
                self::assertSame($textnodeMock, $textnode);
183
            });
184
185
        $this->textnodeRepositoryMock->expects(self::once())
186
            ->method('findByTwineId')
187
            ->willReturn($textnodeMock);
188
189
        $this->parser->setParserContext($parserContext);
190
        $this->parser->startElement($elementName, $attributes);
191
    }
192
193
    /**
194
     * @return void
195
     *
196
     * @expectedException \Exception
197
     *
198
     * @expectedExceptionMessage Nested 'someTagName' found in Twine archive file ''
199
     */
200
    public function testStartElementForNestedPassageDatas(): void
201
    {
202
        $elementName = 'someTagName';
203
        $attributes = [
204
            'pid' => 1,
205
            'tags' => 'ID:someId',
206
            'name' => 'someName',
207
        ];
208
209
        $parserContext = $this->createParserContextMock();
210
        $parserContext->expects(self::once())
211
            ->method('isTwineText')
212
            ->willReturn(true);
213
        $this->parser->setParserContext($parserContext);
214
215
        $this->parser->startElement($elementName, $attributes);
216
    }
217
218
    /**
219
     * @return void
220
     *
221
     * @expectedException \Exception
222
     *
223
     * @expectedExceptionMessage There is a 'someTagName' in the Twine archive file '' which is missing its 'pid' attribute.
224
     */
225
    public function testStartElementForMissingPIDAttribute(): void
226
    {
227
        $elementName = 'someTagName';
228
        $attributes = [
229
            'tags' => 'ID:someId',
230
            'name' => 'someName',
231
        ];
232
233
        $parserContext = $this->createParserContextMock();
234
        $parserContext->expects(self::once())
235
            ->method('isTwineText')
236
            ->willReturn(false);
237
        $this->parser->setParserContext($parserContext);
238
239
        $this->parser->startElement($elementName, $attributes);
240
    }
241
242
    /**
243
     * @return void
244
     *
245
     * @expectedException \Exception
246
     *
247
     * @expectedExceptionMessage There is a 'someTagName' in the Twine archive file '' which hasn't a numeric value in its 'pid' attribute ('someNonNumericPid' was found instead).
248
     */
249
    public function testStartElementForNonNumericPIDAttribute(): void
250
    {
251
        $elementName = 'someTagName';
252
        $attributes = [
253
            'pid' => 'someNonNumericPid',
254
            'tags' => 'ID:someId',
255
            'name' => 'someName',
256
        ];
257
258
        $parserContext = $this->createParserContextMock();
259
        $parserContext->expects(self::once())
260
            ->method('isTwineText')
261
            ->willReturn(false);
262
        $this->parser->setParserContext($parserContext);
263
264
        $this->parser->startElement($elementName, $attributes);
265
    }
266
267
    /**
268
     * @return void
269
     *
270
     * @expectedException \Exception
271
     *
272
     * @expectedExceptionMessage There is a 'someTagName' in the Twine archive file '' which has a non unique 'id' tag [someId], in node 'someName'
273
     */
274
    public function testStartElementForNonUniquePIDAttribute(): void
275
    {
276
        $elementName = 'someTagName';
277
        $attributes = [
278
            'pid' => '2',
279
            'tags' => 'ID:someId',
280
            'name' => 'someName',
281
        ];
282
283
        $parserContext = $this->createParserContextMock();
284
        $parserContext->expects(self::once())
285
            ->method('isTwineText')
286
            ->willReturn(false);
287
        $parserContext->expects(self::once())
288
            ->method('getTextnodeMapping')
289
            ->willReturn(['someId' => 'someTextnodeId']);
290
        $this->parser->setParserContext($parserContext);
291
292
        $this->parser->startElement($elementName, $attributes);
293
    }
294
295
    /**
296
     * @return void
297
     *
298
     * @expectedException \Exception
299
     *
300
     * @expectedExceptionMessage no ID given for Textnode "someName"
301
     */
302
    public function testStartElementForMissingTwineIdTag(): void
303
    {
304
        $elementName = 'someTagName';
305
        $attributes = [
306
            'pid' => '2',
307
            'tags' => '',
308
            'name' => 'someName',
309
        ];
310
311
        $parserContext = $this->createParserContextMock();
312
        $parserContext->expects(self::once())
313
            ->method('isTwineText')
314
            ->willReturn(false);
315
        $this->parser->setParserContext($parserContext);
316
317
        $this->parser->startElement($elementName, $attributes);
318
    }
319
320
    /**
321
     * @return void
322
     *
323
     * @expectedException \Exception
324
     *
325
     * @expectedExceptionMessage no ID given for Textnode "someName"
326
     */
327
    public function testStartElementForInvalidTwineIdTag(): void
328
    {
329
        $elementName = 'someTagName';
330
        $attributes = [
331
            'pid' => '2',
332
            'tags' => 'nonempty',
333
            'name' => 'someName',
334
        ];
335
336
        $parserContext = $this->createParserContextMock();
337
        $parserContext->expects(self::once())
338
            ->method('isTwineText')
339
            ->willReturn(false);
340
        $this->parser->setParserContext($parserContext);
341
342
        $this->parser->startElement($elementName, $attributes);
343
    }
344
345
    /**
346
     * @return void
347
     */
348
    public function testEndElement(): void
349
    {
350
        $textnodeMock = $this->createMock(Textnode::class);
351
352
        $parserContext = $this->createParserContextMock();
353
        $parserContext->expects(self::any())
354
            ->method('getCurrentTextnode')
355
            ->willReturn($textnodeMock);
356
        $parserContext->expects(self::once())
357
            ->method('setTwineText')
358
            ->with(false);
359
        $this->parser->setParserContext($parserContext);
360
361
        $this->parser->endElement();
362
    }
363
364
    /**
365
     * @return \PHPUnit_Framework_MockObject_MockObject|ParserContext
366
     */
367
    private function createParserContextMock(): ParserContext
368
    {
369
        return $this->createMock(ParserContext::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock...t\ParserContext::class) returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return AdminBundle\Service\TwineImport\ParserContext.
Loading history...
370
    }
371
}
372