TableUuidTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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\TestSuite\TestCase;
  19. use Cake\Utility\Text;
  20. /**
  21. * Integration tests for Table class with uuid primary keys.
  22. */
  23. class TableUuidTest extends TestCase
  24. {
  25. /**
  26. * Fixtures
  27. *
  28. * @var array
  29. */
  30. public $fixtures = [
  31. 'core.BinaryUuiditems',
  32. 'core.Uuiditems',
  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. * Provider for testing that string and binary uuids work the same
  57. *
  58. * @return array
  59. */
  60. public function uuidTableProvider()
  61. {
  62. return [['uuiditems'], ['binary_uuiditems']];
  63. }
  64. /**
  65. * Test saving new records sets uuids
  66. *
  67. * @dataProvider uuidTableProvider
  68. * @return void
  69. */
  70. public function testSaveNew($tableName)
  71. {
  72. $entity = new Entity([
  73. 'name' => 'shiny new',
  74. 'published' => true,
  75. ]);
  76. $table = $this->getTableLocator()->get($tableName);
  77. $this->assertSame($entity, $table->save($entity));
  78. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  79. $row = $table->find('all')->where(['id' => $entity->id])->first();
  80. $row->id = strtolower($row->id);
  81. $this->assertEquals($entity->toArray(), $row->toArray());
  82. }
  83. /**
  84. * Test saving new records allows manual uuids
  85. *
  86. * @dataProvider uuidTableProvider
  87. * @return void
  88. */
  89. public function testSaveNewSpecificId($tableName)
  90. {
  91. $id = Text::uuid();
  92. $entity = new Entity([
  93. 'id' => $id,
  94. 'name' => 'shiny and new',
  95. 'published' => true,
  96. ]);
  97. $table = $this->getTableLocator()->get($tableName);
  98. $this->assertSame($entity, $table->save($entity));
  99. $this->assertSame($id, $entity->id);
  100. $row = $table->find('all')->where(['id' => $id])->first();
  101. $this->assertNotEmpty($row);
  102. $this->assertSame($id, strtolower($row->id));
  103. $this->assertSame($entity->name, $row->name);
  104. }
  105. /**
  106. * Test saving existing records works
  107. *
  108. * @dataProvider uuidTableProvider
  109. * @return void
  110. */
  111. public function testSaveUpdate($tableName)
  112. {
  113. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  114. $entity = new Entity([
  115. 'id' => $id,
  116. 'name' => 'shiny update',
  117. 'published' => true,
  118. ]);
  119. $table = $this->getTableLocator()->get($tableName);
  120. $this->assertSame($entity, $table->save($entity));
  121. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  122. $row = $table->find('all')->where(['id' => $entity->id])->first();
  123. $row->id = strtolower($row->id);
  124. $this->assertEquals($entity->toArray(), $row->toArray());
  125. }
  126. /**
  127. * Test delete with string pk.
  128. *
  129. * @dataProvider uuidTableProvider
  130. * @return void
  131. */
  132. public function testGetById($tableName)
  133. {
  134. $table = $this->getTableLocator()->get($tableName);
  135. $entity = $table->find('all')->firstOrFail();
  136. $result = $table->get($entity->id);
  137. $this->assertSame($result->id, $entity->id);
  138. }
  139. /**
  140. * Test delete with string pk.
  141. *
  142. * @dataProvider uuidTableProvider
  143. * @return void
  144. */
  145. public function testDelete($tableName)
  146. {
  147. $table = $this->getTableLocator()->get($tableName);
  148. $entity = $table->find('all')->firstOrFail();
  149. $this->assertTrue($table->delete($entity));
  150. $query = $table->find('all')->where(['id' => $entity->id]);
  151. $this->assertEmpty($query->first(), 'No row left');
  152. }
  153. /**
  154. * Tests that sql server does not error when an empty uuid is bound
  155. *
  156. * @dataProvider uuidTableProvider
  157. * @return void
  158. */
  159. public function testEmptyUuid($tableName)
  160. {
  161. $id = '';
  162. $table = $this->getTableLocator()->get($tableName);
  163. $entity = $table->find('all')
  164. ->where(['id' => $id])
  165. ->first();
  166. $this->assertNull($entity);
  167. }
  168. }