PropertyTest.php 7.9 KB

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