Skip to content

Commit

Permalink
Merge pull request #9 from dereuromark/master-nullable-schema-defaults
Browse files Browse the repository at this point in the history
Try to use schema default in nullable case.
  • Loading branch information
dereuromark committed Nov 3, 2016
2 parents 67d5297 + bbe18b7 commit 373951d
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 4 deletions.
3 changes: 3 additions & 0 deletions composer.json
Expand Up @@ -34,5 +34,8 @@
"Cake\\Test\\": "vendor/cakephp/cakephp/tests",
"TestApp\\": "tests/test_app/TestApp"
}
},
"config": {
"process-timeout": 600
}
}
3 changes: 2 additions & 1 deletion src/Model/Behavior/NullableBehavior.php
Expand Up @@ -49,7 +49,8 @@ protected function _process($data, Table $table) {
continue;
}

$data[$key] = null;
$default = Hash::get((array)$table->schema()->column($key), 'default');
$data[$key] = $default;
}

return $data;
Expand Down
7 changes: 4 additions & 3 deletions tests/Fixture/NullablesFixture.php
Expand Up @@ -15,11 +15,12 @@ class NullablesFixture extends TestFixture {
'id' => ['type' => 'integer'],
'optional_id' => ['type' => 'integer', 'null' => true],
'required_id' => ['type' => 'integer', 'null' => false],
'comment' => 'text',
'string_optional' => ['type' => 'text', 'null' => true],
'string_required' => ['type' => 'text', 'null' => false],
'string_optional' => ['type' => 'string', 'null' => true],
'string_required' => ['type' => 'string', 'null' => false],
'string_optional_notnull' => ['type' => 'string', 'null' => true, 'default' => ''],
'active_optional' => ['type' => 'boolean', 'null' => true],
'active_required' => ['type' => 'boolean', 'null' => false],
'active_optional_notnull' => ['type' => 'boolean', 'null' => true, 'default' => 0],
'datetime_optional' => ['type' => 'datetime', 'null' => true],
'datetime_required' => ['type' => 'datetime', 'null' => false],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
Expand Down
20 changes: 20 additions & 0 deletions tests/TestCase/Model/Behavior/NullableBehaviorTest.php
Expand Up @@ -2,6 +2,7 @@

namespace Tools\Test\TestCase\Model\Behavior;

use Cake\Core\Configure;
use Cake\ORM\TableRegistry;
use Shim\TestSuite\TestCase;

Expand Down Expand Up @@ -58,4 +59,23 @@ public function testPatch() {
$this->assertSame($expected, $entity->toArray());
}

/**
* @return void
*/
public function testPatchOptionalNotNull() {
$this->skipIf(version_compare(Configure::version(), '3.3.7') <= 0);

$data = [
'string_optional_notnull' => '',
'active_optional_notnull' => '',
];
$entity = $this->Table->newEntity($data);

$expected = [
'string_optional_notnull' => '',
'active_optional_notnull' => false,
];
$this->assertSame($expected, $entity->toArray());
}

}

0 comments on commit 373951d

Please sign in to comment.