PropertyTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. require_once 'Zend/CodeGenerator/Php/Property.php';
  26. require_once 'Zend/Reflection/Class.php';
  27. /**
  28. * @category Zend
  29. * @package Zend_CodeGenerator
  30. * @subpackage UnitTests
  31. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  32. * @license http://framework.zend.com/license/new-bsd New BSD License
  33. *
  34. * @group Zend_CodeGenerator_Php
  35. */
  36. class Zend_CodeGenerator_Php_PropertyTest extends PHPUnit_Framework_TestCase
  37. {
  38. public function setup()
  39. {
  40. if (!class_exists('Zend_CodeGenerator_Php_TestClassWithManyProperties')) {
  41. require_once dirname(__FILE__) . '/_files/TestClassWithManyProperties.php';
  42. }
  43. }
  44. public function testPropertyConstructor()
  45. {
  46. $codeGenProperty = new Zend_CodeGenerator_Php_Property();
  47. $this->isInstanceOf($codeGenProperty, 'Zend_CodeGenerator_Php_Property');
  48. }
  49. public function testPropertyReturnsSimpleValue()
  50. {
  51. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value'));
  52. $this->assertEquals(' public $someVal = \'some string value\';', $codeGenProperty->generate());
  53. }
  54. public function testPropertyMultilineValue()
  55. {
  56. $targetValue = array(
  57. 5,
  58. 'one' => 1,
  59. 'two' => '2',
  60. );
  61. $expectedSource = <<<EOS
  62. public \$myFoo = array(
  63. 5,
  64. 'one' => 1,
  65. 'two' => '2'
  66. );
  67. EOS;
  68. $property = new Zend_CodeGenerator_Php_Property(array(
  69. 'name' => 'myFoo',
  70. 'defaultValue' => $targetValue
  71. ));
  72. $this->assertEquals($expectedSource, $property->generate());
  73. }
  74. public function testPropertyCanProduceContstantModifier()
  75. {
  76. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'const' => true));
  77. $this->assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
  78. }
  79. public function testPropertyCanProduceStaticModifier()
  80. {
  81. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'static' => true));
  82. $this->assertEquals(' public static $someVal = \'some string value\';', $codeGenProperty->generate());
  83. }
  84. /**
  85. * @group ZF-6444
  86. */
  87. public function testPropertyWillLoadFromReflection()
  88. {
  89. $reflectionClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_TestClassWithManyProperties');
  90. // test property 1
  91. $reflProp = $reflectionClass->getProperty('_bazProperty');
  92. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  93. $this->assertEquals('_bazProperty', $cgProp->getName());
  94. $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue());
  95. $this->assertEquals('private', $cgProp->getVisibility());
  96. $reflProp = $reflectionClass->getProperty('_bazStaticProperty');
  97. // test property 2
  98. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  99. $this->assertEquals('_bazStaticProperty', $cgProp->getName());
  100. $this->assertEquals(Zend_CodeGenerator_Php_TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue());
  101. $this->assertTrue($cgProp->isStatic());
  102. $this->assertEquals('private', $cgProp->getVisibility());
  103. }
  104. /**
  105. * @group ZF-6444
  106. */
  107. public function testPropertyWillEmitStaticModifier()
  108. {
  109. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  110. 'name' => 'someVal',
  111. 'static' => true,
  112. 'visibility' => 'protected',
  113. 'defaultValue' => 'some string value'
  114. ));
  115. $this->assertEquals(' protected static $someVal = \'some string value\';', $codeGenProperty->generate());
  116. }
  117. /**
  118. * @group ZF-7205
  119. */
  120. public function testPropertyCanHaveDocblock()
  121. {
  122. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  123. 'name' => 'someVal',
  124. 'static' => true,
  125. 'visibility' => 'protected',
  126. 'defaultValue' => 'some string value',
  127. 'docblock' => '@var string $someVal This is some val'
  128. ));
  129. $expected = <<<EOS
  130. /**
  131. * @var string \$someVal This is some val
  132. */
  133. protected static \$someVal = 'some string value';
  134. EOS;
  135. $this->assertEquals($expected, $codeGenProperty->generate());
  136. }
  137. }