mirror of
https://github.com/wintercms/winter.git
synced 2024-06-28 05:33:29 +02:00
Merge branch 'develop' of github.com:octobercms/october into develop
This commit is contained in:
commit
66faa42a2d
@ -22,4 +22,11 @@ class Author extends Model
|
||||
'posts' => 'Database\Tester\Models\Post',
|
||||
];
|
||||
|
||||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $hasOne = [
|
||||
'phone' => 'Database\Tester\Models\Phone',
|
||||
];
|
||||
|
||||
}
|
30
tests/fixtures/plugins/database/tester/models/Phone.php
vendored
Normal file
30
tests/fixtures/plugins/database/tester/models/Phone.php
vendored
Normal file
@ -0,0 +1,30 @@
|
||||
<?php namespace Database\Tester\Models;
|
||||
|
||||
use Model;
|
||||
|
||||
class Phone extends Model
|
||||
{
|
||||
|
||||
/**
|
||||
* @var string The database table used by the model.
|
||||
*/
|
||||
public $table = 'database_tester_phones';
|
||||
|
||||
/**
|
||||
* @var array Guarded fields
|
||||
*/
|
||||
protected $guarded = ['*'];
|
||||
|
||||
/**
|
||||
* @var array Fillable fields
|
||||
*/
|
||||
protected $fillable = [];
|
||||
|
||||
/**
|
||||
* @var array Relations
|
||||
*/
|
||||
public $belongsTo = [
|
||||
'author' => 'Database\Tester\Models\Author',
|
||||
];
|
||||
|
||||
}
|
26
tests/fixtures/plugins/database/tester/updates/create_phones_table.php
vendored
Normal file
26
tests/fixtures/plugins/database/tester/updates/create_phones_table.php
vendored
Normal file
@ -0,0 +1,26 @@
|
||||
<?php namespace Database\Tester\Updates;
|
||||
|
||||
use Schema;
|
||||
use October\Rain\Database\Updates\Migration;
|
||||
|
||||
class CreatePhonesTable extends Migration
|
||||
{
|
||||
|
||||
public function up()
|
||||
{
|
||||
Schema::create('database_tester_phones', function($table)
|
||||
{
|
||||
$table->engine = 'InnoDB';
|
||||
$table->increments('id');
|
||||
$table->string('number')->nullable();
|
||||
$table->integer('author_id')->unsigned()->index()->nullable();
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('database_tester_phones');
|
||||
}
|
||||
|
||||
}
|
@ -3,4 +3,5 @@
|
||||
- Create tables
|
||||
- create_posts_table.php
|
||||
- create_authors_table.php
|
||||
- create_phones_table.php
|
||||
- create_categories_table.php
|
||||
|
@ -47,6 +47,16 @@ class BelongsToModelTest extends PluginTestCase
|
||||
$this->assertEquals($author3->id, $post->author_id);
|
||||
}
|
||||
|
||||
public function testGetRelationValue()
|
||||
{
|
||||
Model::unguard();
|
||||
$author = Author::create(['name' => 'Stevie']);
|
||||
$post = Post::make(['title' => "First post", 'author_id' => $author->id]);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertEquals($author->id, $post->getRelationValue('author'));
|
||||
}
|
||||
|
||||
public function testDeferredBinding()
|
||||
{
|
||||
$sessionKey = uniqid('session_key', true);
|
||||
@ -87,4 +97,5 @@ class BelongsToModelTest extends PluginTestCase
|
||||
$this->assertNull($post->author);
|
||||
}
|
||||
|
||||
|
||||
}
|
107
tests/unit/plugins/database/HasOneModelTest.php
Normal file
107
tests/unit/plugins/database/HasOneModelTest.php
Normal file
@ -0,0 +1,107 @@
|
||||
<?php
|
||||
|
||||
use Database\Tester\Models\Author;
|
||||
use Database\Tester\Models\Phone;
|
||||
|
||||
class HasOneModelTest extends PluginTestCase
|
||||
{
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
|
||||
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Phone.php';
|
||||
|
||||
$this->runPluginRefreshCommand('Database.Tester');
|
||||
}
|
||||
|
||||
public function testSetRelationValue()
|
||||
{
|
||||
Model::unguard();
|
||||
$author = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
|
||||
$phone1 = Phone::create(['number' => '0404040404']);
|
||||
$phone2 = Phone::create(['number' => '0505050505']);
|
||||
$phone3 = Phone::make(['number' => '0606060606']);
|
||||
Model::reguard();
|
||||
|
||||
// Set by Model object
|
||||
$author->phone = $phone1;
|
||||
$author->save();
|
||||
$this->assertEquals($author->id, $phone1->author_id);
|
||||
$this->assertEquals('0404040404', $author->phone->number);
|
||||
|
||||
// Set by primary key
|
||||
$phoneId = $phone2->id;
|
||||
$author->phone = $phoneId;
|
||||
$author->save();
|
||||
$phone2 = Phone::find($phoneId);
|
||||
$this->assertEquals($author->id, $phone2->author_id);
|
||||
$this->assertEquals('0505050505', $author->phone->number);
|
||||
|
||||
// Nullify
|
||||
$author->phone = null;
|
||||
$author->save();
|
||||
$phone2 = Phone::find($phoneId);
|
||||
$this->assertNull($phone2->author_id);
|
||||
$this->assertNull($phone2->author);
|
||||
|
||||
// Deferred in memory
|
||||
$author->phone = $phone3;
|
||||
$this->assertEquals('0606060606', $author->phone->number);
|
||||
$this->assertEquals($author->id, $phone3->author_id);
|
||||
}
|
||||
|
||||
public function testGetRelationValue()
|
||||
{
|
||||
Model::unguard();
|
||||
$author = Author::create(['name' => 'Stevie']);
|
||||
$phone = Phone::create(['number' => '0404040404', 'author_id' => $author->id]);
|
||||
Model::reguard();
|
||||
|
||||
$this->assertEquals($phone->id, $author->getRelationValue('phone'));
|
||||
}
|
||||
|
||||
public function testDeferredBinding()
|
||||
{
|
||||
$sessionKey = uniqid('session_key', true);
|
||||
|
||||
Model::unguard();
|
||||
$author = Author::create(['name' => 'Stevie']);
|
||||
$phone = Phone::create(['number' => '0404040404']);
|
||||
Model::reguard();
|
||||
|
||||
$phoneId = $phone->id;
|
||||
|
||||
// Deferred add
|
||||
$author->phone()->add($phone, $sessionKey);
|
||||
$this->assertNull($phone->author_id);
|
||||
$this->assertNull($author->phone);
|
||||
|
||||
$this->assertEquals(0, $author->phone()->count());
|
||||
$this->assertEquals(1, $author->phone()->withDeferred($sessionKey)->count());
|
||||
|
||||
// Commit deferred
|
||||
$author->save(null, $sessionKey);
|
||||
$phone = Phone::find($phoneId);
|
||||
$this->assertEquals(1, $author->phone()->count());
|
||||
$this->assertEquals($author->id, $phone->author_id);
|
||||
$this->assertEquals('0404040404', $author->phone->number);
|
||||
|
||||
// New session
|
||||
$sessionKey = uniqid('session_key', true);
|
||||
|
||||
// Deferred remove
|
||||
$author->phone()->remove($phone, $sessionKey);
|
||||
$this->assertEquals(1, $author->phone()->count());
|
||||
$this->assertEquals(0, $author->phone()->withDeferred($sessionKey)->count());
|
||||
$this->assertEquals($author->id, $phone->author_id);
|
||||
$this->assertEquals('0404040404', $author->phone->number);
|
||||
|
||||
// Commit deferred
|
||||
$author->save(null, $sessionKey);
|
||||
$phone = Phone::find($phoneId);
|
||||
$this->assertEquals(0, $author->phone()->count());
|
||||
$this->assertNull($phone->author_id);
|
||||
$this->assertNull($author->phone);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user