TableUuidTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. class TableUuidTest extends TestCase
  26. {
  27. /**
  28. * Fixtures
  29. *
  30. * @var array
  31. */
  32. public $fixtures = [
  33. 'core.uuiditems', 'core.uuidportfolios'
  34. ];
  35. /**
  36. * setup
  37. *
  38. * @return void
  39. */
  40. public function setUp()
  41. {
  42. parent::setUp();
  43. $this->connection = ConnectionManager::get('test');
  44. Configure::write('App.namespace', 'TestApp');
  45. }
  46. /**
  47. * teardown
  48. *
  49. * @return void
  50. */
  51. public function tearDown()
  52. {
  53. parent::tearDown();
  54. TableRegistry::clear();
  55. }
  56. /**
  57. * Test saving new records sets uuids
  58. *
  59. * @return void
  60. */
  61. public function testSaveNew()
  62. {
  63. $entity = new Entity([
  64. 'name' => 'shiny new',
  65. 'published' => true,
  66. ]);
  67. $table = TableRegistry::get('uuiditems');
  68. $this->assertSame($entity, $table->save($entity));
  69. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  70. $row = $table->find('all')->where(['id' => $entity->id])->first();
  71. $row->id = strtolower($row->id);
  72. $this->assertEquals($entity->toArray(), $row->toArray());
  73. }
  74. /**
  75. * Test saving new records allows manual uuids
  76. *
  77. * @return void
  78. */
  79. public function testSaveNewSpecificId()
  80. {
  81. $id = Text::uuid();
  82. $entity = new Entity([
  83. 'id' => $id,
  84. 'name' => 'shiny and new',
  85. 'published' => true,
  86. ]);
  87. $table = TableRegistry::get('uuiditems');
  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. * @return void
  99. */
  100. public function testSaveUpdate()
  101. {
  102. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  103. $entity = new Entity([
  104. 'id' => $id,
  105. 'name' => 'shiny update',
  106. 'published' => true,
  107. ]);
  108. $table = TableRegistry::get('uuiditems');
  109. $this->assertSame($entity, $table->save($entity));
  110. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  111. $row = $table->find('all')->where(['id' => $entity->id])->first();
  112. $row->id = strtolower($row->id);
  113. $this->assertEquals($entity->toArray(), $row->toArray());
  114. }
  115. /**
  116. * Test delete with string pk.
  117. *
  118. * @return void
  119. */
  120. public function testDelete()
  121. {
  122. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  123. $table = TableRegistry::get('uuiditems');
  124. $entity = $table->find('all')->where(['id' => $id])->first();
  125. $this->assertTrue($table->delete($entity));
  126. $query = $table->find('all')->where(['id' => $id]);
  127. $this->assertCount(0, $query->execute(), 'No rows left');
  128. }
  129. /**
  130. * Tests that sql server does not error when an empty uuid is bound
  131. *
  132. * @return void
  133. */
  134. public function testEmptyUuid()
  135. {
  136. $id = '';
  137. $table = TableRegistry::get('uuiditems');
  138. $entity = $table->find('all')
  139. ->where(['id' => $id])
  140. ->first();
  141. $this->assertNull($entity);
  142. }
  143. }