Skip to content

Commit

Permalink
Merge pull request #447 from dmromanov/feature/validator-non-negative
Browse files Browse the repository at this point in the history
Validation for unsigned integer columns
  • Loading branch information
markstory authored Aug 21, 2018
2 parents 3c7c701 + 84aff09 commit 7356656
Show file tree
Hide file tree
Showing 14 changed files with 649 additions and 6 deletions.
17 changes: 16 additions & 1 deletion src/Shell/Task/ModelTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Cake\ORM\Table;
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Validation\Validation;

/**
* Task class for generating model files.
Expand Down Expand Up @@ -707,11 +708,25 @@ public function fieldValidation($schema, $fieldName, array $metaData, $primaryKe
} elseif ($metaData['type'] === 'uuid') {
$rules['uuid'] = [];
} elseif ($metaData['type'] === 'integer') {
$rules['integer'] = [];
if ($metaData['unsigned']) {
$rules['nonNegativeInteger'] = [];
} else {
$rules['integer'] = [];
}
} elseif ($metaData['type'] === 'float') {
$rules['numeric'] = [];
if ($metaData['unsigned']) {
$rules['greaterThanOrEqual'] = [
0
];
}
} elseif ($metaData['type'] === 'decimal') {
$rules['decimal'] = [];
if ($metaData['unsigned']) {
$rules['greaterThanOrEqual'] = [
0
];
}
} elseif ($metaData['type'] === 'boolean') {
$rules['boolean'] = [];
} elseif ($metaData['type'] === 'date') {
Expand Down
2 changes: 2 additions & 0 deletions tests/Fixture/BakeArticlesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ class BakeArticlesFixture extends TestFixture
'bake_user_id' => ['type' => 'integer', 'null' => false],
'title' => ['type' => 'string', 'length' => 50, 'null' => false],
'body' => 'text',
'rating' => ['type' => 'float', 'unsigned' => true, 'default' => 0.0, 'null' => false],
'score' => ['type' => 'decimal', 'unsigned' => true, 'default' => 0.0, 'null' => false],
'published' => ['type' => 'boolean', 'length' => 1, 'default' => false],
'created' => 'datetime',
'updated' => 'datetime',
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixture/CategoryThreadsFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class CategoryThreadsFixture extends TestFixture
'id' => ['type' => 'integer'],
'parent_id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'null' => false],
'lft' => ['type' => 'integer'],
'rght' => ['type' => 'integer'],
'lft' => ['type' => 'integer', 'unsigned' => true],
'rght' => ['type' => 'integer', 'unsigned' => true],
'created' => 'datetime',
'updated' => 'datetime',
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
Expand Down
6 changes: 3 additions & 3 deletions tests/Fixture/NumberTreesFixture.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ class NumberTreesFixture extends TestFixture
'id' => ['type' => 'integer'],
'name' => ['type' => 'string', 'length' => 50, 'null' => false],
'parent_id' => 'integer',
'lft' => ['type' => 'integer'],
'rght' => ['type' => 'integer'],
'depth' => ['type' => 'integer'],
'lft' => ['type' => 'integer', 'unsigned' => true],
'rght' => ['type' => 'integer', 'unsigned' => true],
'depth' => ['type' => 'integer', 'unsigned' => true],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
];

Expand Down
69 changes: 69 additions & 0 deletions tests/TestCase/Shell/Task/ModelTaskAssociationDetectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@
use Bake\Test\TestCase\TestCase;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Database\Driver\Mysql;
use Cake\Database\Driver\Postgres;
use Cake\Database\Driver\Sqlite;
use Cake\Database\Driver\Sqlserver;
use Cake\Datasource\ConnectionManager;
use Cake\ORM\TableRegistry;

/**
Expand Down Expand Up @@ -146,6 +151,22 @@ protected function _compareBakeTableResult($name, $comparisonFile)
*/
public function testBakeAssociationDetectionCategoriesTable()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Mysql, 'Incompatible with mysql');
$this->_compareBakeTableResult('Categories', __FUNCTION__);
}

/**
* test checking if associations where built correctly for categories.
*
* @return void
*/
public function testBakeAssociationDetectionCategoriesTableSigned()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Sqlite, 'Incompatible with sqlite');
$this->skipIf($driver instanceof Postgres, 'Incompatible with postgres');
$this->skipIf($driver instanceof Sqlserver, 'Incompatible with sqlserver');
$this->_compareBakeTableResult('Categories', __FUNCTION__);
}

Expand All @@ -166,6 +187,22 @@ public function testBakeAssociationDetectionCategoriesProductsTable()
*/
public function testBakeAssociationDetectionOldProductsTable()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Mysql, 'Incompatible with mysql');
$this->_compareBakeTableResult('OldProducts', __FUNCTION__);
}

/**
* test checking if associations where built correctly for old_products.
*
* @return void
*/
public function testBakeAssociationDetectionOldProductsTableSigned()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Sqlite, 'Incompatible with sqlite');
$this->skipIf($driver instanceof Postgres, 'Incompatible with postgres');
$this->skipIf($driver instanceof Sqlserver, 'Incompatible with sqlserver');
$this->_compareBakeTableResult('OldProducts', __FUNCTION__);
}

Expand All @@ -176,6 +213,22 @@ public function testBakeAssociationDetectionOldProductsTable()
*/
public function testBakeAssociationDetectionProductVersionsTable()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Mysql, 'Incompatible with mysql');
$this->_compareBakeTableResult('ProductVersions', __FUNCTION__);
}

/**
* test checking if associations where built correctly for product_versions.
*
* @return void
*/
public function testBakeAssociationDetectionProductVersionsTableSigned()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Sqlite, 'Incompatible with sqlite');
$this->skipIf($driver instanceof Postgres, 'Incompatible with postgres');
$this->skipIf($driver instanceof Sqlserver, 'Incompatible with sqlserver');
$this->_compareBakeTableResult('ProductVersions', __FUNCTION__);
}

Expand All @@ -186,6 +239,22 @@ public function testBakeAssociationDetectionProductVersionsTable()
*/
public function testBakeAssociationDetectionProductsTable()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Mysql, 'Incompatible with mysql');
$this->_compareBakeTableResult('Products', __FUNCTION__);
}

/**
* test checking if associations where built correctly for products.
*
* @return void
*/
public function testBakeAssociationDetectionProductsTableSigned()
{
$driver = ConnectionManager::get('test')->getDriver();
$this->skipIf($driver instanceof Sqlite, 'Incompatible with sqlite');
$this->skipIf($driver instanceof Postgres, 'Incompatible with postgres');
$this->skipIf($driver instanceof Sqlserver, 'Incompatible with sqlserver');
$this->_compareBakeTableResult('Products', __FUNCTION__);
}
}
Loading

0 comments on commit 7356656

Please sign in to comment.