ORIENT/tests/unit/plugins/database/BelongsToModelTest.php

49 lines
1.6 KiB
PHP

<?php
use Database\Tester\Models\Post;
use Database\Tester\Models\Author;
class BelongsToModelTest extends PluginTestCase
{
public function setUp()
{
parent::setUp();
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Post.php';
include_once base_path().'/tests/fixtures/plugins/database/tester/models/Author.php';
$this->runPluginRefreshCommand('Database.Tester');
}
public function testSetRelationValueBelongsTo()
{
Model::unguard();
$post = Post::create(['title' => "First post", 'description' => "Yay!!"]);
$author1 = Author::create(['name' => 'Stevie', 'email' => 'stevie@email.tld']);
$author2 = Author::create(['name' => 'Louie', 'email' => 'louie@email.tld']);
$author3 = Author::make(['name' => 'Charlie', 'email' => 'charlie@email.tld']);
Model::reguard();
// Set by Model object
$post->author = $author1;
$this->assertEquals($author1->id, $post->author_id);
$this->assertEquals('Stevie', $post->author->name);
// Set by primary key
$post->author = $author2->id;
$this->assertEquals($author2->id, $post->author_id);
$this->assertEquals('Louie', $post->author->name);
// Nullify
$post->author = null;
$this->assertNull($post->author_id);
$this->assertNull($post->author);
// Deferred
$post->author = $author3;
$this->assertEquals('Charlie', $post->author->name);
$this->assertNull($post->author_id);
$author3->save();
$this->assertEquals($author3->id, $post->author_id);
}
}