TableUuidTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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\Table;
  20. use Cake\ORM\TableRegistry;
  21. use Cake\TestSuite\TestCase;
  22. use Cake\Utility\String;
  23. /**
  24. * Integration tests for Table class with uuid primary keys.
  25. *
  26. */
  27. class TableUuidTest extends TestCase
  28. {
  29. /**
  30. * Fixtures
  31. *
  32. * @var array
  33. */
  34. public $fixtures = [
  35. 'core.uuiditems', 'core.uuidportfolios'
  36. ];
  37. /**
  38. * setup
  39. *
  40. * @return void
  41. */
  42. public function setUp()
  43. {
  44. parent::setUp();
  45. $this->connection = ConnectionManager::get('test');
  46. Configure::write('App.namespace', 'TestApp');
  47. }
  48. /**
  49. * teardown
  50. *
  51. * @return void
  52. */
  53. public function tearDown()
  54. {
  55. parent::tearDown();
  56. TableRegistry::clear();
  57. }
  58. /**
  59. * Test saving new records sets uuids
  60. *
  61. * @return void
  62. */
  63. public function testSaveNew()
  64. {
  65. $entity = new \Cake\ORM\Entity([
  66. 'name' => 'shiny new',
  67. 'published' => true,
  68. ]);
  69. $table = TableRegistry::get('uuiditems');
  70. $this->assertSame($entity, $table->save($entity));
  71. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  72. $row = $table->find('all')->where(['id' => $entity->id])->first();
  73. $row->id = strtolower($row->id);
  74. $this->assertEquals($entity->toArray(), $row->toArray());
  75. }
  76. /**
  77. * Test saving existing records works
  78. *
  79. * @return void
  80. */
  81. public function testSaveUpdate()
  82. {
  83. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  84. $entity = new \Cake\ORM\Entity([
  85. 'id' => $id,
  86. 'name' => 'shiny update',
  87. 'published' => true,
  88. ]);
  89. $table = TableRegistry::get('uuiditems');
  90. $this->assertSame($entity, $table->save($entity));
  91. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  92. $row = $table->find('all')->where(['id' => $entity->id])->first();
  93. $row->id = strtolower($row->id);
  94. $this->assertEquals($entity->toArray(), $row->toArray());
  95. }
  96. /**
  97. * Test delete with string pk.
  98. *
  99. * @return void
  100. */
  101. public function testDelete()
  102. {
  103. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  104. $table = TableRegistry::get('uuiditems');
  105. $entity = $table->find('all')->where(['id' => $id])->first();
  106. $this->assertTrue($table->delete($entity));
  107. $query = $table->find('all')->where(['id' => $id]);
  108. $this->assertCount(0, $query->execute(), 'No rows left');
  109. }
  110. /**
  111. * Tests that sql server does not error when an empty uuid is bound
  112. *
  113. * @return void
  114. */
  115. public function testEmptyUuid()
  116. {
  117. $this->skipIf(
  118. !$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
  119. 'Empty UUIDs only affect SQLServer uniqueidentifier field types'
  120. );
  121. $id = '';
  122. $table = TableRegistry::get('uuiditems');
  123. $entity = $table->find('all')
  124. ->where(['id' => $id])
  125. ->first();
  126. $this->assertNull($entity);
  127. }
  128. }