Completed
Push — master ( 30b823...2fbe7d )
by Piotr
03:23
created

shouldThrowExceptionWhenOccursUnexpectedCharacter()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
/**
3
 * Copyright (C) 2013-2016
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 PHPUnit_Framework_TestCase
38
{
39
    /**
40
     * @test
41
     */
42
    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, Token::NAME, Token::EOF],
56
                ['int', '$age', 'eof']
57
            );
58
    }
59
60
    /**
61
     * @test
62
     */
63
    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...
64
    {
65
        //given
66
        $param = 'string[] $name';
67
        $tokenizer = new Tokenizer();
68
69
        //when
70
        $tokens = $tokenizer->lex($param);
71
72
        //then
73
        Assert::thatArray($tokens)
74
            ->extracting('getName()', 'getValue()')
75
            ->containsExactly(
76
                [Token::TYPE, Token::ARRAYS, Token::NAME, Token::EOF],
77
                ['string', '[]', '$name', 'eof']
78
            );
79
    }
80
81
    /**
82
     * @test
83
     */
84
    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...
85
    {
86
        //given
87
        $param = 'object $name {string $firstName int $age}';
88
        $tokenizer = new Tokenizer();
89
90
        //when
91
        $tokens = $tokenizer->lex($param);
92
93
        //then
94
        Assert::thatArray($tokens)
95
            ->extracting('getName()', 'getValue()')
96
            ->containsExactly(
97
                [Token::TYPE, Token::NAME, Token::OPEN_OBJECT, Token::TYPE, Token::NAME, Token::TYPE, Token::NAME, Token::CLOSE_OBJECT, Token::EOF],
98
                ['object', '$name', '{', 'string', '$firstName', 'int', '$age', '}', 'eof']
99
            );
100
    }
101
102
    /**
103
     * @test
104
     */
105
    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...
106
    {
107
        //given
108
        $param = 'object[] $name {string $firstName int $age}';
109
        $tokenizer = new Tokenizer();
110
111
        //when
112
        $tokens = $tokenizer->lex($param);
113
114
        //then
115
        Assert::thatArray($tokens)
116
            ->extracting('getName()', 'getValue()')
117
            ->containsExactly(
118
                [Token::TYPE, Token::ARRAYS, Token::NAME, Token::OPEN_OBJECT, Token::TYPE, Token::NAME, Token::TYPE, Token::NAME, Token::CLOSE_OBJECT, Token::EOF],
119
                ['object', '[]', '$name', '{', 'string', '$firstName', 'int', '$age', '}', 'eof']
120
            );
121
    }
122
123
    /**
124
     * @test
125
     */
126
    public function shouldTokenizeNestedObjects()
127
    {
128
        //given
129
        $param = 'object $name { 
130
            object $user{
131
                string $firstName
132
                int $age
133
            }
134
            int $count
135
        }';
136
        $tokenizer = new Tokenizer();
137
138
        //when
139
        $tokens = $tokenizer->lex($param);
140
141
        //then
142
        Assert::thatArray($tokens)
143
            ->extracting('getName()', 'getValue()')
144
            ->containsExactly(
145
                [
146
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
147
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
148
                    Token::TYPE, Token::NAME,
149
                    Token::TYPE, Token::NAME,
150
                    Token::CLOSE_OBJECT,
151
                    Token::TYPE, Token::NAME,
152
                    Token::CLOSE_OBJECT,
153
                    Token::EOF
154
                ],
155
                ['object', '$name', '{', 'object', '$user', '{', 'string', '$firstName', 'int', '$age', '}', 'int', '$count', '}', 'eof']
156
            );
157
    }
158
159
    /**
160
     * @test
161
     */
162
    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...
163
    {
164
        //given
165
        $param = 'object $name { 
166
            className \Foo\Bar\Baz
167
        }';
168
        $tokenizer = new Tokenizer();
169
170
        //when
171
        $tokens = $tokenizer->lex($param);
172
173
        //then
174
        Assert::thatArray($tokens)
175
            ->extracting('getName()', 'getValue()')
176
            ->containsExactly(
177
                [
178
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
179
                    Token::TYPE, Token::CLASS_NAME,
180
                    Token::CLOSE_OBJECT,
181
                    Token::EOF
182
                ],
183
                ['object', '$name', '{', 'className', '\Foo\Bar\Baz', '}', 'eof']
184
            );
185
    }
186
187
    /**
188
     * @test
189
     */
190
    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...
191
    {
192
        //given
193
        $param = 'object $name { className \Foo\Bar\Baz }';
194
        $tokenizer = new Tokenizer();
195
196
        //when
197
        $tokens = $tokenizer->lex($param);
198
199
        //then
200
        Assert::thatArray($tokens)
201
            ->extracting('getName()', 'getValue()')
202
            ->containsExactly(
203
                [
204
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
205
                    Token::TYPE, Token::CLASS_NAME,
206
                    Token::CLOSE_OBJECT,
207
                    Token::EOF
208
                ],
209
                ['object', '$name', '{', 'className', '\Foo\Bar\Baz', '}', 'eof']
210
            );
211
    }
212
213
    /**
214
     * @test
215
     */
216
    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...
217
    {
218
        //given
219
        $param = 'object $name { className \\Foo\\Bar\\Baz }';
220
        $tokenizer = new Tokenizer();
221
222
        //when
223
        $tokens = $tokenizer->lex($param);
224
225
        //then
226
        Assert::thatArray($tokens)
227
            ->extracting('getName()', 'getValue()')
228
            ->containsExactly(
229
                [
230
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
231
                    Token::TYPE, Token::CLASS_NAME,
232
                    Token::CLOSE_OBJECT,
233
                    Token::EOF
234
                ],
235
                ['object', '$name', '{', 'className', '\\Foo\\Bar\\Baz', '}', 'eof']
236
            );
237
    }
238
239
    /**
240
     * @test
241
     */
242
    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...
243
    {
244
        //given
245
        $param = 'object $name { className Foo\Bar\Baz }';
246
        $tokenizer = new Tokenizer();
247
248
        //when
249
        $tokens = $tokenizer->lex($param);
250
251
        //then
252
        Assert::thatArray($tokens)
253
            ->extracting('getName()', 'getValue()')
254
            ->containsExactly(
255
                [
256
                    Token::TYPE, Token::NAME, Token::OPEN_OBJECT,
257
                    Token::TYPE, Token::CLASS_NAME,
258
                    Token::CLOSE_OBJECT,
259
                    Token::EOF
260
                ],
261
                ['object', '$name', '{', 'className', 'Foo\Bar\Baz', '}', 'eof']
262
            );
263
    }
264
265
    /**
266
     * @test
267
     */
268
    public function shouldThrowExceptionWhenOccursUnexpectedCharacter()
269
    {
270
        //given
271
        $param = 'string [$message';
272
        $tokenizer = new Tokenizer();
273
274
        //when
275
        CatchException::when($tokenizer)->lex($param);
276
277
        //then
278
        CatchException::assertThat()->hasMessage('Unexpected character: >[< offset >7<');
279
    }
280
}
281