Completed
Push — master ( fcfb84...acf2fd )
by Piotr
05:53
created

ParserTest::shouldParseObjectWithClassName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 19
rs 9.4285
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2013-2018
4
 * Piotr Olaszewski <[email protected]>
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
 * SOFTWARE.
23
 */
24
namespace Tests\WSDL\Lexer;
25
26
use Ouzo\Tests\Assert;
27
use Ouzo\Tests\CatchException;
28
use Ouzo\Utilities\Arrays;
29
use PHPUnit\Framework\TestCase;
30
use WSDL\Lexer\Tokenizer;
31
use WSDL\Parser\Node;
32
use WSDL\Parser\Parser;
33
34
/**
35
 * ParserTest
36
 *
37
 * @author Piotr Olaszewski <[email protected]>
38
 */
39
class ParserTest extends TestCase
40
{
41
    /**
42
     * @test
43
     */
44 View Code Duplication
    public function shouldParseSimple()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        //given
47
        $parser = $this->parser('int $age');
48
49
        //when
50
        $nodes = $parser->S();
51
52
        //then
53
        Assert::thatArray($nodes)
54
            ->extracting('type', 'name', 'isArray', 'elements')
55
            ->containsExactly(['int', '$age', false, []]);
56
    }
57
58
    /**
59
     * @test
60
     */
61 View Code Duplication
    public function shouldParseSimpleArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
62
    {
63
        //given
64
        $parser = $this->parser('int[] $age');
65
66
        //when
67
        $nodes = $parser->S();
68
69
        //then
70
        Assert::thatArray($nodes)
71
            ->extracting('type', 'name', 'isArray', 'elements')
72
            ->containsExactly(['int', '$age', true, []]);
73
    }
74
75
    /**
76
     * @test
77
     */
78 View Code Duplication
    public function shouldParseObject()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
79
    {
80
        //given
81
        $parser = $this->parser('object $user { string $name bool $isActive }');
82
83
        //when
84
        $nodes = $parser->S();
85
86
        //then
87
        Assert::thatArray($nodes)
88
            ->extracting('type', 'name', 'isArray')
89
            ->containsExactly(['object', '$user', false]);
90
91
        /** @var Node $node */
92
        $node = Arrays::first($nodes);
93
        Assert::thatArray($node->getElements())
94
            ->extracting('type', 'name', 'isArray', 'elements')
95
            ->containsExactly(
96
                ['string', '$name', false, []],
97
                ['bool', '$isActive', false, []]
98
            );
99
    }
100
101
    /**
102
     * @test
103
     */
104 View Code Duplication
    public function shouldParseObjectArray()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
105
    {
106
        //given
107
        $parser = $this->parser('object[] $user { string $name bool $isActive }');
108
109
        //when
110
        $nodes = $parser->S();
111
112
        //then
113
        Assert::thatArray($nodes)
114
            ->extracting('type', 'name', 'isArray')
115
            ->containsExactly(['object', '$user', true]);
116
117
        /** @var Node $node */
118
        $node = Arrays::first($nodes);
119
        Assert::thatArray($node->getElements())
120
            ->extracting('type', 'name', 'isArray', 'elements')
121
            ->containsExactly(
122
                ['string', '$name', false, []],
123
                ['bool', '$isActive', false, []]
124
            );
125
    }
126
127
    /**
128
     * @test
129
     */
130
    public function shouldParseSimpleAndObject()
