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

TokenizerTest::shouldTokenizeObjectWithArray()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 17

Duplication

Lines 25
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 25
loc 25
rs 8.8571
cc 1
eloc 17
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 PHPUnit\Framework\TestCase;
29
use WSDL\Lexer\Token;
30
use WSDL\Lexer\Tokenizer;
31
32
/**
33
 * TokenizerTest
34
 *
35
 * @author Piotr Olaszewski <[email protected]>
36
 */
37
class TokenizerTest extends TestCase
38
{
39
    /**
40
     * @test
41
     */
42 View Code Duplication
    public function shouldTokenizeSimpleType()
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...
43
    {
44
        //given
45
        $param = 'int $age';
46
        $tokenizer = new Tokenizer();
47
48
        //when
49
        $tokens = $tokenizer->lex($param);
50
51
        //then
52
        Assert::thatArray($tokens)
53
            ->extracting('getName()', 'getValue()')
54
            ->containsExactly(
55
                [Token::TYPE, 'int'],
56
                [Token::NAME, '$age'],
57
                [Token::EOF, 'eof']
58
            );
59
    }
60
61
    /**
62
     * @test
63
     */
64 View Code Duplication
    public function shouldTokenizeSimpleTypeWithArray()
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...
65
    {
66
        //given
67
        $param = 'string[] $name';
68
        $tokenizer = new Tokenizer();
69
70
        //when
71
        $tokens = $tokenizer->lex($param);
72
73
        //then
74
        Assert::thatArray($tokens)
75
            ->extracting('getName()', 'getValue()')
76
            ->containsExactly(
77
                [Token::TYPE, 'string'],
78
                [Token::ARRAYS, '[]'],
79
                [Token::NAME, '$name'],
80
                [Token::EOF, 'eof']
81
            );
82
    }
83
84
    /**
85
     * @test
86
     */
87 View Code Duplication
    public function shouldTokenizeObject()
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...
88
    {
89
        //given
90
        $param = 'object $name {string $firstName int $age}';
91
        $tokenizer = new Tokenizer();
92
93
        //when
94
        $tokens = $tokenizer->lex($param);
95
96
        //then
97
        Assert::thatArray($tokens)
98
            ->extracting('getName()', 'getValue()')
99
            ->containsExactly(
100
                [Token::TYPE, 'object'],
101
                [Token::NAME, '$name'],
102
                [Token::OPEN_OBJECT, '{'],
103
                [Token::TYPE, 'string'],
104
                [Token::NAME, '$firstName'],
105
                [Token::TYPE, 'int'],
106
                [Token::NAME, '$age'],
107
                [Token::CLOSE_OBJECT, '}'],
108
                [Token::EOF, 'eof']
109
            );
110
    }
111
112
    /**
113
     * @test
114
     */
115 View Code Duplication
    public function shouldTokenizeObjectWithArray()
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...
116
    {
117
        //given
118
        $param = 'object[] $name {string $firstName int $age}';
119
        $tokenizer = new Tokenizer();
120
121
        //when
122
        $tokens = $tokenizer->lex($param);
123
124
        //then
125
        Assert::thatArray($tokens)
126
            ->extracting('getName()', 'getValue()')
127
            ->containsExactly(
128
                [Token::TYPE, 'object'],
129
                [Token::ARRAYS, '[]'],
130
                [Token::NAME, '$name'],
131
                [Token::OPEN_OBJECT, '{'],
132
                [Token::TYPE, 'string'],
133
                [Token::NAME, '$firstName'],
134
                [Token::TYPE, 'int'],
135
                [Token::NAME, '$age'],
136
                [Token::CLOSE_OBJECT, '}'],
137
                [Token::EOF, 'eof']
138
            );
139
    }
140
141
    /**
142
     * @test
143
     */
144
    public function shouldTokenizeNestedObjects()
145
    {
146
        //given
147
        $param = 'object $name { 
148
            object $user{
149
                string $firstName
150
                int $age
151
            }
152
            int $count
153
        }';
154
        $tokenizer = new Tokenizer();
155
156
        //when
157
        $tokens = $tokenizer->lex($param);
158
159
        //then
160
        Assert::thatArray($tokens)
161
            ->extracting('getName()', 'getValue()')
162
            ->containsExactly(
163
                [Token::TYPE, 'object'],
164
                [Token::NAME, '$name'],
165
                [Token::OPEN_OBJECT, '{'],
166
                [Token::TYPE, 'object'],
167
                [Token::NAME, '$user'],
168
                [Token::OPEN_OBJECT, '{'],
169
                [Token::TYPE, 'string'],
170
                [Token::NAME, '$firstName'],
171
                [Token::TYPE, 'int'],
172
                [Token::NAME, '$age'],
173
                [Token::CLOSE_OBJECT, '}'],
174
                [Token::TYPE, 'int'],
175
                [Token::NAME, '$count'],
176
                [Token::CLOSE_OBJECT, '}'],
177
                [Token::EOF, 'eof']
178
            );
179
    }
