ClassTest.php 10 KB

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