TableUuidTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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.binary_uuiditems',
  33. 'core.uuiditems',
  34. ];
  35. /**
  36. * setup
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. parent::setUp();
  43. $this->connection = ConnectionManager::get('test');
  44. static::setAppNamespace();
  45. }
  46. /**
  47. * teardown
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. TableRegistry::clear();
  55. }
  56. /**
  57. * Provider for testing that string and binary uuids work the same
  58. *
  59. * @return array
  60. */
  61. public function uuidTableProvider()
  62. {
  63. return [['uuiditems'], ['binary_uuiditems']];
  64. }
  65. /**
  66. * Test saving new records sets uuids
  67. *
  68. * @dataProvider uuidTableProvider
  69. * @return void
  70. */
  71. public function testSaveNew($tableName)
  72. {
  73. $entity = new Entity([
  74. 'name' => 'shiny new',
  75. 'published' => true,
  76. ]);
  77. $table = TableRegistry::get($tableName);
  78. $this->assertSame($entity, $table->save($entity));
  79. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  80. $row = $table->find('all')->where(['id' => $entity->id])->first();
  81. $row->id = strtolower($row->id);
  82. $this->assertEquals($entity->toArray(), $row->toArray());
  83. }
  84. /**
  85. * Test saving new records allows manual uuids
  86. *
  87. * @dataProvider uuidTableProvider
  88. * @return void
  89. */
  90. public function testSaveNewSpecificId($tableName)
  91. {
  92. $id = Text::uuid();
  93. $entity = new Entity([
  94. 'id' => $id,
  95. 'name' => 'shiny and new',
  96. 'published' => true,
  97. ]);
  98. $table = TableRegistry::get($tableName);
  99. $this->assertSame($entity, $table->save($entity));
  100. $this->assertSame($id, $entity->id);
  101. $row = $table->find('all')->where(['id' => $id])->first();
  102. $this->assertNotEmpty($row);
  103. $this->assertSame($id, strtolower($row->id));
  104. $this->assertSame($entity->name, $row->name);
  105. }
  106. /**
  107. * Test saving existing records works
  108. *
  109. * @dataProvider uuidTableProvider
  110. * @return void
  111. */
  112. public function testSaveUpdate($tableName)
  113. {
  114. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  115. $entity = new Entity([
  116. 'id' => $id,
  117. 'name' => 'shiny update',
  118. 'published' => true,
  119. ]);
  120. $table = TableRegistry::get($tableName);
  121. $this->assertSame($entity, $table->save($entity));
  122. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  123. $row = $table->find('all')->where(['id' => $entity->id])->first();
  124. $row->id = strtolower($row->id);
  125. $this->assertEquals($entity->toArray(), $row->toArray());
  126. }
  127. /**
  128. * Test delete with string pk.
  129. *
  130. * @dataProvider uuidTableProvider
  131. * @return void
  132. */
  133. public function testGetById($tableName)
  134. {
  135. $table = TableRegistry::get($tableName);
  136. $entity = $table->find('all')->firstOrFail();
  137. $result = $table->get($entity->id);
  138. $this->assertSame($result->id, $entity->id);
  139. }
  140. /**
  141. * Test delete with string pk.
  142. *
  143. * @dataProvider uuidTableProvider
  144. * @return void
  145. */
  146. public function testDelete($tableName)
  147. {
  148. $table = TableRegistry::get($tableName);
  149. $entity = $table->find('all')->firstOrFail();
  150. $this->assertTrue($table->delete($entity));
  151. $query = $table->find('all')->where(['id' => $entity->id]);
  152. $this->assertEmpty($query->first(), 'No row left');
  153. }
  154. /**
  155. * Tests that sql server does not error when an empty uuid is bound
  156. *
  157. * @dataProvider uuidTableProvider
  158. * @return void
  159. */
  160. public function testEmptyUuid($tableName)
  161. {
  162. $id = '';
  163. $table = TableRegistry::get($tableName);
  164. $entity = $table->find('all')
  165. ->where(['id' => $id])
  166. ->first();
  167. $this->assertNull($entity);
  168. }
  169. }