DefaultValueTest.php 4.1 KB

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