TableUuidTest.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. * 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. 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. parent::tearDown();
  53. TableRegistry::clear();
  54. }
  55. /**
  56. * Test saving new records sets uuids
  57. *
  58. * @return void
  59. */
  60. public function testSaveNew() {
  61. $entity = new \Cake\ORM\Entity([
  62. 'name' => 'shiny new',
  63. 'published' => true,
  64. ]);
  65. $table = TableRegistry::get('uuiditems');
  66. $this->assertSame($entity, $table->save($entity));
  67. $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
  68. $row = $table->find('all')->where(['id' => $entity->id])->first();
  69. $row->id = strtolower($row->id);
  70. $this->assertEquals($entity->toArray(), $row->toArray());
  71. }
  72. /**
  73. * Test saving existing records works
  74. *
  75. * @return void
  76. */
  77. public function testSaveUpdate() {
  78. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  79. $entity = new \Cake\ORM\Entity([
  80. 'id' => $id,
  81. 'name' => 'shiny update',
  82. 'published' => true,
  83. ]);
  84. $table = TableRegistry::get('uuiditems');
  85. $this->assertSame($entity, $table->save($entity));
  86. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  87. $row = $table->find('all')->where(['id' => $entity->id])->first();
  88. $row->id = strtolower($row->id);
  89. $this->assertEquals($entity->toArray(), $row->toArray());
  90. }
  91. /**
  92. * Test delete with string pk.
  93. *
  94. * @return void
  95. */
  96. public function testDelete() {
  97. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  98. $table = TableRegistry::get('uuiditems');
  99. $entity = $table->find('all')->where(['id' => $id])->first();
  100. $this->assertTrue($table->delete($entity));
  101. $query = $table->find('all')->where(['id' => $id]);
  102. $this->assertCount(0, $query->execute(), 'No rows left');
  103. }
  104. /**
  105. * Tests that sql server does not error when an empty uuid is bound
  106. *
  107. * @return void
  108. */
  109. public function testEmptyUuid() {
  110. $this->skipIf(
  111. !$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
  112. 'Empty UUIDs only affect SQLServer uniqueidentifier field types'
  113. );
  114. $id = '';
  115. $table = TableRegistry::get('uuiditems');
  116. $entity = $table->find('all')
  117. ->where(['id' => $id])
  118. ->first();
  119. $this->assertNull($entity);
  120. }
  121. }