TableUuidTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. * Provider for testing that string and binary uuids work the same
  47. *
  48. * @return array
  49. */
  50. public function uuidTableProvider()
  51. {
  52. return [['uuiditems'], ['binary_uuiditems']];
  53. }
  54. /**
  55. * Test saving new records sets uuids
  56. *
  57. * @dataProvider uuidTableProvider
  58. * @return void
  59. */
  60. public function testSaveNew($tableName)
  61. {
  62. $entity = new Entity([
  63. 'name' => 'shiny new',
  64. 'published' => true,
  65. ]);
  66. $table = $this->getTableLocator()->get($tableName);
  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. * @dataProvider uuidTableProvider
  77. * @return void
  78. */
  79. public function testSaveNewSpecificId($tableName)
  80. {
  81. $id = Text::uuid();
  82. $entity = new Entity([
  83. 'id' => $id,
  84. 'name' => 'shiny and new',
  85. 'published' => true,
  86. ]);
  87. $table = $this->getTableLocator()->get($tableName);
  88. $this->assertSame($entity, $table->save($entity));
  89. $this->assertSame($id, $entity->id);
  90. $row = $table->find('all')->where(['id' => $id])->first();
  91. $this->assertNotEmpty($row);
  92. $this->assertSame($id, strtolower($row->id));
  93. $this->assertSame($entity->name, $row->name);
  94. }
  95. /**
  96. * Test saving existing records works
  97. *
  98. * @dataProvider uuidTableProvider
  99. * @return void
  100. */
  101. public function testSaveUpdate($tableName)
  102. {
  103. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  104. $entity = new Entity([
  105. 'id' => $id,
  106. 'name' => 'shiny update',
  107. 'published' => true,
  108. ]);
  109. $table = $this->getTableLocator()->get($tableName);
  110. $this->assertSame($entity, $table->save($entity));
  111. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  112. $row = $table->find('all')->where(['id' => $entity->id])->first();
  113. $row->id = strtolower($row->id);
  114. $this->assertEquals($entity->toArray(), $row->toArray());
  115. }
  116. /**
  117. * Test delete with string pk.
  118. *
  119. * @dataProvider uuidTableProvider
  120. * @return void
  121. */
  122. public function testGetById($tableName)
  123. {
  124. $table = $this->getTableLocator()->get($tableName);
  125. $entity = $table->find('all')->firstOrFail();
  126. $result = $table->get($entity->id);
  127. $this->assertSame($result->id, $entity->id);
  128. }
  129. /**
  130. * Test delete with string pk.
  131. *
  132. * @dataProvider uuidTableProvider
  133. * @return void
  134. */
  135. public function testDelete($tableName)
  136. {
  137. $table = $this->getTableLocator()->get($tableName);
  138. $entity = $table->find('all')->firstOrFail();
  139. $this->assertTrue($table->delete($entity));
  140. $query = $table->find('all')->where(['id' => $entity->id]);
  141. $this->assertEmpty($query->first(), 'No row left');
  142. }
  143. /**
  144. * Tests that sql server does not error when an empty uuid is bound
  145. *
  146. * @dataProvider uuidTableProvider
  147. * @return void
  148. */
  149. public function testEmptyUuid($tableName)
  150. {
  151. $id = '';
  152. $table = $this->getTableLocator()->get($tableName);
  153. $entity = $table->find('all')
  154. ->where(['id' => $id])
  155. ->first();
  156. $this->assertNull($entity);
  157. }
  158. }