TableUuidTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 3.0.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\ORM;
  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<string>
  29. */
  30. protected array $fixtures = [
  31. 'core.BinaryUuidItems',
  32. 'core.UuidItems',
  33. ];
  34. /**
  35. * setup
  36. */
  37. public function setUp(): void
  38. {
  39. parent::setUp();
  40. static::setAppNamespace();
  41. }
  42. /**
  43. * Provider for testing that string and binary uuids work the same
  44. *
  45. * @return array
  46. */
  47. public function uuidTableProvider(): array
  48. {
  49. return [['uuid_items'], ['binary_uuid_items']];
  50. }
  51. /**
  52. * Test saving new records sets uuids
  53. *
  54. * @dataProvider uuidTableProvider
  55. */
  56. public function testSaveNew(string $tableName): void
  57. {
  58. $entity = new Entity([
  59. 'name' => 'shiny new',
  60. 'published' => true,
  61. ]);
  62. $table = $this->getTableLocator()->get($tableName);
  63. $this->assertSame($entity, $table->save($entity));
  64. $this->assertMatchesRegularExpression('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  65. $row = $table->find('all')->where(['id' => $entity->id])->first();
  66. $row->id = strtolower($row->id);
  67. $this->assertEquals($entity->toArray(), $row->toArray());
  68. }
  69. /**
  70. * Test saving new records allows manual uuids
  71. *
  72. * @dataProvider uuidTableProvider
  73. */
  74. public function testSaveNewSpecificId(string $tableName): void
  75. {
  76. $id = Text::uuid();
  77. $entity = new Entity([
  78. 'id' => $id,
  79. 'name' => 'shiny and new',
  80. 'published' => true,
  81. ]);
  82. $table = $this->getTableLocator()->get($tableName);
  83. $this->assertSame($entity, $table->save($entity));
  84. $this->assertSame($id, $entity->id);
  85. $row = $table->find('all')->where(['id' => $id])->first();
  86. $this->assertNotEmpty($row);
  87. $this->assertSame($id, strtolower($row->id));
  88. $this->assertSame($entity->name, $row->name);
  89. }
  90. /**
  91. * Test saving existing records works
  92. *
  93. * @dataProvider uuidTableProvider
  94. */
  95. public function testSaveUpdate(string $tableName): void
  96. {
  97. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  98. $entity = new Entity([
  99. 'id' => $id,
  100. 'name' => 'shiny update',
  101. 'published' => true,
  102. ]);
  103. $table = $this->getTableLocator()->get($tableName);
  104. $this->assertSame($entity, $table->save($entity));
  105. $this->assertSame($id, $entity->id, 'Should be 36 characters');
  106. $row = $table->find('all')->where(['id' => $entity->id])->first();
  107. $row->id = strtolower($row->id);
  108. $this->assertEquals($entity->toArray(), $row->toArray());
  109. }
  110. /**
  111. * Test delete with string pk.
  112. *
  113. * @dataProvider uuidTableProvider
  114. */
  115. public function testGetById(string $tableName): void
  116. {
  117. $table = $this->getTableLocator()->get($tableName);
  118. $entity = $table->find('all')->firstOrFail();
  119. $result = $table->get($entity->id);
  120. $this->assertSame($result->id, $entity->id);
  121. }
  122. /**
  123. * Test delete with string pk.
  124. *
  125. * @dataProvider uuidTableProvider
  126. */
  127. public function testDelete(string $tableName): void
  128. {
  129. $table = $this->getTableLocator()->get($tableName);
  130. $entity = $table->find('all')->firstOrFail();
  131. $this->assertTrue($table->delete($entity));
  132. $query = $table->find('all')->where(['id' => $entity->id]);
  133. $this->assertEmpty($query->first(), 'No row left');
  134. }
  135. /**
  136. * Tests that sql server does not error when an empty uuid is bound
  137. *
  138. * @dataProvider uuidTableProvider
  139. */
  140. public function testEmptyUuid(string $tableName): void
  141. {
  142. $id = '';
  143. $table = $this->getTableLocator()->get($tableName);
  144. $entity = $table->find('all')
  145. ->where(['id' => $id])
  146. ->first();
  147. $this->assertNull($entity);
  148. }
  149. }