ClassTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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-2009 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @see TestHelper
  23. */
  24. require_once dirname(__FILE__) . '/../../../TestHelper.php';
  25. /**
  26. * @see Zend_CodeGenerator_Php_Class
  27. */
  28. require_once 'Zend/CodeGenerator/Php/Class.php';
  29. /**
  30. * @category Zend
  31. * @package Zend_CodeGenerator
  32. * @subpackage UnitTests
  33. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  34. * @license http://framework.zend.com/license/new-bsd New BSD License
  35. *
  36. * @group Zend_CodeGenerator_Php
  37. */
  38. class Zend_CodeGenerator_Php_ClassTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function testConstruction()
  41. {
  42. $class = new Zend_CodeGenerator_Php_Class();
  43. $this->isInstanceOf($class, 'Zend_CodeGenerator_Php_Class');
  44. }
  45. public function testNameAccessors()
  46. {
  47. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  48. $codeGenClass->setName('TestClass');
  49. $this->assertEquals($codeGenClass->getName(), 'TestClass');
  50. }
  51. public function testClassDocblockAccessors()
  52. {
  53. $this->markTestSkipped();
  54. }
  55. public function testAbstractAccessors()
  56. {
  57. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  58. $this->assertFalse($codeGenClass->isAbstract());
  59. $codeGenClass->setAbstract(true);
  60. $this->assertTrue($codeGenClass->isAbstract());
  61. }
  62. public function testExtendedClassAccessors()
  63. {
  64. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  65. $codeGenClass->setExtendedClass('ExtendedClass');
  66. $this->assertEquals($codeGenClass->getExtendedClass(), 'ExtendedClass');
  67. }
  68. public function testImplementedInterfacesAccessors()
  69. {
  70. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  71. $codeGenClass->setImplementedInterfaces(array('Class1', 'Class2'));
  72. $this->assertEquals($codeGenClass->getImplementedInterfaces(), array('Class1', 'Class2'));
  73. }
  74. public function testPropertyAccessors()
  75. {
  76. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  77. $codeGenClass->setProperties(array(
  78. array('name' => 'propOne'),
  79. new Zend_CodeGenerator_Php_Property(array('name' => 'propTwo'))
  80. ));
  81. $properties = $codeGenClass->getProperties();
  82. $this->assertEquals(count($properties), 2);
  83. $this->isInstanceOf(current($properties), 'Zend_CodeGenerator_Php_Property');
  84. $property = $codeGenClass->getProperty('propTwo');
  85. $this->isInstanceOf($property, 'Zend_CodeGenerator_Php_Property');
  86. $this->assertEquals($property->getName(), 'propTwo');
  87. // add a new property
  88. $codeGenClass->setProperty(array('name' => 'prop3'));
  89. $this->assertEquals(count($codeGenClass->getProperties()), 3);
  90. try {
  91. // add a property by a same name
  92. $codeGenClass->setProperty(array('name' => 'prop3'));
  93. $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
  94. } catch (Exception $e) {
  95. $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
  96. }
  97. }
  98. public function testMethodAccessors()
  99. {
  100. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  101. $codeGenClass->setMethods(array(
  102. array('name' => 'methodOne'),
  103. new Zend_CodeGenerator_Php_Method(array('name' => 'methodTwo'))
  104. ));
  105. $methods = $codeGenClass->getMethods();
  106. $this->assertEquals(count($methods), 2);
  107. $this->isInstanceOf(current($methods), 'Zend_CodeGenerator_Php_Method');
  108. $method = $codeGenClass->getMethod('methodOne');
  109. $this->isInstanceOf($method, 'Zend_CodeGenerator_Php_Method');
  110. $this->assertEquals($method->getName(), 'methodOne');
  111. // add a new property
  112. $codeGenClass->setMethod(array('name' => 'methodThree'));
  113. $this->assertEquals(count($codeGenClass->getMethods()), 3);
  114. try {
  115. // add a property by a same name
  116. $codeGenClass->setMethod(array('name' => 'methodThree'));
  117. $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
  118. } catch (Exception $e) {
  119. $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
  120. }
  121. }
  122. public function testToString()
  123. {
  124. $codeGenClass = new Zend_CodeGenerator_Php_Class(array(
  125. 'abstract' => true,
  126. 'name' => 'SampleClass',
  127. 'extendedClass' => 'ExtendedClassName',
  128. 'implementedInterfaces' => array('Iterator', 'Traversable'),
  129. 'properties' => array(
  130. array('name' => 'foo'),
  131. array('name' => 'bar')
  132. ),
  133. 'methods' => array(
  134. array('name' => 'baz')
  135. ),
  136. ));
  137. $expectedOutput = <<<EOS
  138. abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
  139. {
  140. public \$foo = null;
  141. public \$bar = null;
  142. public function baz()
  143. {
  144. }
  145. }
  146. EOS;
  147. $output = $codeGenClass->generate();
  148. $this->assertEquals($expectedOutput, $output, $output);
  149. }
  150. }