testParseSimpleHitchWithValidKey()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 18
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 11
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\Document\TextnodeHitch;
23
use DembeloMain\Model\Repository\TextNodeRepositoryInterface;
24
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
25
26
/**
27
 * Class HitchParserTest
28
 */
29
class HitchParserTest extends WebTestCase
30
{
31
    /**
32
     * @var HitchParser
33
     */
34
    private $hitchParser;
35
36
    /**
37
     * @var TextNodeRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
38
     */
39
    private $textnodeRepositoryMock;
40
41
    /**
42
     * @inheritdoc
43
     */
44
    public function setUp(): void
45
    {
46
        parent::setUp();
47
        $this->textnodeRepositoryMock = $this->createTextnodeRepositoryMock();
48
        $this->hitchParser = new HitchParser($this->textnodeRepositoryMock);
49
    }
50
51
    /**
52
     * @expectedException \Exception
53
     *
54
     * @throws \Exception
55
     */
56
    public function testParseDoubleArrowRightWithEmptyLeftPart(): void
57
    {
58
        $content = '--&gt;textnodeId';
59
        $name = 'someName';
60
        $this->hitchParser->parseDoubleArrowRight($content, $name);
61
    }
62
63
    /**
64
     * @expectedException \Exception
65
     *
66
     * @throws \Exception
67
     */
68
    public function testParseDoubleArrowRightWithEmptyRightPart(): void
69
    {
70
        $content = 'description--&gt;';
71
        $name = 'someName';
72
        $this->hitchParser->parseDoubleArrowRight($content, $name);
73
    }
74
75
    /**
76
     * @expectedException \Exception
77
     *
78
     * @throws \Exception
79
     */
80
    public function testParseDoubleArrowRightWithNoValidTextnodeIdOnRightPart(): void
81
    {
82
        $content = 'description--&gt;invalidTextnodeId';
83
        $name = 'someName';
84
85
        $this->textnodeRepositoryMock->expects(self::once())
86
            ->method('find')
87
            ->with('invalidTextnodeId')
88
            ->willReturn(null);
89
90
        $this->hitchParser->parseDoubleArrowRight($content, $name);
91
    }
92
93
    /**
94
     * tests parseDoubleArrowRight with a valid textnode id
95
     * @throws \Exception
96
     */
97
    public function testParseDoubleArrowRightWithValidTextnodeIdOnRightPart(): void
98
    {
99
        $content = 'description--&gt;textnodeId';
100
        $twineName = 'bar';
101
102
        $targetTextnode = new Textnode();
103
104
        $this->textnodeRepositoryMock->expects(self::once())
105
            ->method('find')
106
            ->with('textnodeId')
107
            ->willReturn($targetTextnode);
108
109
        $result = $this->hitchParser->parseDoubleArrowRight($content, $twineName);
110
        self::assertInstanceOf(TextnodeHitch::class, $result);
111
        self::assertEquals('description', $result->getDescription());
112
        self::assertSame($targetTextnode, $result->getTargetTextnode());
113
        self::assertEquals(TextnodeHitch::STATUS_ACTIVE, $result->getStatus());
114
    }
115
116
    /**
117
     * @expectedException \Exception
118
     *
119
     * @throws \Exception
120
     */
121
    public function testSingleArrowRightWithEmptyLeftPart(): void
122
    {
123
        $content = '-&gt;nodeName';
124
        $name = 'someName';
125
126
        $this->hitchParser->parseSingleArrowRight($content, $name);
127
    }
128
129
    /**
130
     * @expectedException \Exception
131
     *
132
     * @throws \Exception
133
     */
134
    public function testSingleArrowRightWithEmptyRightPart(): void
135
    {
136
        $content = 'description-&gt;';
137
        $name = 'someName';
138
139
        $this->hitchParser->parseSingleArrowRight($content, $name);
140
    }
141
142
    /**
143
     * @expectedException \Exception
144
     *
145
     * @throws \Exception
146
     */
147
    public function testSingleArrowRightWithInvalidMapName(): void
148
    {
149
        $content = 'description-&gt;name';
150
        $name = 'someName';
151
152
        $mapping = [
153
            'invalidKey' => 'textNodeId',
154
        ];
155
156
        $this->hitchParser->setNodeNameMapping($mapping);
157
158
        $this->hitchParser->parseSingleArrowRight($content, $name);
159
    }
160
161
    /**
162
     * tests singleArrowRight with a valid map name
163
     * @throws \Exception
164
     */
165
    public function testSingleArrowRightWithValidMapName(): void
166
    {
167
        $content = 'description-&gt;key';
168
        $name = 'someName';
169
170
        $targetTextnode = new Textnode();
171
172
        $mapping = [
173
            'key' => $targetTextnode,
174
        ];
175
176
        $this->hitchParser->setNodeNameMapping($mapping);
177
178
        $result = $this->hitchParser->parseSingleArrowRight($content, $name);
179
        self::assertInstanceOf(TextnodeHitch::class, $result);
180
        self::assertEquals('description', $result->getDescription());
181
        self::assertEquals($targetTextnode, $result->getTargetTextnode());
182
        self::assertEquals(TextnodeHitch::STATUS_ACTIVE, $result->getStatus());
183
    }
184
185
    /**
186
     * @expectedException \Exception
187
     *
188
     * @throws \Exception
189
     */
190
    public function testParseSingleArrowLeftWithEmptyLeftPart(): void
191
    {
192
        $content = '&lt;-description';
193
        $name = 'someName';
194
195
        $this->hitchParser->parseSingleArrowLeft($content, $name);
196
    }
197
198
    /**
199
     * @expectedException \Exception
200
     *
201
     * @throws \Exception
202
     */
203
    public function testParseSingleArrowLeftWithEmptyRightPart(): void
204
    {
205
        $content = 'mapKey&lt;-';
206
        $name = 'someName';
207
208
        $this->hitchParser->parseSingleArrowLeft($content, $name);
209
    }
210
211
    /**
212
     * @expectedException \Exception
213
     *
214
     * @throws \Exception
215
     */
216
    public function testParseSingleArrowLeftWithInvalidKey(): void
217
    {
218
        $content = 'mapKey&lt;-description';
219
        $name = 'someName';
220
        $keyMap = [
221
            'invalidKey' => 'textnodeId',
222
        ];
223
224
        $this->hitchParser->setNodeNameMapping($keyMap);
225
226
        $this->hitchParser->parseSingleArrowLeft($content, $name);
227
    }
228
229
    /**
230
     * tests parseSingleArrowLeft with a valid key
231
     * @throws \Exception
232
     */
233
    public function testParseSingleArrowLeftWithValidKey(): void
234
    {
235
        $content = 'mapKey&lt;-description';
236
        $name = 'someName';
237
238
        $targetTextnode = new Textnode();
239
240
        $keyMap = [
241
            'mapKey' => $targetTextnode,
242
        ];
243
244
        $this->hitchParser->setNodeNameMapping($keyMap);
245
246
        $result = $this->hitchParser->parseSingleArrowLeft($content, $name);
247
248
        self::assertInstanceOf(TextnodeHitch::class, $result);
249
        self::assertEquals('description', $result->getDescription());
250
        self::assertEquals($targetTextnode, $result->getTargetTextnode());
251
        self::assertEquals(TextnodeHitch::STATUS_ACTIVE, $result->getStatus());
252
    }
253
254
    /**
255
     * @expectedException \Exception
256
     *
257
     * @throws \Exception
258
     */
259
    public function testParseSimpleHitchWithEmpyContent(): void
260
    {
261
        $content = '';
262
        $name = 'someName';
263
264
        $this->hitchParser->parseSimpleHitch($content, $name);
265
    }
266
267
    /**
268
     * @return void
269
     *
270
     * @expectedException \Exception
271
     *
272
     * @throws \Exception
273
     */
274
    public function testParseSimpleHitchWithInvalidKey(): void
275
    {
276
        $content = 'invalidKey';
277
        $name = 'someName';
278
        $keyMap = [
279
            'mapKey' => 'textnodeId',
280
        ];
281
282
        $this->hitchParser->setNodeNameMapping($keyMap);
283
        $this->hitchParser->parseSimpleHitch($content, $name);
284
    }
285
286
    /**
287
     * tests parseSimpleHitch with a valid key
288
     * @throws \Exception
289
     */
290
    public function testParseSimpleHitchWithValidKey(): void
291
    {
292
        $content = 'mapKey';
293
        $name = 'someName';
294
295
        $targetTextnode = new Textnode();
296
297
        $keyMap = [
298
            'mapKey' => $targetTextnode,
299
        ];
300
301
        $this->hitchParser->setNodeNameMapping($keyMap);
302
        $result = $this->hitchParser->parseSimpleHitch($content, $name);
303
304
        self::assertInstanceOf(TextnodeHitch::class, $result);
305
        self::assertEquals('mapKey', $result->getDescription());
306
        self::assertSame($targetTextnode, $result->getTargetTextnode());
307
        self::assertEquals(TextnodeHitch::STATUS_ACTIVE, $result->getStatus());
308
    }
309
310
    /**
311
     * @return \PHPUnit_Framework_MockObject_MockObject|TextNodeRepositoryInterface
312
     *
313
     * @throws \ReflectionException
314
     */
315
    private function createTextnodeRepositoryMock(): TextNodeRepositoryInterface
316
    {
317
        return $this->createMock(TextNodeRepositoryInterface::class);
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->createMock...sitoryInterface::class) returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return DembeloMain\Model\Reposi...NodeRepositoryInterface.
Loading history...
318
    }
319
}
320