TableComplexIdTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.7.4
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Datasource\ConnectionManager;
  17. use Cake\ORM\Entity;
  18. use Cake\TestSuite\TestCase;
  19. use Cake\Utility\Text;
  20. use DateTime;
  21. /**
  22. * Integration tests for Table class with uuid primary keys.
  23. */
  24. class TableComplexIdTest extends TestCase
  25. {
  26. /**
  27. * Fixtures
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.DateKeys',
  33. ];
  34. /**
  35. * setup
  36. *
  37. * @return void
  38. */
  39. public function setUp()
  40. {
  41. parent::setUp();
  42. $this->connection = ConnectionManager::get('test');
  43. static::setAppNamespace();
  44. }
  45. /**
  46. * teardown
  47. *
  48. * @return void
  49. */
  50. public function tearDown()
  51. {
  52. parent::tearDown();
  53. $this->getTableLocator()->clear();
  54. }
  55. /**
  56. * Test saving new records sets uuids
  57. *
  58. * @return void
  59. */
  60. public function testSaveNew()
  61. {
  62. $now = new DateTime('now');
  63. $entity = new Entity([
  64. 'id' => $now,
  65. 'title' => 'shiny new',
  66. ]);
  67. $table = $this->getTableLocator()->get('DateKeys');
  68. $this->assertSame($entity, $table->save($entity));
  69. $this->assertEquals($now, $entity->id);
  70. $row = $table->find('all')->where(['id' => $entity->id])->first();
  71. $this->assertEquals($row->id->format('Y-m-d'), $entity->id->format('Y-m-d'));
  72. }
  73. /**
  74. * Test saving existing records works
  75. *
  76. * @return void
  77. */
  78. public function testSaveUpdate()
  79. {
  80. $id = new DateTime('now');
  81. $entity = new Entity([
  82. 'id' => $id,
  83. 'title' => 'shiny update',
  84. ]);
  85. $table = $this->getTableLocator()->get('DateKeys');
  86. $this->assertSame($entity, $table->save($entity));
  87. $this->assertEquals($id, $entity->id, 'Should match');
  88. $row = $table->find('all')->where(['id' => $entity->id])->first();
  89. $row->title = 'things';
  90. $this->assertSame($row, $table->save($row));
  91. }
  92. /**
  93. * Test delete with string pk.
  94. *
  95. * @return void
  96. */
  97. public function testDelete()
  98. {
  99. $table = $this->getTableLocator()->get('DateKeys');
  100. $entity = new Entity([
  101. 'id' => new DateTime('now'),
  102. 'title' => 'shiny update',
  103. ]);
  104. $table->save($entity);
  105. $this->assertTrue($table->delete($entity));
  106. $query = $table->find('all')->where(['id' => $entity->id]);
  107. $this->assertEmpty($query->first(), 'No row left');
  108. }
  109. }