TableUuidTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (http://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. (http://cakefoundation.org)
  11. * @link http://cakephp.org CakePHP(tm) Project
  12. * @since 3.0.0
  13. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\ORM;
  16. use Cake\Core\Configure;
  17. use Cake\Database\Expression\QueryExpression;
  18. use Cake\Datasource\ConnectionManager;
  19. use Cake\ORM\Entity;
  20. use Cake\ORM\Table;
  21. use Cake\ORM\TableRegistry;
  22. use Cake\TestSuite\TestCase;
  23. use Cake\Utility\Text;
  24. /**
  25. * Integration tests for Table class with uuid primary keys.
  26. *
  27. */
  28. class TableUuidTest extends TestCase
  29. {
  30. /**
  31. * Fixtures
  32. *
  33. * @var array
  34. */
  35. public $fixtures = [
  36. 'core.uuiditems', 'core.uuidportfolios'
  37. ];
  38. /**
  39. * setup
  40. *
  41. * @return void
  42. */
  43. public function setUp()
  44. {
  45. parent::setUp();
  46. $this->connection = ConnectionManager::get('test');
  47. Configure::write('App.namespace', 'TestApp');
  48. }
  49. /**
  50. * teardown
  51. *
  52. * @return void
  53. */
  54. public function tearDown()
  55. {
  56. parent::tearDown();
  57. TableRegistry::clear();
  58. }
  59. /**
  60. * Test saving new records sets uuids
  61. *
  62. * @return void
  63. */
  64. public function testSaveNew()
  65. {
  66. $entity = new Entity([
  67. 'name' => 'shiny new',
  68. 'published' => true,
  69. ]);
  70. $table = TableRegistry::get('uuiditems');
  71. $this->assertSame($entity, $table->save($entity));
  72. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  73. $row = $table->find('all')->where(['id' => $entity->id])->first();
  74. $row->id = strtolower($row->id);
  75. $this->assertEquals($entity->toArray(), $row->toArray());
  76. }
  77. /**
  78. * Test saving new records allows manual uuids
  79. *
  80. * @return void
  81. */
  82. public function testSaveNewSpecificId()
  83. {
  84. $id = Text::uuid();
  85. $entity = new Entity([
  86. 'id' => $id,
  87. 'name' => 'shiny and new',
  88. 'published' => true,
  89. ]);
  90. $table = TableRegistry::get('uuiditems');
  91. $this->assertSame($entity, $table->save($entity));
  92. $this->assertSame($id, $entity->id);
  93. $row = $table->find('all')->where(['id' => $id])->first();
  94. $this->assertNotEmpty($row);
  95. $this->assertSame($id, strtolower($row->id));
  96. $this->assertSame($entity->name, $row->name);
  97. }
  98. /**
  99. * Test saving existing records works
  100. *
  101. * @return void
  102. */
  103. public function testSaveUpdate()
  104. {
  105. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  106. $entity = new Entity([
  107. 'id' => $id,
  108. 'name' => 'shiny update',
  109. 'published' => true,
  110. ]);
  111. $table = TableRegistry::get('uuiditems');
  112. $this->assertSame($entity, $table->save($entity));
  113. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  114. $row = $table->find('all')->where(['id' => $entity->id])->first();
  115. $row->id = strtolower($row->id);
  116. $this->assertEquals($entity->toArray(), $row->toArray());
  117. }
  118. /**
  119. * Test delete with string pk.
  120. *
  121. * @return void
  122. */
  123. public function testDelete()
  124. {
  125. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  126. $table = TableRegistry::get('uuiditems');
  127. $entity = $table->find('all')->where(['id' => $id])->first();
  128. $this->assertTrue($table->delete($entity));
  129. $query = $table->find('all')->where(['id' => $id]);
  130. $this->assertCount(0, $query->execute(), 'No rows left');
  131. }
  132. /**
  133. * Tests that sql server does not error when an empty uuid is bound
  134. *
  135. * @return void
  136. */
  137. public function testEmptyUuid()
  138. {
  139. $id = '';
  140. $table = TableRegistry::get('uuiditems');
  141. $entity = $table->find('all')
  142. ->where(['id' => $id])
  143. ->first();
  144. $this->assertNull($entity);
  145. }
  146. }