TableUuidTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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.0.0
  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\ORM\TableRegistry;
  19. use Cake\TestSuite\TestCase;
  20. use Cake\Utility\Text;
  21. /**
  22. * Integration tests for Table class with uuid primary keys.
  23. */
  24. class TableUuidTest extends TestCase
  25. {
  26. /**
  27. * Fixtures
  28. *
  29. * @var array
  30. */
  31. public $fixtures = [
  32. 'core.uuiditems', 'core.uuidportfolios'
  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. TableRegistry::clear();
  54. }
  55. /**
  56. * Test saving new records sets uuids
  57. *
  58. * @return void
  59. */
  60. public function testSaveNew()
  61. {
  62. $entity = new Entity([
  63. 'name' => 'shiny new',
  64. 'published' => true,
  65. ]);
  66. $table = TableRegistry::get('uuiditems');
  67. $this->assertSame($entity, $table->save($entity));
  68. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  69. $row = $table->find('all')->where(['id' => $entity->id])->first();
  70. $row->id = strtolower($row->id);
  71. $this->assertEquals($entity->toArray(), $row->toArray());
  72. }
  73. /**
  74. * Test saving new records allows manual uuids
  75. *
  76. * @return void
  77. */
  78. public function testSaveNewSpecificId()
  79. {
  80. $id = Text::uuid();
  81. $entity = new Entity([
  82. 'id' => $id,
  83. 'name' => 'shiny and new',
  84. 'published' => true,
  85. ]);
  86. $table = TableRegistry::get('uuiditems');
  87. $this->assertSame($entity, $table->save($entity));
  88. $this->assertSame($id, $entity->id);
  89. $row = $table->find('all')->where(['id' => $id])->first();
  90. $this->assertNotEmpty($row);
  91. $this->assertSame($id, strtolower($row->id));
  92. $this->assertSame($entity->name, $row->name);
  93. }
  94. /**
  95. * Test saving existing records works
  96. *
  97. * @return void
  98. */
  99. public function testSaveUpdate()
  100. {
  101. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  102. $entity = new Entity([
  103. 'id' => $id,
  104. 'name' => 'shiny update',
  105. 'published' => true,
  106. ]);
  107. $table = TableRegistry::get('uuiditems');
  108. $this->assertSame($entity, $table->save($entity));
  109. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  110. $row = $table->find('all')->where(['id' => $entity->id])->first();
  111. $row->id = strtolower($row->id);
  112. $this->assertEquals($entity->toArray(), $row->toArray());
  113. }
  114. /**
  115. * Test delete with string pk.
  116. *
  117. * @return void
  118. */
  119. public function testDelete()
  120. {
  121. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  122. $table = TableRegistry::get('uuiditems');
  123. $entity = $table->find('all')->where(['id' => $id])->first();
  124. $this->assertTrue($table->delete($entity));
  125. $query = $table->find('all')->where(['id' => $id]);
  126. $this->assertCount(0, $query->execute(), 'No rows left');
  127. }
  128. /**
  129. * Tests that sql server does not error when an empty uuid is bound
  130. *
  131. * @return void
  132. */
  133. public function testEmptyUuid()
  134. {
  135. $id = '';
  136. $table = TableRegistry::get('uuiditems');
  137. $entity = $table->find('all')
  138. ->where(['id' => $id])
  139. ->first();
  140. $this->assertNull($entity);
  141. }
  142. }