TableUuidTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. $row = $table->find('all')->where(['id' => $id])->first();
  93. $this->assertNotEmpty($row);
  94. $this->assertSame($id, strtolower($row->id));
  95. $this->assertSame($entity->name, $row->name);
  96. }
  97. /**
  98. * Test saving existing records works
  99. *
  100. * @return void
  101. */
  102. public function testSaveUpdate()
  103. {
  104. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  105. $entity = new Entity([
  106. 'id' => $id,
  107. 'name' => 'shiny update',
  108. 'published' => true,
  109. ]);
  110. $table = TableRegistry::get('uuiditems');
  111. $this->assertSame($entity, $table->save($entity));
  112. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  113. $row = $table->find('all')->where(['id' => $entity->id])->first();
  114. $row->id = strtolower($row->id);
  115. $this->assertEquals($entity->toArray(), $row->toArray());
  116. }
  117. /**
  118. * Test delete with string pk.
  119. *
  120. * @return void
  121. */
  122. public function testDelete()
  123. {
  124. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  125. $table = TableRegistry::get('uuiditems');
  126. $entity = $table->find('all')->where(['id' => $id])->first();
  127. $this->assertTrue($table->delete($entity));
  128. $query = $table->find('all')->where(['id' => $id]);
  129. $this->assertCount(0, $query->execute(), 'No rows left');
  130. }
  131. /**
  132. * Tests that sql server does not error when an empty uuid is bound
  133. *
  134. * @return void
  135. */
  136. public function testEmptyUuid()
  137. {
  138. $id = '';
  139. $table = TableRegistry::get('uuiditems');
  140. $entity = $table->find('all')
  141. ->where(['id' => $id])
  142. ->first();
  143. $this->assertNull($entity);
  144. }
  145. }