forked from zendframework/zendframework
/
ClassGeneratorTest.php
380 lines (307 loc) · 11.3 KB
/
ClassGeneratorTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Code
*/
namespace ZendTest\Code\Generator;
use Zend\Code\Generator\ClassGenerator;
use Zend\Code\Generator\PropertyGenerator;
use Zend\Code\Generator\MethodGenerator;
use Zend\Code\Reflection\ClassReflection;
/**
* @category Zend
* @package Zend_Code_Generator
* @subpackage UnitTests
*
* @group Zend_Code_Generator
* @group Zend_Code_Generator_Php
*/
class ClassGeneratorTest extends \PHPUnit_Framework_TestCase
{
public function testConstruction()
{
$class = new ClassGenerator();
$this->isInstanceOf($class, '\Zend\Code\Generator\ClassGenerator');
}
public function testNameAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('TestClass');
$this->assertEquals($classGenerator->getName(), 'TestClass');
}
public function testClassDocBlockAccessors()
{
$this->markTestIncomplete();
}
public function testAbstractAccessors()
{
$classGenerator = new ClassGenerator();
$this->assertFalse($classGenerator->isAbstract());
$classGenerator->setAbstract(true);
$this->assertTrue($classGenerator->isAbstract());
}
public function testExtendedClassAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setExtendedClass('ExtendedClass');
$this->assertEquals($classGenerator->getExtendedClass(), 'ExtendedClass');
}
public function testImplementedInterfacesAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->setImplementedInterfaces(array('Class1', 'Class2'));
$this->assertEquals($classGenerator->getImplementedInterfaces(), array('Class1', 'Class2'));
}
public function testPropertyAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperties(array(
'propOne',
new PropertyGenerator('propTwo')
));
$properties = $classGenerator->getProperties();
$this->assertEquals(count($properties), 2);
$this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', current($properties));
$property = $classGenerator->getProperty('propTwo');
$this->assertInstanceOf('Zend\Code\Generator\PropertyGenerator', $property);
$this->assertEquals($property->getName(), 'propTwo');
// add a new property
$classGenerator->addProperty('prop3');
$this->assertEquals(count($classGenerator->getProperties()), 3);
}
public function testSetPropertyAlreadyExistsThrowsException()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperty('prop3');
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'A property by name prop3 already exists in this class'
);
$classGenerator->addProperty('prop3');
}
public function testSetPropertyNoArrayOrPropertyThrowsException()
{
$classGenerator = new ClassGenerator();
$this->setExpectedException(
'Zend\Code\Generator\Exception\InvalidArgumentException',
'addProperty() expects string for name'
);
$classGenerator->addProperty(true);
}
public function testMethodAccessors()
{
$classGenerator = new ClassGenerator();
$classGenerator->addMethods(array(
'methodOne',
new MethodGenerator('methodTwo')
));
$methods = $classGenerator->getMethods();
$this->assertEquals(count($methods), 2);
$this->isInstanceOf(current($methods), '\Zend\Code\Generator\PhpMethod');
$method = $classGenerator->getMethod('methodOne');
$this->isInstanceOf($method, '\Zend\Code\Generator\PhpMethod');
$this->assertEquals($method->getName(), 'methodOne');
// add a new property
$classGenerator->addMethod('methodThree');
$this->assertEquals(count($classGenerator->getMethods()), 3);
}
public function testSetMethodNoMethodOrArrayThrowsException()
{
$classGenerator = new ClassGenerator();
$this->setExpectedException(
'Zend\Code\Generator\Exception\ExceptionInterface',
'addMethod() expects string for name'
);
$classGenerator->addMethod(true);
}
public function testSetMethodNameAlreadyExistsThrowsException()
{
$methodA = new MethodGenerator();
$methodA->setName("foo");
$methodB = new MethodGenerator();
$methodB->setName("foo");
$classGenerator = new ClassGenerator();
$classGenerator->addMethodFromGenerator($methodA);
$this->setExpectedException('Zend\Code\Generator\Exception\InvalidArgumentException', 'A method by name foo already exists in this class.');
$classGenerator->addMethodFromGenerator($methodB);
}
/**
* @group ZF-7361
*/
public function testHasMethod()
{
$classGenerator = new ClassGenerator();
$classGenerator->addMethod('methodOne');
$this->assertTrue($classGenerator->hasMethod('methodOne'));
}
/**
* @group ZF-7361
*/
public function testHasProperty()
{
$classGenerator = new ClassGenerator();
$classGenerator->addProperty('propertyOne');
$this->assertTrue($classGenerator->hasProperty('propertyOne'));
}
public function testToString()
{
$classGenerator = new ClassGenerator(
'SampleClass',
null,
ClassGenerator::FLAG_ABSTRACT,
'ExtendedClassName',
array('Iterator', 'Traversable'),
array('foo', 'bar'),
array('baz')
);
$classGenerator = ClassGenerator::fromArray(
array(
'name' => 'SampleClass',
//'abstract' => true,
'flags' => ClassGenerator::FLAG_ABSTRACT,
'name' => 'SampleClass',
'extendedClass' => 'ExtendedClassName',
'implementedInterfaces' => array('Iterator', 'Traversable'),
'properties' => array('foo',
array('name' => 'bar')
),
'methods' => array(
array('name' => 'baz')
),
));
$expectedOutput = <<<EOS
abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
{
public \$foo = null;
public \$bar = null;
public function baz()
{
}
}
EOS;
$output = $classGenerator->generate();
$this->assertEquals($expectedOutput, $output, $output);
}
/**
* @group ZF-7909
*/
public function testClassFromReflectionThatImplementsInterfaces()
{
$reflClass = new ClassReflection('ZendTest\Code\Generator\TestAsset\ClassWithInterface');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$classGenerator->setSourceDirty(true);
$code = $classGenerator->generate();
$expectedClassDef = 'class ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\OneInterface'
. ', ZendTest\Code\Generator\TestAsset\TwoInterface';
$this->assertContains($expectedClassDef, $code);
}
/**
* @group ZF-7909
*/
public function testClassFromReflectionDiscardParentImplementedInterfaces()
{
$reflClass = new ClassReflection('\ZendTest\Code\Generator\TestAsset\NewClassWithInterface');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$classGenerator->setSourceDirty(true);
$code = $classGenerator->generate();
$expectedClassDef = 'class NewClassWithInterface'
. ' extends ZendTest\Code\Generator\TestAsset\ClassWithInterface'
. ' implements ZendTest\Code\Generator\TestAsset\ThreeInterface';
$this->assertContains($expectedClassDef, $code);
}
/**
* @group ZF-9602
*/
public function testSetextendedclassShouldIgnoreEmptyClassnameOnGenerate()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName( 'MyClass' )
->setExtendedClass('');
$expected = <<<CODE
class MyClass
{
}
CODE;
$this->assertEquals( $expected, $classGeneratorClass->generate() );
}
/**
* @group ZF-9602
*/
public function testSetextendedclassShouldNotIgnoreNonEmptyClassnameOnGenerate()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName( 'MyClass' )
->setExtendedClass('ParentClass');
$expected = <<<CODE
class MyClass extends ParentClass
{
}
CODE;
$this->assertEquals( $expected, $classGeneratorClass->generate() );
}
/**
* @group namespace
*/
public function testCodeGenerationShouldTakeIntoAccountNamespacesFromReflection()
{
$reflClass = new ClassReflection('ZendTest\Code\Generator\TestAsset\ClassWithNamespace');
$classGenerator = ClassGenerator::fromReflection($reflClass);
$this->assertEquals('ZendTest\Code\Generator\TestAsset', $classGenerator->getNamespaceName());
$this->assertEquals('ClassWithNamespace', $classGenerator->getName());
$expected = <<<CODE
namespace ZendTest\Code\Generator\\TestAsset;
class ClassWithNamespace
{
}
CODE;
$received = $classGenerator->generate();
$this->assertEquals($expected, $received, $received);
}
/**
* @group namespace
*/
public function testSetNameShouldDetermineIfNamespaceSegmentIsPresent()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$this->assertEquals('My\Namespaced', $classGeneratorClass->getNamespaceName());
}
/**
* @group namespace
*/
public function testPassingANamespacedClassnameShouldGenerateANamespaceDeclaration()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$received = $classGeneratorClass->generate();
$this->assertContains('namespace My\Namespaced;', $received, $received);
}
/**
* @group namespace
*/
public function testPassingANamespacedClassnameShouldGenerateAClassnameWithoutItsNamespace()
{
$classGeneratorClass = new ClassGenerator();
$classGeneratorClass->setName('My\Namespaced\FunClass');
$received = $classGeneratorClass->generate();
$this->assertContains('class FunClass', $received, $received);
}
/**
* @group ZF2-151
*/
public function testAddUses()
{
$classGenerator = new ClassGenerator();
$classGenerator->setName('My\Class');
$classGenerator->addUse('My\First\Use\Class');
$classGenerator->addUse('My\Second\Use\Class', 'MyAlias');
$generated = $classGenerator->generate();
$this->assertContains('use My\First\Use\Class;', $generated);
$this->assertContains('use My\Second\Use\Class as MyAlias;', $generated);
}
}