PropertyTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. $targetSource = $property->generate();
  81. $targetSource = str_replace("\r", '', $targetSource);
  82. $this->assertEquals($expectedSource, $targetSource);
  83. }
  84. public function testPropertyCanProduceContstantModifier()
  85. {
  86. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'const' => true));
  87. $this->assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
  88. }
  89. public function testPropertyCanProduceStaticModifier()
  90. {
  91. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'static' => true));
  92. $this->assertEquals(' public static $someVal = \'some string value\';', $codeGenProperty->generate());
  93. }
  94. /**
  95. * @group ZF-6444
  96. */
  97. public function testPropertyWillLoadFromReflection()
  98. {
  99. $reflectionClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_TestClassWithManyProperties');
  100. // test property 1
  101. $reflProp = $reflectionClass->getProperty('_bazProperty');
  102. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  103. $this->assertEquals('_bazProperty', $cgProp->getName());
  104. $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue());
  105. $this->assertEquals('private', $cgProp->getVisibility());
  106. $reflProp = $reflectionClass->getProperty('_bazStaticProperty');
  107. // test property 2
  108. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  109. $this->assertEquals('_bazStaticProperty', $cgProp->getName());
  110. $this->assertEquals(Zend_CodeGenerator_Php_TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue());
  111. $this->assertTrue($cgProp->isStatic());
  112. $this->assertEquals('private', $cgProp->getVisibility());
  113. }
  114. /**
  115. * @group ZF-6444
  116. */
  117. public function testPropertyWillEmitStaticModifier()
  118. {
  119. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  120. 'name' => 'someVal',
  121. 'static' => true,
  122. 'visibility' => 'protected',
  123. 'defaultValue' => 'some string value'
  124. ));
  125. $this->assertEquals(' protected static $someVal = \'some string value\';', $codeGenProperty->generate());
  126. }
  127. /**
  128. * @group ZF-7205
  129. */
  130. public function testPropertyCanHaveDocblock()
  131. {
  132. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  133. 'name' => 'someVal',
  134. 'static' => true,
  135. 'visibility' => 'protected',
  136. 'defaultValue' => 'some string value',
  137. 'docblock' => '@var string $someVal This is some val'
  138. ));
  139. $expected = <<<EOS
  140. /**
  141. * @var string \$someVal This is some val
  142. */
  143. protected static \$someVal = 'some string value';
  144. EOS;
  145. $this->assertEquals($expected, $codeGenProperty->generate());
  146. }
  147. public function testOtherTypesThrowExceptionOnGenerate()
  148. {
  149. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  150. 'name' => 'someVal',
  151. 'defaultValue' => new stdClass(),
  152. ));
  153. $this->setExpectedException("Zend_CodeGenerator_Php_Exception");
  154. $codeGenProperty->generate();
  155. }
  156. static public function dataSetTypeSetValueGenerate()
  157. {
  158. return array(
  159. array('string', 'foo', "'foo';"),
  160. array('int', 1, "1;"),
  161. array('integer', 1, "1;"),
  162. array('bool', true, "true;"),
  163. array('bool', false, "false;"),
  164. array('boolean', true, "true;"),
  165. array('number', 1, '1;'),
  166. array('float', 1.23, '1.23;'),
  167. array('double', 1.23, '1.23;'),
  168. array('constant', 'FOO', 'FOO;'),
  169. array('null', null, 'null;'),
  170. );
  171. }
  172. /**
  173. * @dataProvider dataSetTypeSetValueGenerate
  174. * @param string $type
  175. * @param mixed $value
  176. * @param string $code
  177. */
  178. public function testSetTypeSetValueGenerate($type, $value, $code)
  179. {
  180. $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  181. $defaultValue->setType($type);
  182. $defaultValue->setValue($value);
  183. $this->assertEquals($type, $defaultValue->getType());
  184. $this->assertEquals($code, $defaultValue->generate());
  185. }
  186. /**
  187. * @dataProvider dataSetTypeSetValueGenerate
  188. * @param string $type
  189. * @param mixed $value
  190. * @param string $code
  191. */
  192. public function testSetBogusTypeSetValueGenerateUseAutoDetection($type, $value, $code)
  193. {
  194. if($type == 'constant') {
  195. return; // constant can only be detected explicitly
  196. }
  197. $defaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  198. $defaultValue->setType("bogus");
  199. $defaultValue->setValue($value);
  200. $this->assertEquals($code, $defaultValue->generate());
  201. }
  202. /**
  203. * @group ZF-8849
  204. */
  205. public function testZF8849()
  206. {
  207. $property = new Zend_CodeGenerator_Php_Property(array(
  208. 'defaultValue' => array('value' => 1.337, 'type' => 'string'),
  209. 'name' => 'ZF8849',
  210. 'const' => true
  211. ));
  212. $this->assertEquals(
  213. $property->generate(),
  214. " const ZF8849 = '1.337';"
  215. );
  216. }
  217. }