public function testSetRelationValue() { Model::unguard(); $author = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $post1 = Post::create(['title' => "First post", 'description' => "Yay!!"]); $post2 = Post::create(['title' => "Second post", 'description' => "Woohoo!!"]); $post3 = Post::create(['title' => "Third post", 'description' => "Yipiee!!"]); $post4 = Post::make(['title' => "Fourth post", 'description' => "Hooray!!"]); Model::reguard(); // Set by Model object $author->posts = new Collection([$post1, $post2]); $author->save(); $this->assertEquals($author->id, $post1->author_id); $this->assertEquals($author->id, $post2->author_id); $this->assertEquals(['First post', 'Second post'], $author->posts->lists('title')); // Set by primary key $postId = $post3->id; $author->posts = $postId; $author->save(); $post3 = Post::find($postId); $this->assertEquals($author->id, $post3->author_id); $this->assertEquals(['Third post'], $author->posts->lists('title')); // Nullify $author->posts = null; $author->save(); $post3 = Post::find($postId); $this->assertNull($post3->author_id); $this->assertNull($post3->author); // Deferred in memory $author->posts = $post4; $this->assertEquals($author->id, $post4->author_id); $this->assertEquals(['Fourth post'], $author->posts->lists('title')); }
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); }