131
    {
132
        //given
133
        $parser = $this->parser('int $age 
134
        object $user { string $name bool $isActive }');
135
136
        //when
137
        $nodes = $parser->S();
138
139
        //then
140
        Assert::thatArray($nodes)
141
            ->extracting('type', 'name', 'isArray')
142
            ->containsExactly(
143
                ['int', '$age', false],
144
                ['object', '$user', false]
145
            );
146
147
        /** @var Node $node */
148
        $node = $nodes[0];
149
        $this->assertEmpty($node->getElements());
150
151
        /** @var Node $node */
152
        $node = $nodes[1];
153
        Assert::thatArray($node->getElements())
154
            ->extracting('type', 'name', 'isArray', 'elements')
155
            ->containsExactly(
156
                ['string', '$name', false, []],
157
                ['bool', '$isActive', false, []]
158
            );
159
    }
160
161
    /**
162
     * @test
163
     */
164 View Code Duplication
    public function shouldParseObjectWithArrayInsideAttribute()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
165
    {
166
        //given
167
        $parser = $this->parser('object $user { string[] $name bool $isActive }');
168
169
        //when
170
        $nodes = $parser->S();
171
172
        //then
173
        Assert::thatArray($nodes)
174
            ->extracting('type', 'name', 'isArray')
175
            ->containsExactly(['object', '$user', false]);
176
177
        /** @var Node $node */
178
        $node = Arrays::first($nodes);
179
        Assert::thatArray($node->getElements())
180
            ->extracting('type', 'name', 'isArray', 'elements')
181
            ->containsExactly(
182
                ['string', '$name', true, []],
183
                ['bool', '$isActive', false, []]
184
            );
185
    }
186
187
    /**
188
     * @test
189
     */
190
    public function shouldParseObjectWithClassName()
191
    {
192
        //given
193
        $parser = $this->parser('object $user { className \Foo\Bar\Baz }');
194
195
        //when
196
        $nodes = $parser->S();
197
198
        //then
199
        Assert::thatArray($nodes)
200
            ->extracting('type', 'name', 'isArray')
201
            ->containsExactly(['object', '$user', false]);
202
203
        /** @var Node $node */
204
        $node = Arrays::first($nodes);
205
        Assert::thatArray($node->getElements())
206
            ->extracting('type', 'name', 'isArray', 'elements')
207
            ->containsExactly(['className', '\Foo\Bar\Baz', false, []]);
208
    }
209
210
    /**
211
     * @test
212
     */
213
    public function shouldThrowExceptionWhenSimpleTypeNotHaveType()
214
    {
215
        //given
216
        $parser = $this->parser('$name');
217
218
        //when
219
        CatchException::when($parser)->S();
220
221
        //then
222
        CatchException::assertThat()
223
            ->isInstanceOf('\WSDL\Parser\ParserException')
224
            ->hasMessage('Wrong type');
225
    }
226
227
    /**
228
     * @test
229
     */
230
    public function shouldThrowExceptionWhenSimpleTypeNotHaveName()
231
    {
232
        //given
233
        $parser = $this->parser('int');
234
235
        //when
236
        CatchException::when($parser)->S();
237
238
        //then
239
        CatchException::assertThat()
240
            ->isInstanceOf('\WSDL\Parser\ParserException')
241
            ->hasMessage('Wrong name');
242
    }
243
244
    /**
245
     * @test
246
     */
247
    public function shouldThrowExceptionWhenObjectNotHaveType()
248
    {
249
        //given
250
        $parser = $this->parser('$user { int $age }');
251
252
        //when
253
        CatchException::when($parser)->S();
254
255
        //then
256
        CatchException::assertThat()
257
            ->isInstanceOf('\WSDL\Parser\ParserException')
258
            ->hasMessage('Wrong type');
259
    }
260
261
    /**
262
     * @test
263
     */
264
    public function shouldThrowExceptionWhenObjectNotHaveName()
265
    {
266
        //given
267
        $parser = $this->parser('object { int $age }');
268
269
        //when
270
        CatchException::when($parser)->S();
271
272
        //then
273
        CatchException::assertThat()
274
            ->isInstanceOf('\WSDL\Parser\ParserException')
275
            ->hasMessage('Wrong name');
276
    }
277
278
    /**
279
     * @test
280
     */
281
    public function shouldThrowExceptionWhenInsideObjectAttributeNotHaveType()
282
    {
283
        //given
284
        $parser = $this->parser('object $user { $age }');
285
286
        //when
287
        CatchException::when($parser)->S();
288
289
        //then
290
        CatchException::assertThat()
291
            ->isInstanceOf('\WSDL\Parser\ParserException')
292
            ->hasMessage('Wrong type');
293
    }
294
295
    /**
296
     * @test
297
     */
298
    public function shouldThrowExceptionWhenInsideObjectAttributeNotHaveName()
299
    {
300
        //given
301
        $parser = $this->parser('object $user { int }');
302
303
        //when
304
        CatchException::when($parser)->S();
305
306
        //then
307
        CatchException::assertThat()
308
            ->isInstanceOf('\WSDL\Parser\ParserException')
309
            ->hasMessage('Wrong name');
310
    }
311
312
    /**
313
     * @test
314
     */
315
    public function shouldThrowExceptionWhenObjectNotHaveOpen()
316
    {
317
        //given
318
        $parser = $this->parser('object $user int $age }');
319
320
        //when
321
        CatchException::when($parser)->S();
322
323
        //then
324
        CatchException::assertThat()
325
            ->isInstanceOf('\WSDL\Parser\ParserException')
326
            ->hasMessage('Missing open object');
327
    }
328
329
    /**
330
     * @test
331
     */
332
    public function shouldThrowExceptionWhenObjectNotHaveOpenInNestedObject()
333
    {
334
        //given
335
        $parser = $this->parser('object $user { 
336
            int $age 
337
            object $role 
338
                int $id
339
                string $name
340
            }
341
        }');
342
343
        //when
344
        CatchException::when($parser)->S();
345
346
        //then
347
        CatchException::assertThat()
348
            ->isInstanceOf('\WSDL\Parser\ParserException')
349
            ->hasMessage('Missing open object');
350
    }
351
352
    /**
353
     * @test
354
     */
355
    public function shouldThrowExceptionWhenObjectNotHaveClose()
356
    {
357
        //given
358
        $parser = $this->parser('object $user { int $age ');
359
360
        //when
361
        CatchException::when($parser)->S();
362
363
        //then
364
        CatchException::assertThat()
365
            ->isInstanceOf('\WSDL\Parser\ParserException')
366
            ->hasMessage('Missing close object');
367
    }
368
369
    /**
370
     * @test
371
     */
372
    public function shouldThrowExceptionWhenObjectNotHaveCloseInNestedObject()
373
    {
374
        //given
375
        $parser = $this->parser('object $user { 
376
            int $age 
377
            object $role {
378
                int $id
379
                string $name
380
        }');
381
382
        //when
383
        CatchException::when($parser)->S();
384
385
        //then
386
        CatchException::assertThat()
387
            ->isInstanceOf('\WSDL\Parser\ParserException')
388
            ->hasMessage('Missing close object');
389
    }
390
391
    private function parser($string)
392
    {
393
        $tokenizer = new Tokenizer();
394
        $tokens = $tokenizer->lex($string);
395
        return new Parser($tokens);
396
    }
397
}
398