Skip to content

Commit 5e390aa

Browse files
committed
added some more unit tests, and doc comments
1 parent 753fbcb commit 5e390aa

10 files changed

+196
-1
lines changed

src/CG/Core/DefaultGeneratorStrategy.php

+8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77
use CG\Generator\DefaultVisitor;
88
use CG\Generator\DefaultNavigator;
99

10+
/**
11+
* The default generator strategy.
12+
*
13+
* This strategy allows to change the order in which methods, properties and
14+
* constants are sorted.
15+
*
16+
* @author Johannes M. Schmitt <[email protected]>
17+
*/
1018
class DefaultGeneratorStrategy implements GeneratorStrategyInterface
1119
{
1220
private $navigator;

src/CG/Core/DefaultNamingStrategy.php

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
namespace CG\Core;
44

5+
/**
6+
* The default naming strategy.
7+
*
8+
* @author Johannes M. Schmitt <[email protected]>
9+
*/
510
class DefaultNamingStrategy implements NamingStrategyInterface
611
{
712
public function getClassName(\ReflectionClass $class)

src/CG/Core/GeneratorStrategyInterface.php

+8
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,14 @@
44

55
use CG\Generator\PhpClass;
66

7+
/**
8+
* Generator Strategy Interface.
9+
*
10+
* Implementing classes are responsible for generating PHP code from the given
11+
* PhpClass instance.
12+
*
13+
* @author Johannes M. Schmitt <[email protected]>
14+
*/
715
interface GeneratorStrategyInterface
816
{
917
function generate(PhpClass $class);

src/CG/Core/NamingStrategyInterface.php

+14
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,23 @@
22

33
namespace CG\Core;
44

5+
/**
6+
* The naming strategy interface.
7+
*
8+
* @author Johannes M. Schmitt <[email protected]>
9+
*/
510
interface NamingStrategyInterface
611
{
712
const SEPARATOR = '__CG__';
813

14+
/**
15+
* Returns the class name for the proxy class.
16+
*
17+
* The generated class name MUST be the original class appended with the
18+
* separator, and an optional string that is up to the implementation.
19+
*
20+
* @param \ReflectionClass $class
21+
* @return string the class name for the generated class
22+
*/
923
function getClassName(\ReflectionClass $class);
1024
}

src/CG/Core/ReflectionUtils.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static function getOverrideableMethods(\ReflectionClass $class, $publicOn
1414

1515
return array_filter(
1616
$class->getMethods($filter),
17-
function($method) { return !$method->isFinal(); }
17+
function($method) { return !$method->isFinal() && !$method->isStatic(); }
1818
);
1919
}
2020

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace CG\Tests\Core;
4+
5+
use CG\Core\ClassUtils;
6+
7+
class ClassUtilsTest extends \PHPUnit_Framework_TestCase
8+
{
9+
public function testGetUserClassName()
10+
{
11+
$this->assertEquals('Foo', ClassUtils::getUserClass('Foo'));
12+
$this->assertEquals('Bar', ClassUtils::getUserClass('Bar__CG__FOO'));
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace CG\Tests\Core;
4+
5+
use CG\Generator\Writer;
6+
use CG\Core\ReflectionUtils;
7+
8+
class ReflectionUtilsTest extends \PHPUnit_Framework_TestCase
9+
{
10+
public function testGetOverridableMethods()
11+
{
12+
$ref = new \ReflectionClass('CG\Tests\Core\OverridableReflectionTest');
13+
$methods = ReflectionUtils::getOverrideableMethods($ref);
14+
15+
$this->assertEquals(4, count($methods));
16+
17+
$methods = array_map(function($v) { return $v->name; }, $methods);
18+
sort($methods);
19+
$this->assertEquals(array('a', 'd', 'e', 'h'), $methods);
20+
}
21+
22+
public function testGetUnindentedDocComment()
23+
{
24+
$writer = new Writer();
25+
$comment = $writer
26+
->writeln('/**')
27+
->indent()
28+
->writeln(' * Foo.')
29+
->write(' */')
30+
->getContent()
31+
;
32+
33+
$this->assertEquals("/**\n * Foo.\n */", ReflectionUtils::getUnindentedDocComment($comment));
34+
}
35+
}
36+
37+
abstract class OverridableReflectionTest
38+
{
39+
public function a() { }
40+
public final function b() { }
41+
public static function c() { }
42+
abstract public function d();
43+
protected function e() { }
44+
protected final function f() {}
45+
protected static function g() { }
46+
abstract protected function h();
47+
private function i() { }
48+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class GenerationTestClass
2+
{
3+
const a = 'foo';
4+
const b = 'bar';
5+
6+
public $a;
7+
public $b;
8+
9+
public function a()
10+
{
11+
}
12+
13+
public function b()
14+
{
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class GenerationTestClass
2+
{
3+
const b = 'bar';
4+
const a = 'foo';
5+
6+
public $a;
7+
public $b;
8+
9+
public function a()
10+
{
11+
}
12+
13+
public function b()
14+
{
15+
}
16+
}

0 commit comments

Comments
 (0)