ClassTest.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. <?php
  2. /**
  3. * Zend Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://framework.zend.com/license/new-bsd
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@zend.com so we can send you a copy immediately.
  14. *
  15. * @category Zend
  16. * @package Zend_CodeGenerator
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. * @version $Id $
  21. */
  22. /**
  23. * @see TestHelper
  24. */
  25. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  26. /**
  27. * @see Zend_CodeGenerator_Php_Class
  28. */
  29. require_once 'Zend/CodeGenerator/Php/Class.php';
  30. /**
  31. * @category Zend
  32. * @package Zend_CodeGenerator
  33. * @subpackage UnitTests
  34. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  35. * @license http://framework.zend.com/license/new-bsd New BSD License
  36. *
  37. * @group Zend_CodeGenerator
  38. * @group Zend_CodeGenerator_Php
  39. */
  40. class Zend_CodeGenerator_Php_ClassTest extends PHPUnit_Framework_TestCase
  41. {
  42. public function testConstruction()
  43. {
  44. $class = new Zend_CodeGenerator_Php_Class();
  45. $this->isInstanceOf($class, 'Zend_CodeGenerator_Php_Class');
  46. }
  47. public function testNameAccessors()
  48. {
  49. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  50. $codeGenClass->setName('TestClass');
  51. $this->assertEquals($codeGenClass->getName(), 'TestClass');
  52. }
  53. public function testClassDocblockAccessors()
  54. {
  55. $this->markTestSkipped();
  56. }
  57. public function testAbstractAccessors()
  58. {
  59. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  60. $this->assertFalse($codeGenClass->isAbstract());
  61. $codeGenClass->setAbstract(true);
  62. $this->assertTrue($codeGenClass->isAbstract());
  63. }
  64. public function testExtendedClassAccessors()
  65. {
  66. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  67. $codeGenClass->setExtendedClass('ExtendedClass');
  68. $this->assertEquals($codeGenClass->getExtendedClass(), 'ExtendedClass');
  69. }
  70. public function testImplementedInterfacesAccessors()
  71. {
  72. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  73. $codeGenClass->setImplementedInterfaces(array('Class1', 'Class2'));
  74. $this->assertEquals($codeGenClass->getImplementedInterfaces(), array('Class1', 'Class2'));
  75. }
  76. public function testPropertyAccessors()
  77. {
  78. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  79. $codeGenClass->setProperties(array(
  80. array('name' => 'propOne'),
  81. new Zend_CodeGenerator_Php_Property(array('name' => 'propTwo'))
  82. ));
  83. $properties = $codeGenClass->getProperties();
  84. $this->assertEquals(count($properties), 2);
  85. $this->isInstanceOf(current($properties), 'Zend_CodeGenerator_Php_Property');
  86. $property = $codeGenClass->getProperty('propTwo');
  87. $this->isInstanceOf($property, 'Zend_CodeGenerator_Php_Property');
  88. $this->assertEquals($property->getName(), 'propTwo');
  89. // add a new property
  90. $codeGenClass->setProperty(array('name' => 'prop3'));
  91. $this->assertEquals(count($codeGenClass->getProperties()), 3);
  92. }
  93. public function testSetProperty_AlreadyExists_ThrowsException()
  94. {
  95. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  96. $codeGenClass->setProperty(array('name' => 'prop3'));
  97. $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
  98. $codeGenClass->setProperty(array('name' => 'prop3'));
  99. }
  100. public function testSetProperty_NoArrayOrProperty_ThrowsException()
  101. {
  102. $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
  103. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  104. $codeGenClass->setProperty("propertyName");
  105. }
  106. public function testMethodAccessors()
  107. {
  108. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  109. $codeGenClass->setMethods(array(
  110. array('name' => 'methodOne'),
  111. new Zend_CodeGenerator_Php_Method(array('name' => 'methodTwo'))
  112. ));
  113. $methods = $codeGenClass->getMethods();
  114. $this->assertEquals(count($methods), 2);
  115. $this->isInstanceOf(current($methods), 'Zend_CodeGenerator_Php_Method');
  116. $method = $codeGenClass->getMethod('methodOne');
  117. $this->isInstanceOf($method, 'Zend_CodeGenerator_Php_Method');
  118. $this->assertEquals($method->getName(), 'methodOne');
  119. // add a new property
  120. $codeGenClass->setMethod(array('name' => 'methodThree'));
  121. $this->assertEquals(count($codeGenClass->getMethods()), 3);
  122. }
  123. public function testSetMethod_NoMethodOrArray_ThrowsException()
  124. {
  125. $this->setExpectedException("Zend_CodeGenerator_Php_Exception",
  126. 'setMethod() expects either an array of method options or an instance of Zend_CodeGenerator_Php_Method'
  127. );
  128. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  129. $codeGenClass->setMethod("aMethodName");
  130. }
  131. public function testSetMethod_NameAlreadyExists_ThrowsException()
  132. {
  133. $methodA = new Zend_CodeGenerator_Php_Method();
  134. $methodA->setName("foo");
  135. $methodB = new Zend_CodeGenerator_Php_Method();
  136. $methodB->setName("foo");
  137. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  138. $codeGenClass->setMethod($methodA);
  139. $this->setExpectedException("Zend_CodeGenerator_Php_Exception", 'A method by name foo already exists in this class.');
  140. $codeGenClass->setMethod($methodB);
  141. }
  142. /**
  143. * @group ZF-7361
  144. */
  145. public function testHasMethod()
  146. {
  147. $method = new Zend_CodeGenerator_Php_Method();
  148. $method->setName('methodOne');
  149. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  150. $codeGenClass->setMethod($method);
  151. $this->assertTrue($codeGenClass->hasMethod('methodOne'));
  152. }
  153. /**
  154. * @group ZF-7361
  155. */
  156. public function testHasProperty()
  157. {
  158. $property = new Zend_CodeGenerator_Php_Property();
  159. $property->setName('propertyOne');
  160. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  161. $codeGenClass->setProperty($property);
  162. $this->assertTrue($codeGenClass->hasProperty('propertyOne'));
  163. }
  164. public function testToString()
  165. {
  166. $codeGenClass = new Zend_CodeGenerator_Php_Class(array(
  167. 'abstract' => true,
  168. 'name' => 'SampleClass',
  169. 'extendedClass' => 'ExtendedClassName',
  170. 'implementedInterfaces' => array('Iterator', 'Traversable'),
  171. 'properties' => array(
  172. array('name' => 'foo'),
  173. array('name' => 'bar')
  174. ),
  175. 'methods' => array(
  176. array('name' => 'baz')
  177. ),
  178. ));
  179. $expectedOutput = <<<EOS
  180. abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
  181. {
  182. public \$foo = null;
  183. public \$bar = null;
  184. public function baz()
  185. {
  186. }
  187. }
  188. EOS;
  189. $output = $codeGenClass->generate();
  190. $this->assertEquals($expectedOutput, $output, $output);
  191. }
  192. /**
  193. * @group ZF-7909 */
  194. public function testClassFromReflectionThatImplementsInterfaces()
  195. {
  196. if(!class_exists('Zend_CodeGenerator_Php_ClassWithInterface')) {
  197. require_once dirname(__FILE__)."/_files/ClassAndInterfaces.php";
  198. }
  199. require_once "Zend/Reflection/Class.php";
  200. $reflClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_ClassWithInterface');
  201. $codeGen = Zend_CodeGenerator_Php_Class::fromReflection($reflClass);
  202. $codeGen->setSourceDirty(true);
  203. $code = $codeGen->generate();
  204. $expectedClassDef = 'class Zend_CodeGenerator_Php_ClassWithInterface implements Zend_Code_Generator_Php_OneInterface, Zend_Code_Generator_Php_TwoInterface';
  205. $this->assertContains($expectedClassDef, $code);
  206. }
  207. /**
  208. * @group ZF-7909
  209. */
  210. public function testClassFromReflectionDiscardParentImplementedInterfaces()
  211. {
  212. if(!class_exists('Zend_CodeGenerator_Php_ClassWithInterface')) {
  213. require_once dirname(__FILE__)."/_files/ClassAndInterfaces.php";
  214. }
  215. require_once "Zend/Reflection/Class.php";
  216. $reflClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_NewClassWithInterface');
  217. $codeGen = Zend_CodeGenerator_Php_Class::fromReflection($reflClass);
  218. $codeGen->setSourceDirty(true);
  219. $code = $codeGen->generate();
  220. $expectedClassDef = 'class Zend_CodeGenerator_Php_NewClassWithInterface extends Zend_CodeGenerator_Php_ClassWithInterface implements Zend_Code_Generator_Php_ThreeInterface';
  221. $this->assertContains($expectedClassDef, $code);
  222. }
  223. /**
  224. * @group ZF-9602
  225. */
  226. public function testSetextendedclassShouldIgnoreEmptyClassnameOnGenerate()
  227. {
  228. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  229. $codeGenClass->setName( 'MyClass' )
  230. ->setExtendedClass('');
  231. $expected = <<<CODE
  232. class MyClass
  233. {
  234. }
  235. CODE;
  236. $this->assertEquals( $expected, $codeGenClass->generate() );
  237. }
  238. /**
  239. * @group ZF-9602
  240. */
  241. public function testSetextendedclassShouldNotIgnoreNonEmptyClassnameOnGenerate()
  242. {
  243. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  244. $codeGenClass->setName( 'MyClass' )
  245. ->setExtendedClass('ParentClass');
  246. $expected = <<<CODE
  247. class MyClass extends ParentClass
  248. {
  249. }
  250. CODE;
  251. $this->assertEquals( $expected, $codeGenClass->generate() );
  252. }
  253. }