PageRenderTime 55ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/test/testsuite/generator/builder/om/GeneratedObjectEnumColumnTypeTest.php

http://github.com/propelorm/Propel
PHP | 144 lines | 105 code | 12 blank | 27 comment | 1 complexity | be1bda654d730c98c87f51b2a07ab1d0 MD5 | raw file
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. require_once dirname(__FILE__) . '/../../../../../generator/lib/util/PropelQuickBuilder.php';
  10. require_once dirname(__FILE__) . '/../../../../../runtime/lib/Propel.php';
  11. /**
  12. * Tests the generated objects for enum column types accessor & mutator
  13. *
  14. * @author Francois Zaninotto
  15. * @package generator.builder.om
  16. */
  17. class GeneratedObjectEnumColumnTypeTest extends PHPUnit_Framework_TestCase
  18. {
  19. public function setUp()
  20. {
  21. if (!class_exists('ComplexColumnTypeEntity3')) {
  22. $schema = <<<EOF
  23. <database name="generated_object_complex_type_test_3">
  24. <table name="complex_column_type_entity_3">
  25. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true" />
  26. <column name="bar" type="ENUM" valueSet="foo, bar, baz, 1, 4,(, foo bar " />
  27. <column name="bar2" type="ENUM" valueSet="foo, bar" defaultValue="bar" />
  28. </table>
  29. </database>
  30. EOF;
  31. PropelQuickBuilder::buildSchema($schema);
  32. // ok this is hackish but it makes testing of getter and setter independent of each other
  33. $publicAccessorCode = <<<EOF
  34. class PublicComplexColumnTypeEntity3 extends ComplexColumnTypeEntity3
  35. {
  36. public \$bar;
  37. }
  38. EOF;
  39. eval($publicAccessorCode);
  40. }
  41. }
  42. public function testGetter()
  43. {
  44. $this->assertTrue(method_exists('ComplexColumnTypeEntity3', 'getBar'));
  45. $e = new ComplexColumnTypeEntity3();
  46. $this->assertNull($e->getBar());
  47. $e = new PublicComplexColumnTypeEntity3();
  48. $e->bar = 0;
  49. $this->assertEquals('foo', $e->getBar());
  50. $e->bar = 3;
  51. $this->assertEquals('1', $e->getBar());
  52. $e->bar = 6;
  53. $this->assertEquals('foo bar', $e->getBar());
  54. }
  55. /**
  56. * @expectedException PropelException
  57. */
  58. public function testGetterThrowsExceptionOnUnknownKey()
  59. {
  60. $e = new PublicComplexColumnTypeEntity3();
  61. $e->bar = 156;
  62. $e->getBar();
  63. }
  64. public function testGetterDefaultValue()
  65. {
  66. $e = new PublicComplexColumnTypeEntity3();
  67. $this->assertEquals('bar', $e->getBar2());
  68. }
  69. public function testSetter()
  70. {
  71. $this->assertTrue(method_exists('ComplexColumnTypeEntity3', 'setBar'));
  72. $e = new PublicComplexColumnTypeEntity3();
  73. $e->setBar('foo');
  74. $this->assertEquals(0, $e->bar);
  75. $e->setBar(1);
  76. $this->assertEquals(3, $e->bar);
  77. $e->setBar('1');
  78. $this->assertEquals(3, $e->bar);
  79. $e->setBar('foo bar');
  80. $this->assertEquals(6, $e->bar);
  81. }
  82. /**
  83. * @expectedException PropelException
  84. */
  85. public function testSetterThrowsExceptionOnUnknownValue()
  86. {
  87. $e = new ComplexColumnTypeEntity3();
  88. $e->setBar('bazz');
  89. }
  90. public function testValueIsPersisted()
  91. {
  92. $e = new ComplexColumnTypeEntity3();
  93. $e->setBar('baz');
  94. $e->save();
  95. ComplexColumnTypeEntity3Peer::clearInstancePool();
  96. $e = ComplexColumnTypeEntity3Query::create()->findOne();
  97. $this->assertEquals('baz', $e->getBar());
  98. }
  99. public function testValueIsCopied()
  100. {
  101. $e1 = new ComplexColumnTypeEntity3();
  102. $e1->setBar('baz');
  103. $e2 = new ComplexColumnTypeEntity3();
  104. $e1->copyInto($e2);
  105. $this->assertEquals('baz', $e2->getBar());
  106. }
  107. /**
  108. * @see https://github.com/propelorm/Propel/issues/139
  109. */
  110. public function testSetterWithSameValueDoesNotUpdateObject()
  111. {
  112. $e = new ComplexColumnTypeEntity3();
  113. $e->setBar('baz');
  114. $e->resetModified();
  115. $e->setBar('baz');
  116. $this->assertFalse($e->isModified());
  117. }
  118. /**
  119. * @see https://github.com/propelorm/Propel/issues/139
  120. */
  121. public function testSetterWithSameValueDoesNotUpdateHydratedObject()
  122. {
  123. $e = new ComplexColumnTypeEntity3();
  124. $e->setBar('baz');
  125. $e->save();
  126. // force hydration
  127. ComplexColumnTypeEntity3Peer::clearInstancePool();
  128. $e = ComplexColumnTypeEntity3Query::create()->findPk($e->getPrimaryKey());
  129. $e->setBar('baz');
  130. $this->assertFalse($e->isModified());
  131. }
  132. }