TableUuidTest.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. /**
  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 \Cake\ORM\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 existing records works
  77. *
  78. * @return void
  79. */
  80. public function testSaveUpdate()
  81. {
  82. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  83. $entity = new \Cake\ORM\Entity([
  84. 'id' => $id,
  85. 'name' => 'shiny update',
  86. 'published' => true,
  87. ]);
  88. $table = TableRegistry::get('uuiditems');
  89. $this->assertSame($entity, $table->save($entity));
  90. $this->assertEquals($id, $entity->id, 'Should be 36 characters');
  91. $row = $table->find('all')->where(['id' => $entity->id])->first();
  92. $row->id = strtolower($row->id);
  93. $this->assertEquals($entity->toArray(), $row->toArray());
  94. }
  95. /**
  96. * Test delete with string pk.
  97. *
  98. * @return void
  99. */
  100. public function testDelete()
  101. {
  102. $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
  103. $table = TableRegistry::get('uuiditems');
  104. $entity = $table->find('all')->where(['id' => $id])->first();
  105. $this->assertTrue($table->delete($entity));
  106. $query = $table->find('all')->where(['id' => $id]);
  107. $this->assertCount(0, $query->execute(), 'No rows left');
  108. }
  109. /**
  110. * Tests that sql server does not error when an empty uuid is bound
  111. *
  112. * @return void
  113. */
  114. public function testEmptyUuid()
  115. {
  116. $this->skipIf(
  117. !$this->connection->driver() instanceof \Cake\Database\Driver\Sqlserver,
  118. 'Empty UUIDs only affect SQLServer uniqueidentifier field types'
  119. );
  120. $id = '';
  121. $table = TableRegistry::get('uuiditems');
  122. $entity = $table->find('all')
  123. ->where(['id' => $id])
  124. ->first();
  125. $this->assertNull($entity);
  126. }
  127. }