DefaultValueTest.php 4.1 KB

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