PropertyTest.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. 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-2009 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. );
  63. $expectedSource = <<<EOS
  64. public \$myFoo = array(
  65. 5,
  66. 'one' => 1,
  67. 'two' => '2'
  68. );
  69. EOS;
  70. $property = new Zend_CodeGenerator_Php_Property(array(
  71. 'name' => 'myFoo',
  72. 'defaultValue' => $targetValue
  73. ));
  74. $this->assertEquals($expectedSource, $property->generate());
  75. }
  76. public function testPropertyCanProduceContstantModifier()
  77. {
  78. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'const' => true));
  79. $this->assertEquals(' const someVal = \'some string value\';', $codeGenProperty->generate());
  80. }
  81. public function testPropertyCanProduceStaticModifier()
  82. {
  83. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array('name' => 'someVal', 'defaultValue' => 'some string value', 'static' => true));
  84. $this->assertEquals(' public static $someVal = \'some string value\';', $codeGenProperty->generate());
  85. }
  86. /**
  87. * @group ZF-6444
  88. */
  89. public function testPropertyWillLoadFromReflection()
  90. {
  91. $reflectionClass = new Zend_Reflection_Class('Zend_CodeGenerator_Php_TestClassWithManyProperties');
  92. // test property 1
  93. $reflProp = $reflectionClass->getProperty('_bazProperty');
  94. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  95. $this->assertEquals('_bazProperty', $cgProp->getName());
  96. $this->assertEquals(array(true, false, true), $cgProp->getDefaultValue()->getValue());
  97. $this->assertEquals('private', $cgProp->getVisibility());
  98. $reflProp = $reflectionClass->getProperty('_bazStaticProperty');
  99. // test property 2
  100. $cgProp = Zend_CodeGenerator_Php_Property::fromReflection($reflProp);
  101. $this->assertEquals('_bazStaticProperty', $cgProp->getName());
  102. $this->assertEquals(Zend_CodeGenerator_Php_TestClassWithManyProperties::FOO, $cgProp->getDefaultValue()->getValue());
  103. $this->assertTrue($cgProp->isStatic());
  104. $this->assertEquals('private', $cgProp->getVisibility());
  105. }
  106. /**
  107. * @group ZF-6444
  108. */
  109. public function testPropertyWillEmitStaticModifier()
  110. {
  111. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  112. 'name' => 'someVal',
  113. 'static' => true,
  114. 'visibility' => 'protected',
  115. 'defaultValue' => 'some string value'
  116. ));
  117. $this->assertEquals(' protected static $someVal = \'some string value\';', $codeGenProperty->generate());
  118. }
  119. /**
  120. * @group ZF-7205
  121. */
  122. public function testPropertyCanHaveDocblock()
  123. {
  124. $codeGenProperty = new Zend_CodeGenerator_Php_Property(array(
  125. 'name' => 'someVal',
  126. 'static' => true,
  127. 'visibility' => 'protected',
  128. 'defaultValue' => 'some string value',
  129. 'docblock' => '@var string $someVal This is some val'
  130. ));
  131. $expected = <<<EOS
  132. /**
  133. * @var string \$someVal This is some val
  134. */
  135. protected static \$someVal = 'some string value';
  136. EOS;
  137. $this->assertEquals($expected, $codeGenProperty->generate());
  138. }
  139. }