1
+ <?php
2
+
3
+ namespace CG \Tests \Core ;
4
+
5
+ use CG \Core \DefaultGeneratorStrategy ;
6
+
7
+ use CG \Generator \PhpProperty ;
8
+ use CG \Generator \PhpMethod ;
9
+ use CG \Generator \PhpClass ;
10
+
11
+ class DefaultGeneratorStrategyTest extends \PHPUnit_Framework_TestCase
12
+ {
13
+ public function testGenerate ()
14
+ {
15
+ $ strategy = new DefaultGeneratorStrategy ();
16
+ $ strategy ->setConstantSortFunc (function ($ a , $ b ) {
17
+ return strcasecmp ($ a , $ b );
18
+ });
19
+ $ strategy ->setMethodSortFunc ($ func = function ($ a , $ b ) {
20
+ return strcasecmp ($ a ->getName (), $ b ->getName ());
21
+ });
22
+ $ strategy ->setPropertySortFunc ($ func );
23
+
24
+ $ this ->assertEquals (
25
+ $ this ->getContent ('GenerationTestClass_A.php ' ),
26
+ $ strategy ->generate ($ this ->getClass ())
27
+ );
28
+ }
29
+
30
+ public function testGenerateChangedConstantOrder ()
31
+ {
32
+ $ strategy = new DefaultGeneratorStrategy ();
33
+ $ strategy ->setConstantSortFunc (function ($ a , $ b ) {
34
+ return -1 * strcasecmp ($ a , $ b );
35
+ });
36
+ $ strategy ->setMethodSortFunc ($ func = function ($ a , $ b ) {
37
+ return strcasecmp ($ a ->getName (), $ b ->getName ());
38
+ });
39
+ $ strategy ->setPropertySortFunc ($ func );
40
+
41
+ $ this ->assertEquals (
42
+ $ this ->getContent ('GenerationTestClass_B.php ' ),
43
+ $ strategy ->generate ($ this ->getClass ())
44
+ );
45
+ }
46
+
47
+ private function getContent ($ file )
48
+ {
49
+ return file_get_contents (__DIR__ .'/generated/ ' .$ file );
50
+ }
51
+
52
+ private function getClass ()
53
+ {
54
+ $ class = PhpClass::create ()
55
+ ->setName ('GenerationTestClass ' )
56
+ ->setMethod (PhpMethod::create ('a ' ))
57
+ ->setMethod (PhpMethod::create ('b ' ))
58
+ ->setProperty (PhpProperty::create ('a ' ))
59
+ ->setProperty (PhpProperty::create ('b ' ))
60
+ ->setConstant ('a ' , 'foo ' )
61
+ ->setConstant ('b ' , 'bar ' )
62
+ ;
63
+
64
+ return $ class ;
65
+ }
66
+ }
0 commit comments