ClassTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. * @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-2009 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. try {
  93. // add a property by a same name
  94. $codeGenClass->setProperty(array('name' => 'prop3'));
  95. $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
  96. } catch (Exception $e) {
  97. $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
  98. }
  99. }
  100. public function testMethodAccessors()
  101. {
  102. $codeGenClass = new Zend_CodeGenerator_Php_Class();
  103. $codeGenClass->setMethods(array(
  104. array('name' => 'methodOne'),
  105. new Zend_CodeGenerator_Php_Method(array('name' => 'methodTwo'))
  106. ));
  107. $methods = $codeGenClass->getMethods();
  108. $this->assertEquals(count($methods), 2);
  109. $this->isInstanceOf(current($methods), 'Zend_CodeGenerator_Php_Method');
  110. $method = $codeGenClass->getMethod('methodOne');
  111. $this->isInstanceOf($method, 'Zend_CodeGenerator_Php_Method');
  112. $this->assertEquals($method->getName(), 'methodOne');
  113. // add a new property
  114. $codeGenClass->setMethod(array('name' => 'methodThree'));
  115. $this->assertEquals(count($codeGenClass->getMethods()), 3);
  116. try {
  117. // add a property by a same name
  118. $codeGenClass->setMethod(array('name' => 'methodThree'));
  119. $this->fail('Zend_CodeGenerator_Php_Exception should have been thrown.');
  120. } catch (Exception $e) {
  121. $this->isInstanceOf($e, 'Zend_CodeGenerator_Php_Exception');
  122. }
  123. }
  124. public function testToString()
  125. {
  126. $codeGenClass = new Zend_CodeGenerator_Php_Class(array(
  127. 'abstract' => true,
  128. 'name' => 'SampleClass',
  129. 'extendedClass' => 'ExtendedClassName',
  130. 'implementedInterfaces' => array('Iterator', 'Traversable'),
  131. 'properties' => array(
  132. array('name' => 'foo'),
  133. array('name' => 'bar')
  134. ),
  135. 'methods' => array(
  136. array('name' => 'baz')
  137. ),
  138. ));
  139. $expectedOutput = <<<EOS
  140. abstract class SampleClass extends ExtendedClassName implements Iterator, Traversable
  141. {
  142. public \$foo = null;
  143. public \$bar = null;
  144. public function baz()
  145. {
  146. }
  147. }
  148. EOS;
  149. $output = $codeGenClass->generate();
  150. $this->assertEquals($expectedOutput, $output, $output);
  151. }
  152. }