public function testSetRelationValue() { Model::unguard(); $post = Post::create(['title' => "First post", 'description' => "Yay!!"]); $author = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $meta1 = Meta::create(['meta_title' => 'Question', 'meta_description' => 'Industry', 'meta_keywords' => 'major', 'canonical_url' => 'http://google.com/search/jobs', 'redirect_url' => 'http://google.com', 'robot_index' => 'index', 'robot_follow' => 'follow']); $meta2 = Meta::create(['meta_title' => 'Comment', 'meta_description' => 'Social', 'meta_keywords' => 'startup', 'canonical_url' => 'http://facebook.com/search/users', 'redirect_url' => 'http://facebook.com', 'robot_index' => 'index', 'robot_follow' => 'follow']); $meta3 = Meta::make(['meta_title' => 'Answer', 'meta_description' => 'Employment', 'meta_keywords' => 'minor', 'canonical_url' => 'http://yahoo.com/search/stats', 'redirect_url' => 'http://yahoo.com', 'robot_index' => 'index', 'robot_follow' => 'follow']); Model::reguard(); // Set by Model object $post->meta = $meta1; $post->save(); $this->assertEquals($post->id, $meta1->taggable_id); $this->assertEquals(get_class($post), $meta1->taggable_type); $this->assertEquals('Question', $post->meta->meta_title); // Set by primary key $metaId = $meta2->id; $author->meta = $metaId; $author->save(); $meta2 = Meta::find($metaId); $this->assertEquals($author->id, $meta2->taggable_id); $this->assertEquals(get_class($author), $meta2->taggable_type); $this->assertEquals('Comment', $author->meta->meta_title); // Nullify $author->meta = null; $author->save(); $meta = Meta::find($metaId); $this->assertNull($meta->taggable_type); $this->assertNull($meta->taggable_id); $this->assertNull($meta->taggable); // Deferred in memory $author->meta = $meta3; $this->assertEquals('Answer', $author->meta->meta_title); $this->assertEquals($author->id, $meta3->taggable_id); }
public function testDeferredBinding() { $sessionKey = uniqid('session_key', true); Model::unguard(); $post = Post::make(['title' => "First post"]); $author = Author::create(['name' => 'Stevie']); Model::reguard(); // Deferred add $post->author()->add($author, $sessionKey); $this->assertNull($post->author_id); $this->assertNull($post->author); $this->assertEquals(0, $post->author()->count()); $this->assertEquals(1, $post->author()->withDeferred($sessionKey)->count()); // Commit deferred $post->save(null, $sessionKey); $this->assertEquals(1, $post->author()->count()); $this->assertEquals($author->id, $post->author_id); $this->assertEquals('Stevie', $post->author->name); // New session $sessionKey = uniqid('session_key', true); // Deferred remove $post->author()->remove($author, $sessionKey); $this->assertEquals(1, $post->author()->count()); $this->assertEquals(0, $post->author()->withDeferred($sessionKey)->count()); $this->assertEquals($author->id, $post->author_id); $this->assertEquals('Stevie', $post->author->name); // Commit deferred $post->save(null, $sessionKey); $this->assertEquals(0, $post->author()->count()); $this->assertNull($post->author_id); $this->assertNull($post->author); }
public function testSetRelationValue() { Model::unguard(); $author = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $post1 = Post::create(['title' => "First post", 'description' => "Yay!!"]); $post2 = Post::make(['title' => "Second post", 'description' => "Woohoo!!"]); $event = EventLog::create(['action' => "user-created"]); Model::reguard(); // Set by Model object $event->related = $author; $event->save(); $this->assertEquals($author->id, $event->related_id); $this->assertEquals('Stevie', $event->related->name); // Set by primary key $event->related = [$post1->id, get_class($post1)]; $this->assertEquals($post1->id, $event->related_id); $this->assertEquals('First post', $event->related->title); // Nullify $event->related = null; $this->assertNull($event->related_id); $this->assertNull($event->related); // Deferred in memory $event->related = $post2; $this->assertEquals('Second post', $event->related->title); $this->assertNull($event->related_id); $event->save(); $this->assertEquals($post2->id, $event->related_id); }
public function testSetRelationValueBelongsTo() { Model::unguard(); $post = Post::create(['title' => "First post", 'description' => "Yay!!"]); $author1 = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $author2 = Author::create(['name' => 'Louie', 'email' => '*****@*****.**']); $author3 = Author::make(['name' => 'Charlie', 'email' => '*****@*****.**']); 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); }
public function testCommitBinding() { $sessionKey = uniqid('session_key', true); DeferredBinding::truncate(); Model::unguard(); $author = Author::make(['name' => 'Stevie']); $post = Post::create(['title' => "First post"]); Model::reguard(); $author->posts()->add($post, $sessionKey); $this->assertEquals(1, DeferredBinding::count()); $author->commitDeferred($sessionKey); $this->assertEquals(0, DeferredBinding::count()); }
public function testDeferredBinding() { $sessionKey = uniqid('session_key', true); Model::unguard(); $author = Author::create(['name' => 'Stevie']); $post = Post::create(['title' => "First post", 'description' => "Yay!!"]); Model::reguard(); $postId = $post->id; // Deferred add $author->posts()->add($post, $sessionKey); $this->assertNull($post->author_id); $this->assertEmpty($author->posts); $this->assertEquals(0, $author->posts()->count()); $this->assertEquals(1, $author->posts()->withDeferred($sessionKey)->count()); // Commit deferred $author->save(null, $sessionKey); $post = Post::find($postId); $this->assertEquals(1, $author->posts()->count()); $this->assertEquals($author->id, $post->author_id); $this->assertEquals(['First post'], $author->posts->lists('title')); // New session $sessionKey = uniqid('session_key', true); // Deferred remove $author->posts()->remove($post, $sessionKey); $this->assertEquals(1, $author->posts()->count()); $this->assertEquals(0, $author->posts()->withDeferred($sessionKey)->count()); $this->assertEquals($author->id, $post->author_id); $this->assertEquals(['First post'], $author->posts->lists('title')); // Commit deferred $author->save(null, $sessionKey); $post = Post::find($postId); $this->assertEquals(0, $author->posts()->count()); $this->assertNull($post->author_id); $this->assertEmpty($author->posts); }
/** * @expectedException \Illuminate\Database\Eloquent\MassAssignmentException * @expectedExceptionMessage title */ public function testGuardedAttribute() { Post::create(['title' => 'Hi!', 'slug' => 'authenticity']); }