PropertyTest.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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. require_once 'Zend/CodeGenerator/Php/Property.php';
  27. require_once 'Zend/Reflection/Class.php';
  28. /**
  29. * @category Zend
  30. * @package Zend_CodeGenerator
  31. * @subpackage UnitTests
  32. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  33. * @license http://framework.zend.com/license/new-bsd New BSD License
  34. *
  35. * @group Zend_CodeGenerator
  36. * @group Zend_CodeGenerator_Php
  37. */
  38. class Zend_CodeGenerator_Php_PropertyTest extends PHPUnit_Framework_TestCase
  39. {
  40. public function setup()
  41. {
  42. if (!class_exists('Zend_CodeGenerator_Php_TestClassWithManyProperties')) {
  43. require_once dirname(__FILE__) . '/_files/TestClassWithManyProperties.php';
  44. }
  45. }
  46. public function testPropertyConstructor()
  47. {
  48. $codeGenProperty = new Zend_CodeGenerator_Php_Property();
  49. $this->isInstanceOf($codeGenProperty, 'Zend_CodeGenerator_Php_Property');
  50. }
  51. public function testPropertyReturnsSimpleValue()
  52. {
  53. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value'));
  54. $this->assertEquals(' public $someVal = \'some string value\';', $codeGenProperty->generate());
  55. }
  56. public function testPropertyMultilineValue()
  57. {
  58. $targetValue = array(
  59. 5,
  60. 'one' => 1,
  61. 'two' => '2',
  62. 'null' => null,
  63. 'true' => true,
  64. "bar's" => "bar's",
  65. );
  66. $expectedSource = <<<EOS
  67. public \$myFoo = array(
  68. 5,
  69. 'one' => 1,
  70. 'two' => '2',
  71. 'null' => null,
  72. 'true' => true,
  73. 'bar\'s' => 'bar\'s'
  74. );
  75. EOS;
  76. $property = new Zend_CodeGenerator_Php_Property(array(
  77. 'name' => 'myFoo',
  78. 'defaultValue' => $targetValue
  79. ));
  80. $this->assertEquals($expectedSource, $property->generate());
  81. }
  82. public function testPropertyCanProduceContstantModifier()
  83. {
  84. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'const' => true));
  85. $this->assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
  86. }
  87. public function testPropertyCanProduceStaticModifier()
  88. {
  89. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'static' => true));
  90. $this->assertEquals(' public static $someVal = \'some string value\';', $codeGenProperty->generate());
  91. }
  92. /**
  93. * @group ZF-6444
  94. */
  95. public function testPropertyWillLoadFromReflection()
  96. {
  97. $reflectionClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_TestClassWithManyProperties');
  98. // test property 1
  99. $reflProp = $reflectionClass->getProperty('_bazProperty');
  100. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  101. $this->assertEquals('_bazProperty', $cgProp->getName());
  102. $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue());
  103. $this->assertEquals('private', $cgProp->getVisibility());
  104. $reflProp = $reflectionClass->getProperty('_bazStaticProperty');
  105. // test property 2
  106. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  107. $this->assertEquals('_bazStaticProperty', $cgProp->getName());
  108. $this->assertEquals(Zend_CodeGenerator_Php_TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue());
  109. $this->assertTrue($cgProp->isStatic());
  110. $this->assertEquals('private', $cgProp->getVisibility());
  111. }
  112. /**
  113. * @group ZF-6444
  114. */
  115. public function testPropertyWillEmitStaticModifier()
  116. {
  117. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  118. 'name' => 'someVal',
  119. 'static' => true,
  120. 'visibility' => 'protected',
  121. 'defaultValue' => 'some string value'
  122. ));
  123. $this->assertEquals(' protected static $someVal = \'some string value\';', $codeGenProperty->generate());
  124. }
  125. /**
  126. * @group ZF-7205
  127. */
  128. public function testPropertyCanHaveDocblock()
  129. {
  130. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  131. 'name' => 'someVal',
  132. 'static' => true,
  133. 'visibility' => 'protected',
  134. 'defaultValue' => 'some string value',
  135. 'docblock' => '@var string $someVal This is some val'
  136. ));
  137. $expected = <<<EOS
  138. /**
  139. * @var string \$someVal This is some val
  140. */
  141. protected static \$someVal = 'some string value';
  142. EOS;
  143. $this->assertEquals($expected, $codeGenProperty->generate());
  144. }
  145. public function testOtherTypesThrowExceptionOnGenerate()
  146. {
  147. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  148. 'name' => 'someVal',
  149. 'defaultValue' => new stdClass(),
  150. ));
  151. $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
  152. $codeGenProperty->generate();
  153. }
  154. static public function dataSetTypeSetValueGenerate()
  155. {
  156. return array(
  157. array('string', 'foo', "'foo';"),
  158. array('int', 1, "1;"),
  159. array('integer', 1, "1;"),
  160. array('bool', true, "true;"),
  161. array('bool', false, "false;"),
  162. array('boolean', true, "true;"),
  163. array('number', 1, '1;'),
  164. array('float', 1.23, '1.23;'),
  165. array('double', 1.23, '1.23;'),
  166. array('constant', 'FOO', 'FOO;'),
  167. array('null', null, 'null;'),
  168. );
  169. }
  170. /**
  171. * @dataProvider dataSetTypeSetValueGenerate
  172. * @param string $type
  173. * @param mixed $value
  174. * @param string $code
  175. */
  176. public function testSetTypeSetValueGenerate($type, $value, $code)
  177. {
  178. $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  179. $defaultValue->setType($type);
  180. $defaultValue->setValue($value);
  181. $this->assertEquals($type, $defaultValue->getType());
  182. $this->assertEquals($code, $defaultValue->generate());
  183. }
  184. /**
  185. * @dataProvider dataSetTypeSetValueGenerate
  186. * @param string $type
  187. * @param mixed $value
  188. * @param string $code
  189. */
  190. public function testSetBogusTypeSetValueGenerateUseAutoDetection($type, $value, $code)
  191. {
  192. if($type == 'constant') {
  193. return; // constant can only be detected explicitly
  194. }
  195. $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  196. $defaultValue->setType("bogus");
  197. $defaultValue->setValue($value);
  198. $this->assertEquals($code, $defaultValue->generate());
  199. }
  200. }