Browse Source

Add integration test for binary uuid.

Extend the existing uuid table test to do the same tests for binary
uuids.
Mark Story 8 years ago
parent
commit
4db1edd813

+ 2 - 2
src/Database/Type/BinaryUuidType.php

@@ -73,11 +73,11 @@ class BinaryUuidType extends Type implements TypeInterface
     /**
      * Generate a new binary UUID
      *
-     * @return mixed A new primary key value.
+     * @return string A new primary key value.
      */
     public function newId()
     {
-        return $this->convertStringToBinaryUuid(Text::uuid());
+        return Text::uuid();
     }
 
     /**

+ 47 - 0
tests/Fixture/BinaryUuiditemsFixture.php

@@ -0,0 +1,47 @@
+<?php
+/**
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ *
+ * Licensed under The MIT License
+ * For full copyright and license information, please see the LICENSE.txt
+ * Redistributions of files must retain the above copyright notice
+ *
+ * @copyright     Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
+ * @link          https://cakephp.org CakePHP(tm) Project
+ * @since         1.2.0
+ * @license       https://opensource.org/licenses/mit-license.php MIT License
+ */
+namespace Cake\Test\Fixture;
+
+use Cake\TestSuite\Fixture\TestFixture;
+
+/**
+ * BinaryUuiditemsFixture
+ */
+class BinaryUuiditemsFixture extends TestFixture
+{
+
+    /**
+     * fields property
+     *
+     * @var array
+     */
+    public $fields = [
+        'id' => ['type' => 'binaryuuid'],
+        'name' => ['type' => 'string', 'null' => false],
+        'published' => ['type' => 'boolean', 'null' => false],
+        '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
+    ];
+
+    /**
+     * records property
+     *
+     * @var array
+     */
+    public $records = [
+        ['id' => '481fc6d0-b920-43e0-a40d-6d1740cf8569', 'published' => true, 'name' => 'Item 1'],
+        ['id' => '48298a29-81c0-4c26-a7fb-413140cf8569', 'published' => false, 'name' => 'Item 2'],
+        ['id' => '482b7756-8da0-419a-b21f-27da40cf8569', 'published' => true, 'name' => 'Item 3'],
+    ];
+}

+ 27 - 11
tests/TestCase/ORM/TableUuidTest.php

@@ -32,7 +32,8 @@ class TableUuidTest extends TestCase
      * @var array
      */
     public $fixtures = [
-        'core.uuiditems', 'core.uuidportfolios'
+        'core.binary_uuiditems',
+        'core.uuiditems',
     ];
 
     /**
@@ -59,17 +60,28 @@ class TableUuidTest extends TestCase
     }
 
     /**
+     * Provider for testing that string and binary uuids work the same
+     *
+     * @return array
+     */
+    public function uuidTableProvider()
+    {
+        return [['uuiditems'], ['binary_uuiditems']];
+    }
+
+    /**
      * Test saving new records sets uuids
      *
+     * @dataProvider uuidTableProvider
      * @return void
      */
-    public function testSaveNew()
+    public function testSaveNew($tableName)
     {
         $entity = new Entity([
             'name' => 'shiny new',
             'published' => true,
         ]);
-        $table = TableRegistry::get('uuiditems');
+        $table = TableRegistry::get($tableName);
         $this->assertSame($entity, $table->save($entity));
         $this->assertRegExp('/^[a-f0-9-]{36}$/', $entity->id, 'Should be 36 characters');
 
@@ -81,9 +93,10 @@ class TableUuidTest extends TestCase
     /**
      * Test saving new records allows manual uuids
      *
+     * @dataProvider uuidTableProvider
      * @return void
      */
-    public function testSaveNewSpecificId()
+    public function testSaveNewSpecificId($tableName)
     {
         $id = Text::uuid();
         $entity = new Entity([
@@ -91,7 +104,7 @@ class TableUuidTest extends TestCase
             'name' => 'shiny and new',
             'published' => true,
         ]);
-        $table = TableRegistry::get('uuiditems');
+        $table = TableRegistry::get($tableName);
         $this->assertSame($entity, $table->save($entity));
         $this->assertSame($id, $entity->id);
 
@@ -104,9 +117,10 @@ class TableUuidTest extends TestCase
     /**
      * Test saving existing records works
      *
+     * @dataProvider uuidTableProvider
      * @return void
      */
-    public function testSaveUpdate()
+    public function testSaveUpdate($tableName)
     {
         $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
         $entity = new Entity([
@@ -115,7 +129,7 @@ class TableUuidTest extends TestCase
             'published' => true,
         ]);
 
-        $table = TableRegistry::get('uuiditems');
+        $table = TableRegistry::get($tableName);
         $this->assertSame($entity, $table->save($entity));
         $this->assertEquals($id, $entity->id, 'Should be 36 characters');
 
@@ -127,12 +141,13 @@ class TableUuidTest extends TestCase
     /**
      * Test delete with string pk.
      *
+     * @dataProvider uuidTableProvider
      * @return void
      */
-    public function testDelete()
+    public function testDelete($tableName)
     {
         $id = '481fc6d0-b920-43e0-a40d-6d1740cf8569';
-        $table = TableRegistry::get('uuiditems');
+        $table = TableRegistry::get($tableName);
         $entity = $table->find('all')->where(['id' => $id])->first();
 
         $this->assertTrue($table->delete($entity));
@@ -143,12 +158,13 @@ class TableUuidTest extends TestCase
     /**
      * Tests that sql server does not error when an empty uuid is bound
      *
+     * @dataProvider uuidTableProvider
      * @return void
      */
-    public function testEmptyUuid()
+    public function testEmptyUuid($tableName)
     {
         $id = '';
-        $table = TableRegistry::get('uuiditems');
+        $table = TableRegistry::get($tableName);
         $entity = $table->find('all')
             ->where(['id' => $id])
             ->first();