TableUuidTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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\Datasource\ConnectionManager;
  18. use Cake\ORM\Entity;
  19. use Cake\ORM\TableRegistry;
  20. use Cake\TestSuite\TestCase;
  21. use Cake\Utility\Text;
  22. /**
  23. * Integration tests for Table class with uuid primary keys.
  24. *
  25. */
  26. class TableUuidTest extends TestCase
  27. {
  28. /**
  29. * Fixtures
  30. *
  31. * @var array
  32. */
  33. public $fixtures = [
  34. 'core.uuiditems', 'core.uuidportfolios'
  35. ];
  36. /**
  37. * setup
  38. *
  39. * @return void
  40. */
  41. public function setUp()
  42. {
  43. parent::setUp();
  44. $this->connection = ConnectionManager::get('test');
  45. Configure::write('App.namespace', 'TestApp');
  46. }
  47. /**
  48. * teardown
  49. *
  50. * @return void
  51. */
  52. public function tearDown()
  53. {
  54. parent::tearDown();
  55. TableRegistry::clear();
  56. }
  57. /**
  58. * Test saving new records sets uuids
  59. *
  60. * @return void
  61. */
  62. public function testSaveNew()
  63. {
  64. $entity = new Entity([
  65. 'name' => 'shiny new',
  66. 'published' => true,
  67. ]);
  68. $table = TableRegistry::get('uuiditems');
  69. $this->assertSame($entity, $table->save($entity));
  70. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  71. $row = $table->find('all')->where(['id' => $entity->id])->first();
  72. $row->id = strtolower($row->id);
  73. $this->assertEquals($entity->toArray(), $row->toArray());
  74. }
  75. /**
  76. * Test saving new records allows manual uuids
  77. *
  78. * @return void
  79. */
  80. public function testSaveNewSpecificId()
  81. {
  82. $id = Text::uuid();
  83. $entity = new Entity([
  84. 'id' => $id,
  85. 'name' => 'shiny and new',
  86. 'published' => true,
  87. ]);
  88. $table = TableRegistry::get('uuiditems');
  89. $this->assertSame($entity, $table->save($entity));
  90. $this->assertSame($id, $entity->id);
  91. $row = $table->find('all')->where(['id' => $id])->first();
  92. $this->assertNotEmpty($row);
  93. $this->assertSame($id, strtolower($row->id));
  94. $this->assertSame($entity->name, $row->name);
  95. }
  96. /**
  97. * Test saving existing records works
  98. *
  99. * @return void
  100. */
  101. public function testSaveUpdate()
  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 = TableRegistry::get('uuiditems');
  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. * @return void
  120. */
  121. public function testDelete()
  122. {
  123. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  124. $table = TableRegistry::get('uuiditems');
  125. $entity = $table->find('all')->where(['id' => $id])->first();
  126. $this->assertTrue($table->delete($entity));
  127. $query = $table->find('all')->where(['id' => $id]);
  128. $this->assertCount(0, $query->execute(), 'No rows left');
  129. }
  130. /**
  131. * Tests that sql server does not error when an empty uuid is bound
  132. *
  133. * @return void
  134. */
  135. public function testEmptyUuid()
  136. {
  137. $id = '';
  138. $table = TableRegistry::get('uuiditems');
  139. $entity = $table->find('all')
  140. ->where(['id' => $id])
  141. ->first();
  142. $this->assertNull($entity);
  143. }
  144. }