Skip to content

Commit

Permalink
Add integration test for binary uuid.
Browse files Browse the repository at this point in the history
Extend the existing uuid table test to do the same tests for binary
uuids.
  • Loading branch information
markstory committed Oct 15, 2017
1 parent 3324bcb commit 4db1edd
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 13 deletions.
4 changes: 2 additions & 2 deletions src/Database/Type/BinaryUuidType.php
Expand Up @@ -73,11 +73,11 @@ public function toDatabase($value, Driver $driver)
/**
* 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();
}

/**
Expand Down
47 changes: 47 additions & 0 deletions 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'],
];
}
38 changes: 27 additions & 11 deletions tests/TestCase/ORM/TableUuidTest.php
Expand Up @@ -32,7 +32,8 @@ class TableUuidTest extends TestCase
* @var array
*/
public $fixtures = [
'core.uuiditems', 'core.uuidportfolios'
'core.binary_uuiditems',
'core.uuiditems',
];

/**
Expand All @@ -58,18 +59,29 @@ public function tearDown()
TableRegistry::clear();
}

/**
* 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');

Expand All @@ -81,17 +93,18 @@ public function testSaveNew()
/**
* Test saving new records allows manual uuids
*
* @dataProvider uuidTableProvider
* @return void
*/
public function testSaveNewSpecificId()
public function testSaveNewSpecificId($tableName)
{
$id = Text::uuid();
$entity = new Entity([
'id' => $id,
'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);

Expand All @@ -104,9 +117,10 @@ public function testSaveNewSpecificId()
/**
* 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([
Expand All @@ -115,7 +129,7 @@ public function testSaveUpdate()
'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');

Expand All @@ -127,12 +141,13 @@ public function testSaveUpdate()
/**
* 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));
Expand All @@ -143,12 +158,13 @@ public function testDelete()
/**
* 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();
Expand Down

0 comments on commit 4db1edd

Please sign in to comment.