Completed
Branch master (3b8125)
by
unknown
01:13
created

ToolsTest::testIsValidPropertyNameString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace HelloWordPl\SimpleEntityGeneratorBundle\Tests\Lib;
4
5
use HelloWordPl\SimpleEntityGeneratorBundle\Lib\Tools;
6
7
/**
8
 * Tools Tests
9
 *
10
 * @author Sławomir Kania <[email protected]>
11
 */
12
class ToolsTest extends \PHPUnit_Framework_TestCase
13
{
14
15
    /**
16
     * @dataProvider dataForMethodRemoveBackslashPrefixFromNamespace
17
     */
18
    public function testRemoveBackslashPrefixFromNamespace($namespace, $output)
19
    {
20
        $this->assertEquals($output, Tools::removeBackslashPrefixFromNamespace($namespace));
21
    }
22
23 View Code Duplication
    public function dataForMethodRemoveBackslashPrefixFromNamespace()
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...
24
    {
25
        return [
26
            ["\\GeneratorEntityBundle\\Controller\\ClassName", "GeneratorEntityBundle\\Controller\\ClassName"],
27
            ["\\GeneratorEntityBundle\\Controller\\Test\\ClassName", "GeneratorEntityBundle\\Controller\\Test\\ClassName"],
28
            ["\\GeneratorEntityBundle\\ClassName", "GeneratorEntityBundle\\ClassName"],
29
        ];
30
    }
31
32
    /**
33
     * @dataProvider dataForMethodGetNameFromNamespace
34
     */
35
    public function testGetNameFromNamespace($namespace, $output)
36
    {
37
        $this->assertEquals($output, Tools::getNameFromNamespace($namespace));
38
    }
39
40
    public function dataForMethodGetNameFromNamespace()
41
    {
42
        return [
43
            ["\\GeneratorEntityBundle\\Controller\\ClassName", "ClassName"],
44
            ["\\GeneratorEntityBundle\\Controller\\Test\\ClassName", "ClassName"],
45
            ["\\GeneratorEntityBundle\\ClassName", "ClassName"],
46
        ];
47
    }
48
49
    /**
50
     * @dataProvider dataForMethodGetDirectoryFromNamespace
51
     */
52
    public function testGetDirectoryFromNamespace($namespace, $output)
53
    {
54
        $this->assertEquals($output, Tools::getDirectoryFromNamespace($namespace));
55
    }
56
57 View Code Duplication
    public function dataForMethodGetDirectoryFromNamespace()
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...
58
    {
59
        return [
60
            ["\\GeneratorEntityBundle\\Controller\\ClassName", "/GeneratorEntityBundle/Controller"],
61
            ["\\GeneratorEntityBundle\\Controller\\Test\\ClassName", "/GeneratorEntityBundle/Controller/Test"],
62
            ["\\GeneratorEntityBundle\\ClassName", "/GeneratorEntityBundle"],
63
        ];
64
    }
65
66
    /**
67
     * @dataProvider dataForMethodGetNamespaceWithoutName
68
     */
69
    public function testGetNamespaceWithoutName($namespace, $output)
70
    {
71
        $this->assertEquals($output, Tools::getNamespaceWithoutName($namespace));
72
    }
73
74
    public function dataForMethodGetNamespaceWithoutName()
75
    {
76
        return [
77
            ["\\GeneratorEntityBundle\\ClassName", "\\GeneratorEntityBundle"],
78
            ["\\GeneratorEntityBundle\\Controller\\ClassName", "\\GeneratorEntityBundle\\Controller"],
79
            ["\\GeneratorEntityBundle\\Controller\\Test\\ClassName", "\\GeneratorEntityBundle\\Controller\\Test"],
80
            ["\\GeneratorEntityBundle\\ClassName", "\\GeneratorEntityBundle"],
81
        ];
82
    }
83
84
    /**
85
     * @dataProvider dataForMethodIsNamespaceValidWhenTrue
86
     */
87
    public function testIsNamespaceValidWhenTrue($namespace)
88
    {
89
        $this->assertTrue(Tools::isNamespaceValid($namespace));
90
    }
91
92
    public function dataForMethodIsNamespaceValidWhenTrue()
93
    {
94
        return [
95
            ["\\GeneratorEntityBundle\\Controller\\Lib"],
96
            ["\\GeneratorEntityBundle1\\Controller\\Lib"],
97
            ["\\Generator1EntityBundle\\Controller\\Lib"],
98
            ["\\Generator1EntityBundle\\Controller1\\Lib1"],
99
            ["\\GeneratorEntityBundle\\Controller"],
100
            ["\\generator_entity_bundle\\controller"],
101
            ["\\generatorentitybundle\\controller"],
102
        ];
103
    }
104
105
    /**
106
     * @dataProvider dataForMethodIsNamespaceValidWhenFalse
107
     */
108
    public function testIsNamespaceValidWhenFalse($namespace)
109
    {
110
        $this->assertFalse(Tools::isNamespaceValid($namespace));
111
    }
112
113
    public function dataForMethodIsNamespaceValidWhenFalse()
114
    {
115
        return [
116
            ["\\GeneratorEntityBundle"],
117
            ["\\\\1GeneratorEntityBundle\\\\Controller\\\\Lib"],
118
            ["\\GeneratorEntityBundle\\1Controller\\Lib"],
119
            ["\\GeneratorEntityBundle\\Controller\\1Lib"],
120
            ["\\Generator!EntityBundle\\Controller"],
121
            ["\\Generator@EntityBundle\\Controller"],
122
            ["\\Generator#EntityBundle\\Controller"],
123
            ["\\Generator\$EntityBundle\\Controller"],
124
            ["\\Generator%EntityBundle\\Controller"],
125
            ["\\Generator^EntityBundle\\Controller"],
126
            ["\\Generator&EntityBundle\\Controller"],
127
            ["\\Generator*EntityBundle\\Controller"],
128
            ["\\Generator(EntityBundle\\Controller"],
129
            ["\\Generator)EntityBundle\\Controller"],
130
            ["\\Generator-EntityBundle\\Controller"],
131
            ["\\Generator=EntityBundle\\Controller"],
132
            ["\\GeneratorEntityBundle\\Cont&roller"],
133
            ["\\Generator EntityBundle\\Cont&roller"],
134
            ["\\GeneratorEntityBundle\\ Cont&roller"],
135
            ["\\Generator;EntityBundle\\Cont&roller"],
136
            ["\\GeneratorEntityBundle\\Cont&roller"],
137
            ["/GeneratorEntityBundle/Cont&roller"],
138
            ["\\GeneratorEntityBundle\\Cont%roller"],
139
            ["\\~GeneratorEntityBundle\\Cont%roller"],
140
            ["\\GeneratorEntityBundle\\\\Controller"],
141
            ["\\GeneratorEntityBundle>Controller"],
142
            ["\\GeneratorEntityBundle<Controller"],
143
            ["\\GeneratorEntityBundle?Controller"],
144
            ["\\GeneratorEntityBundle:Controller"],
145
            ["\\GeneratorEntityBundle;Controller"],
146
            ["\\GeneratorEntityBundle'Controller"],
147
        ];
148
    }
149
150
    /**
151
     * @dataProvider dataForMethodIsNamespaceValidFirstBackslashNotMandatory
152
     */
153
    public function testIsNamespaceValidFirstBackslashNotMandatory($namespace)
154
    {
155
        $this->assertTrue(Tools::isNamespaceValid($namespace, false));
156
    }
157
158
    public function dataForMethodIsNamespaceValidFirstBackslashNotMandatory()
159
    {
160
        return [
161
            ["GeneratorEntityBundle\\Entity\\NotExistingYetEntityType"],
162
            ["GeneratorEntityBundle\\NotExistingYet\\EntityType"],
163
        ];
164
    }
165
166
    /**
167
     * @dataProvider dataForTestIsValidPropertyNameString
168
     */
169
    public function testIsValidPropertyNameString($name, $result)
170
    {
171
        $this->assertEquals(Tools::isValidPropertyNameString($name), $result);
172
    }
173
174
    public function dataForTestIsValidPropertyNameString()
175
    {
176
        return [
177
            ['email', true],
178
            ['name', true],
179
            ['name2', true],
180
            ['na3me', true],
181
            ['na3me', true],
182
            ['full_name', true],
183
            [' name', false],
184
            ['name ', false],
185
            [' name ', false],
186
            ['', false],
187
            [null, false],
188
            [false, false],
189
            [true, false],
190
            ['3name', false],
191
            ['\name', false],
192
            ['na!me', false],
193
            ['na@me', false],
194
            ['na#me', false],
195
            ['na$me', false],
196
            ['na%me', false],
197
            ['na^me', false],
198
            ['na&me', false],
199
            ['na(me', false],
200
            ['na*me', false],
201
            ['na)me', false],
202
            ['na)me', false],
203
            ['na,me', false],
204
            ['na.me', false],
205
            ['na"me', false],
206
            ['na~me', false],
207
            ['na`me', false],
208
            ['na me', false],
209
            ['_', false],
210
            ['9', false],
211
        ];
212
    }
213
214
    /**
215
     * @dataProvider dataForTestIsValidPropertyTypeString
216
     */
217
    public function testIsValidPropertyTypeString($name, $result)
218
    {
219
        $this->assertEquals(Tools::isValidPropertyTypeString($name), $result);
220
    }
221
222
    public function dataForTestIsValidPropertyTypeString()
223
    {
224
        return [
225
            ['integer', true],
226
            ['string', true],
227
            ['array', true],
228
            ['array<Some\Object>', true],
229
            ['boolean', true],
230
            ['float', true],
231
            ['double', true],
232
            ['DateTime', true],
233
            ["DateTime<'format'>", true],
234
            ['Doctrine\Common\Collections\ArrayCollection', true],
235
            ['Doctrine\Common\Collections\ArrayCollection<string>', true],
236
            ['Doctrine\Common\Collections\ArrayCollection<Some\Object>', true],
237
            ['Doctrine\Common\Collections\ArrayCollection<string, Some\Object>', true],
238
            ['\\', false],
239
            ['', false],
240
            [null, false],
241
            [false, false],
242
            [true, false],
243
            [true, false],
244
            ['int!eger', false],
245
            ['int@eger', false],
246
            ['int#eger', false],
247
            ['int$eger', false],
248
            ['int%eger', false],
249
            ['int^eger', false],
250
            ['int&eger', false],
251
            ['int(eger', false],
252
            ['int*eger', false],
253
            ['int)eger', false],
254
            ['int)eger', false],
255
            ['int.eger', false],
256
            ['int"eger', false],
257
            ['int~eger', false],
258
            ['int`eger', false],
259
            ['9', false],
260
            ['\DateTime', false],
261
            ['array<\Some\Object>', false],
262
            ['\Some\Object', false],
263
        ];
264
    }
265
266
    public function testExplodeTemplateStringToArray()
267
    {
268
        $this->assertEquals(3, count(Tools::explodeTemplateStringToArray("foo\nbar\n")));
269
        $this->assertEquals(2, count(Tools::explodeTemplateStringToArray("foo\n")));
270
        $this->assertEquals(1, count(Tools::explodeTemplateStringToArray("foo")));
271
    }
272
273
    public function testImplodeArrayToTemplate()
274
    {
275
        $this->assertEquals("foo\nbar", Tools::implodeArrayToTemplate(["foo", "bar"]));
276
        $this->assertEquals("foo\nbar\n", Tools::implodeArrayToTemplate(["foo", "bar", ""]));
277
        $this->assertEquals("foo\n\nbar", Tools::implodeArrayToTemplate(["foo", "", "bar"]));
278
        $this->assertEquals("foo", Tools::implodeArrayToTemplate(["foo"]));
279
        $this->assertEquals("foo\n", Tools::implodeArrayToTemplate(["foo", ""]));
280
    }
281
282
    /**
283
     * @dataProvider dataForTestIsCallableConstraintAnnotation
284
     */
285
    public function testIsCallableConstraintAnnotation($constraint, $result)
286
    {
287
        $this->assertEquals($result, Tools::isCallableConstraintAnnotation($constraint));
288
    }
289
290
    public function dataForTestIsCallableConstraintAnnotation()
291
    {
292
        return [
293
            ["@\Symfony\Component\Validator\Constraints\Valid()", true],
294
            ["@\Symfony\Component\Validator\Constraints\Valid(message=\"Has to be valid!\")", true],
295
            ["@Symfony\Component\Validator\Constraints\Valid()", true],
296
            ["@Symfony\Component\Validator\Constraints\Valid(message=\"Has to be valid!\")", true],
297
            ["@Symfony\Component\Validator\Constraints\Valid(sdfgsdfgsdfgsdgsdg)", true], // @todo in the future
298
            ["@Symfony\Component\Validator\Constraints\NotExistConstraint()", false],
299
            ["@Symfony\Component\Validator\Constraints\Valid", false],
300
            ["@Symfony\Component\Validator\Constraints\Valid(", false],
301
            ["@Symfony\Component\Validator\Constraints\Valid)", false],
302
            ["Symfony\Component\Validator\Constraints\Valid()", false],
303
            ["@Symfony-Component\Validator\Constraints\Valid()", false],
304
            ["@Symfony-Component\Validator\Constraints", false],
305
            [null, false],
306
            ["", false],
307
            ["4l594276974569-0df70-trt7wre", false],
308
            ['', false],
309
            ["@\Symfony\Component\Validator\Constraints\@\Symfony\Component\Validator\Constraints\Valid()", false],
310
        ];
311
    }
312
313
    /**
314
     * @dataProvider dataForTestIsFirstCharBackslash
315
     */
316
    public function testIsFirstCharBackslash($string, $result)
317
    {
318
        $this->assertEquals($result, Tools::isFirstCharBackslash($string));
319
    }
320
321
    public function dataForTestIsFirstCharBackslash()
322
    {
323
        return [
324
            ["\\LoremIpsum", true],
325
            ["LoremIpsum", false],
326
            ["LoremIpsum\\", false],
327
            ["Lorem\\Ipsum", false],
328
            ["\\", true],
329
            ["", false],
330
            ['', false],
331
        ];
332
    }
333
334
    /**
335
     * @dataProvider dataForTestIsFirstCharBackslashWhenInvalidString
336
     * @expectedException \Exception
337
     */
338
    public function testIsFirstCharBackslashWhenInvalidString($value)
339
    {
340
        Tools::isFirstCharBackslash($value);
341
    }
342
343
    public function dataForTestIsFirstCharBackslashWhenInvalidString()
344
    {
345
        return [
346
            [null],
347
            [34],
348
            [[]],
349
        ];
350
    }
351
}
352