Merge pull request #1724 from scottbedard/stable

Add tests for Nullable trait
This commit is contained in:
Samuel Georges 2016-01-27 08:05:13 +11:00
commit a8e72541e1
3 changed files with 51 additions and 0 deletions

View File

@ -33,6 +33,23 @@ class Post extends Model
}
class NullablePost extends Post
{
use \October\Rain\Database\Traits\Nullable;
/**
* @var array Guarded fields
*/
protected $guarded = [];
/**
* @var array List of attributes to nullify
*/
protected $nullable = [
'author_nickname',
];
}
class SluggablePost extends Post
{

View File

@ -18,6 +18,7 @@ class CreatePostsTable extends Migration
$table->boolean('is_published')->default(false);
$table->timestamp('published_at')->nullable();
$table->integer('author_id')->unsigned()->index()->nullable();
$table->string('author_nickname')->default('October')->nullable();
$table->softDeletes();
$table->timestamps();
});

View File

@ -0,0 +1,33 @@
<?php
use Database\Tester\Models\NullablePost;
class NullableModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testNullifyingFields()
{
// The nullable field should be unset and use sql default
$post = NullablePost::create(['author_nickname' => '']);
$this->assertEquals('October', NullablePost::find($post->id)->author_nickname);
// Once saved, fields will be set to null
$post->author_nickname = '';
$post->save();
$this->assertNull($post->author_nickname);
}
public function testNonEmptyValuesAreIgnored()
{
$post = NullablePost::create(['author_nickname' => 'Joe']);
$this->assertEquals('Joe', $post->author_nickname);
}
}