DefaultValueTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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/DefaultValue.php';
  26. /**
  27. * @category Zend
  28. * @package Zend_CodeGenerator
  29. * @subpackage UnitTests
  30. * @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
  31. * @license http://framework.zend.com/license/new-bsd New BSD License
  32. *
  33. * @group Zend_CodeGenerator_Php
  34. */
  35. class Zend_CodeGenerator_Php_Property_DefaultValueTest extends PHPUnit_Framework_TestCase
  36. {
  37. public function testPropertyDefaultValueConstructor()
  38. {
  39. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  40. $this->isInstanceOf($propDefaultValue, 'Zend_CodeGenerator_Php_Property_DefaultValue');
  41. }
  42. public function testPropertyDefaultValueIsSettable()
  43. {
  44. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  45. $propDefaultValue->setValue('foo');
  46. $this->assertEquals('foo', $propDefaultValue->getValue());
  47. //$this->assertEquals('\'foo\';', $propDefaultValue->generate());
  48. }
  49. public function testPropertyDefaultValueCanHandleStrings()
  50. {
  51. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  52. $propDefaultValue->setValue('foo');
  53. $this->assertEquals('\'foo\';', $propDefaultValue->generate());
  54. }
  55. public function testPropertyDefaultValueCanHandleArray()
  56. {
  57. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  58. $propDefaultValue->setValue(array('foo'));
  59. $this->assertEquals('array(\'foo\');', $propDefaultValue->generate());
  60. }
  61. public function testPropertyDefaultValueCanHandleUnquotedString()
  62. {
  63. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  64. $propDefaultValue->setValue('PHP_EOL');
  65. $propDefaultValue->setType('constant');
  66. $this->assertEquals('PHP_EOL;', $propDefaultValue->generate());
  67. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  68. $propDefaultValue->setValue(5);
  69. $this->assertEquals('5;', $propDefaultValue->generate());
  70. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  71. $propDefaultValue->setValue(5.25);
  72. $this->assertEquals('5.25;', $propDefaultValue->generate());
  73. }
  74. public function testPropertyDefaultValueCanHandleComplexArrayOfTypes()
  75. {
  76. $targetValue = array(
  77. 5,
  78. 'one' => 1,
  79. 'two' => '2',
  80. array(
  81. 'foo',
  82. 'bar',
  83. array(
  84. 'baz1',
  85. 'baz2'
  86. )
  87. ),
  88. new Zend_CodeGenerator_Php_Property_DefaultValue(array('value' => 'PHP_EOL', 'type' => 'constant'))
  89. );
  90. $expectedSource = <<<EOS
  91. array(
  92. 5,
  93. 'one' => 1,
  94. 'two' => '2',
  95. array(
  96. 'foo',
  97. 'bar',
  98. array(
  99. 'baz1',
  100. 'baz2'
  101. )
  102. ),
  103. PHP_EOL
  104. );
  105. EOS;
  106. $propDefaultValue = new Zend_CodeGenerator_Php_Property_DefaultValue();
  107. $propDefaultValue->setValue($targetValue);
  108. $generatedTargetSource = $propDefaultValue->generate();
  109. $this->assertEquals($expectedSource, $generatedTargetSource);
  110. }
  111. }