PageRenderTime 50ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/Propel/Tests/Generator/Builder/Om/GeneratedObjectEnumColumnTypeTest.php

http://github.com/propelorm/Propel2
PHP | 174 lines | 113 code | 15 blank | 46 comment | 1 complexity | 8b3da179004758f2c0d21fe51562c2de MD5 | raw file
  1. <?php
  2. /**
  3. * MIT License. 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. namespace Propel\Tests\Generator\Builder\Om;
  8. use ComplexColumnTypeEntity3;
  9. use ComplexColumnTypeEntity3Query;
  10. use Map\ComplexColumnTypeEntity3TableMap;
  11. use Propel\Generator\Util\QuickBuilder;
  12. use Propel\Runtime\Exception\PropelException;
  13. use Propel\Tests\TestCase;
  14. use PublicComplexColumnTypeEntity3;
  15. /**
  16. * Tests the generated objects for enum column types accessor & mutator
  17. *
  18. * @author Francois Zaninotto
  19. */
  20. class GeneratedObjectEnumColumnTypeTest extends TestCase
  21. {
  22. /**
  23. * @return void
  24. */
  25. public function setUp(): void
  26. {
  27. if (!class_exists('ComplexColumnTypeEntity3')) {
  28. $schema = <<<EOF
  29. <database name="generated_object_complex_type_test_3">
  30. <table name="complex_column_type_entity_3">
  31. <column name="id" primaryKey="true" type="INTEGER" autoIncrement="true"/>
  32. <column name="bar" type="ENUM" valueSet="foo, bar, baz, 1, 4,(, foo bar "/>
  33. <column name="bar2" type="ENUM" valueSet="foo, bar" defaultValue="bar"/>
  34. </table>
  35. </database>
  36. EOF;
  37. QuickBuilder::buildSchema($schema);
  38. // ok this is hackish but it makes testing of getter and setter independent of each other
  39. $publicAccessorCode = <<<EOF
  40. class PublicComplexColumnTypeEntity3 extends ComplexColumnTypeEntity3
  41. {
  42. public \$bar;
  43. }
  44. EOF;
  45. eval($publicAccessorCode);
  46. }
  47. }
  48. /**
  49. * @return void
  50. */
  51. public function testGetter()
  52. {
  53. $this->assertTrue(method_exists('ComplexColumnTypeEntity3', 'getBar'));
  54. $e = new ComplexColumnTypeEntity3();
  55. $this->assertNull($e->getBar());
  56. $e = new PublicComplexColumnTypeEntity3();
  57. $e->bar = 0;
  58. $this->assertEquals('foo', $e->getBar());
  59. $e->bar = 3;
  60. $this->assertEquals('1', $e->getBar());
  61. $e->bar = 6;
  62. $this->assertEquals('foo bar', $e->getBar());
  63. }
  64. /**
  65. * @return void
  66. */
  67. public function testGetterThrowsExceptionOnUnknownKey()
  68. {
  69. $this->expectException(PropelException::class);
  70. $e = new PublicComplexColumnTypeEntity3();
  71. $e->bar = 156;
  72. $e->getBar();
  73. }
  74. /**
  75. * @return void
  76. */
  77. public function testGetterDefaultValue()
  78. {
  79. $e = new PublicComplexColumnTypeEntity3();
  80. $this->assertEquals('bar', $e->getBar2());
  81. }
  82. /**
  83. * @return void
  84. */
  85. public function testSetter()
  86. {
  87. $this->assertTrue(method_exists('\ComplexColumnTypeEntity3', 'setBar'));
  88. $e = new PublicComplexColumnTypeEntity3();
  89. $e->setBar('foo');
  90. $this->assertEquals(0, $e->bar);
  91. $e->setBar(1);
  92. $this->assertEquals(3, $e->bar);
  93. $e->setBar('1');
  94. $this->assertEquals(3, $e->bar);
  95. $e->setBar('foo bar');
  96. $this->assertEquals(6, $e->bar);
  97. }
  98. /**
  99. * @return void
  100. */
  101. public function testSetterThrowsExceptionOnUnknownValue()
  102. {
  103. $this->expectException(PropelException::class);
  104. $e = new ComplexColumnTypeEntity3();
  105. $e->setBar('bazz');
  106. }
  107. /**
  108. * @return void
  109. */
  110. public function testValueIsPersisted()
  111. {
  112. $e = new ComplexColumnTypeEntity3();
  113. $e->setBar('baz');
  114. $e->save();
  115. ComplexColumnTypeEntity3TableMap::clearInstancePool();
  116. $e = ComplexColumnTypeEntity3Query::create()->findOne();
  117. $this->assertEquals('baz', $e->getBar());
  118. }
  119. /**
  120. * @return void
  121. */
  122. public function testValueIsCopied()
  123. {
  124. $e1 = new ComplexColumnTypeEntity3();
  125. $e1->setBar('baz');
  126. $e2 = new ComplexColumnTypeEntity3();
  127. $e1->copyInto($e2);
  128. $this->assertEquals('baz', $e2->getBar());
  129. }
  130. /**
  131. * @see https://github.com/propelorm/Propel/issues/139
  132. *
  133. * @return void
  134. */
  135. public function testSetterWithSameValueDoesNotUpdateObject()
  136. {
  137. $e = new ComplexColumnTypeEntity3();
  138. $e->setBar('baz');
  139. $e->resetModified();
  140. $e->setBar('baz');
  141. $this->assertFalse($e->isModified());
  142. }
  143. /**
  144. * @see https://github.com/propelorm/Propel/issues/139
  145. *
  146. * @return void
  147. */
  148. public function testSetterWithSameValueDoesNotUpdateHydratedObject()
  149. {
  150. $e = new ComplexColumnTypeEntity3();
  151. $e->setBar('baz');
  152. $e->save();
  153. // force hydration
  154. ComplexColumnTypeEntity3TableMap::clearInstancePool();
  155. $e = ComplexColumnTypeEntity3Query::create()->findPk($e->getPrimaryKey());
  156. $e->setBar('baz');
  157. $this->assertFalse($e->isModified());
  158. }
  159. }