180
181
    /**
182
     * @test
183
     */
184 View Code Duplication
    public function shouldTokenizeClassWrapper()
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...
185
    {
186
        //given
187
        $param = 'object $name { 
188
            className \Foo\Bar\Baz
189
        }';
190
        $tokenizer = new Tokenizer();
191
192
        //when
193
        $tokens = $tokenizer->lex($param);
194
195
        //then
196
        Assert::thatArray($tokens)
197
            ->extracting('getName()', 'getValue()')
198
            ->containsExactly(
199
                [Token::TYPE, 'object'],
200
                [Token::NAME, '$name'],
201
                [Token::OPEN_OBJECT, '{'],
202
                [Token::TYPE, 'className'],
203
                [Token::CLASS_NAME, '\Foo\Bar\Baz'],
204
                [Token::CLOSE_OBJECT, '}'],
205
                [Token::EOF, 'eof']
206
            );
207
    }
208
209
    /**
210
     * @test
211
     */
212 View Code Duplication
    public function shouldTokenizeClassWrapperOneLine()
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...
213
    {
214
        //given
215
        $param = 'object $name { className \Foo\Bar\Baz }';
216
        $tokenizer = new Tokenizer();
217
218
        //when
219
        $tokens = $tokenizer->lex($param);
220
221
        //then
222
        Assert::thatArray($tokens)
223
            ->extracting('getName()', 'getValue()')
224
            ->containsExactly(
225
                [Token::TYPE, 'object'],
226
                [Token::NAME, '$name'],
227
                [Token::OPEN_OBJECT, '{'],
228
                [Token::TYPE, 'className'],
229
                [Token::CLASS_NAME, '\Foo\Bar\Baz'],
230
                [Token::CLOSE_OBJECT, '}'],
231
                [Token::EOF, 'eof']
232
            );
233
    }
234
235
    /**
236
     * @test
237
     */
238 View Code Duplication
    public function shouldTokenizeClassWrapperWithDuplicatedBackSlash()
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...
239
    {
240
        //given
241
        $param = 'object $name { className \\Foo\\Bar\\Baz }';
242
        $tokenizer = new Tokenizer();
243
244
        //when
245
        $tokens = $tokenizer->lex($param);
246
247
        //then
248
        Assert::thatArray($tokens)
249
            ->extracting('getName()', 'getValue()')
250
            ->containsExactly(
251
                [Token::TYPE, 'object'],
252
                [Token::NAME, '$name'],
253
                [Token::OPEN_OBJECT, '{'],
254
                [Token::TYPE, 'className'],
255
                [Token::CLASS_NAME, '\\Foo\\Bar\\Baz'],
256
                [Token::CLOSE_OBJECT, '}'],
257
                [Token::EOF, 'eof']
258
            );
259
    }
260
261
    /**
262
     * @test
263
     */
264 View Code Duplication
    public function shouldTokenizeClassWrapperWithoutFirstBackSlash()
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...
265
    {
266
        //given
267
        $param = 'object $name { className Foo\Bar\Baz }';
268
        $tokenizer = new Tokenizer();
269
270
        //when
271
        $tokens = $tokenizer->lex($param);
272
273
        //then
274
        Assert::thatArray($tokens)
275
            ->extracting('getName()', 'getValue()')
276
            ->containsExactly(
277
                [Token::TYPE, 'object'],
278
                [Token::NAME, '$name'],
279
                [Token::OPEN_OBJECT, '{'],
280
                [Token::TYPE, 'className'],
281
                [Token::CLASS_NAME, 'Foo\Bar\Baz'],
282
                [Token::CLOSE_OBJECT, '}'],
283
                [Token::EOF, 'eof']
284
            );
285
    }
286
287
    /**
288
     * @test
289
     */
290
    public function shouldThrowExceptionWhenOccursUnexpectedCharacter()
291
    {
292
        //given
293
        $param = 'string [$message';
294
        $tokenizer = new Tokenizer();
295
296
        //when
297
        CatchException::when($tokenizer)->lex($param);
298
299
        //then
300
        CatchException::assertThat()->hasMessage('Unexpected character: >[< offset >7<');
301
    }
302
}
303