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 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); }
public function testDeferredBinding() { $sessionKey = uniqid('session_key', true); Model::unguard(); $author = Author::create(['name' => 'Stevie']); $meta = 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']); Model::reguard(); $metaId = $meta->id; // Deferred add $author->meta()->add($meta, $sessionKey); $this->assertNull($meta->taggable_id); $this->assertNull($author->meta); $this->assertEquals(0, $author->meta()->count()); $this->assertEquals(1, $author->meta()->withDeferred($sessionKey)->count()); // Commit deferred $author->save(null, $sessionKey); $meta = Meta::find($metaId); $this->assertEquals(1, $author->meta()->count()); $this->assertEquals($author->id, $meta->taggable_id); $this->assertEquals('Comment', $author->meta->meta_title); // New session $sessionKey = uniqid('session_key', true); // Deferred remove $author->meta()->remove($meta, $sessionKey); $this->assertEquals(1, $author->meta()->count()); $this->assertEquals(0, $author->meta()->withDeferred($sessionKey)->count()); $this->assertEquals($author->id, $meta->taggable_id); $this->assertEquals('Comment', $author->meta->meta_title); // Commit deferred $author->save(null, $sessionKey); $meta = Meta::find($metaId); $this->assertEquals(0, $author->meta()->count()); $this->assertNull($meta->taggable_id); $this->assertNull($author->meta); }
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); }
public function testDeferredBinding() { $sessionKey = uniqid('session_key', true); Model::unguard(); $author = Author::create(['name' => 'Stevie']); $event = EventLog::create(['action' => "user-created"]); Model::reguard(); $eventId = $event->id; // Deferred add $author->event_log()->add($event, $sessionKey); $this->assertNull($event->related_id); $this->assertEmpty($author->event_log); $this->assertEquals(0, $author->event_log()->count()); $this->assertEquals(1, $author->event_log()->withDeferred($sessionKey)->count()); // Commit deferred $author->save(null, $sessionKey); $event = EventLog::find($eventId); $this->assertEquals(1, $author->event_log()->count()); $this->assertEquals($author->id, $event->related_id); $this->assertEquals(['user-created'], $author->event_log->lists('action')); // New session $sessionKey = uniqid('session_key', true); // Deferred remove $author->event_log()->remove($event, $sessionKey); $this->assertEquals(1, $author->event_log()->count()); $this->assertEquals(0, $author->event_log()->withDeferred($sessionKey)->count()); $this->assertEquals($author->id, $event->related_id); $this->assertEquals(['user-created'], $author->event_log->lists('action')); // Commit deferred (model is deleted as per definition) $author->save(null, $sessionKey); $event = EventLog::find($eventId); $this->assertEquals(0, $author->event_log()->count()); $this->assertNull($event); $this->assertEmpty($author->event_log); }
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 testGetRelationValue() { Model::unguard(); $author = Author::create(['name' => 'Stevie']); $event = EventLog::make(['action' => "user-created", 'related_id' => $author->id, 'related_type' => get_class($author)]); Model::reguard(); $this->assertEquals([$author->id, get_class($author)], $event->getRelationValue('related')); }
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 testDeleteOptionOnSoftModel() { Model::unguard(); $user = UserWithAuthorAndSoftDelete::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $author = Author::create(['name' => 'Louie', 'email' => '*****@*****.**', 'user_id' => $user->id]); Model::reguard(); $authorId = $author->id; $user->delete(); // Soft $this->assertNotNull(Author::find($authorId)); // Do nothing $userId = $user->id; $user = UserWithAuthorAndSoftDelete::withTrashed()->find($userId); $user->restore(); $user->forceDelete(); // Hard $this->assertNull(Author::find($authorId)); }
public function testConditionsWithPivotAttributes() { Model::unguard(); $author = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $role1 = Role::create(['name' => "Designer", 'description' => "Quality"]); $role2 = Role::create(['name' => "Programmer", 'description' => "Speed"]); $role3 = Role::create(['name' => "Manager", 'description' => "Budget"]); Model::reguard(); $author->roles()->add($role1, null, ['is_executive' => 1]); $author->roles()->add($role2, null, ['is_executive' => 1]); $author->roles()->add($role3, null, ['is_executive' => 0]); $this->assertEquals([1, 2], $author->executive_authors->lists('id')); }
public function testDetachAfterDelete() { // Needed for other "delete" events include_once base_path() . '/tests/fixtures/plugins/database/tester/models/User.php'; include_once base_path() . '/tests/fixtures/plugins/database/tester/models/EventLog.php'; Model::unguard(); $author = Author::create(['name' => 'Stevie', 'email' => '*****@*****.**']); $role1 = Role::create(['name' => "Designer", 'description' => "Quality"]); $role2 = Role::create(['name' => "Programmer", 'description' => "Speed"]); $role3 = Role::create(['name' => "Manager", 'description' => "Budget"]); Model::reguard(); $author->roles()->add($role1); $author->roles()->add($role2); $author->roles()->add($role3); $this->assertEquals(3, Db::table('database_tester_authors_roles')->where('author_id', $author->id)->count()); $author->delete(); $this->assertEquals(0, Db::table('database_tester_authors_roles')->where('author_id', $author->id)->count()); }