Skip to content

Commit

Permalink
Write tests for deferred binding
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Sep 15, 2015
1 parent 293a047 commit bf961a9
Show file tree
Hide file tree
Showing 3 changed files with 155 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
* **Build 297** (2015-09-16)
- Fixed a bug in deferred binding that allowed repeat bindings and ignored add/delete pairs.

* **Build 293** (2015-09-07)
- Corrected a flaw in the Behavior/Extension logic that previously made protected methods and properties accessible as if they were public.

Expand Down
45 changes: 43 additions & 2 deletions tests/unit/plugins/database/BelongsToModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function setUp()
$this->runPluginRefreshCommand('Database.Tester');
}

public function testSetRelationValueBelongsTo()
public function testSetRelationValue()
{
Model::unguard();
$post = Post::create(['title' => "First post", 'description' => "Yay!!"]);
Expand All @@ -39,11 +39,52 @@ public function testSetRelationValueBelongsTo()
$this->assertNull($post->author_id);
$this->assertNull($post->author);

// Deferred
// Deferred in memory
$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 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);
}

}
109 changes: 109 additions & 0 deletions tests/unit/plugins/database/DeferredBindingTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php

use Database\Tester\Models\Post;
use Database\Tester\Models\Author;
use October\Rain\Database\Models\DeferredBinding;

class DeferredBindingTest 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 testNegatedBinding()
{
$sessionKey = uniqid('session_key', true);
DeferredBinding::truncate();

Model::unguard();
$author = Author::make(['name' => 'Stevie']);
$post = Post::create(['title' => "First post"]);
$post2 = Post::create(['title' => "Second post"]);
Model::reguard();

$author->posts()->add($post, $sessionKey);
$this->assertEquals(1, DeferredBinding::count());

// Skip repeat bindings
$author->posts()->add($post, $sessionKey);
$this->assertEquals(1, DeferredBinding::count());

// Remove add-delete pairs
$author->posts()->remove($post, $sessionKey);
$this->assertEquals(0, DeferredBinding::count());

// Multi ball
$sessionKey = uniqid('session_key', true);
$author->posts()->add($post, $sessionKey);
$author->posts()->add($post, $sessionKey);
$author->posts()->add($post, $sessionKey);
$author->posts()->add($post, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$this->assertEquals(2, DeferredBinding::count());

// Clean up add-delete pairs
$author->posts()->remove($post, $sessionKey);
$author->posts()->remove($post2, $sessionKey);
$this->assertEquals(0, DeferredBinding::count());

// Double negative
$author->posts()->remove($post, $sessionKey);
$author->posts()->remove($post2, $sessionKey);
$this->assertEquals(2, DeferredBinding::count());

// Skip repeat bindings
$author->posts()->remove($post, $sessionKey);
$author->posts()->remove($post2, $sessionKey);
$this->assertEquals(2, DeferredBinding::count());

// Clean up add-delete pairs again
$author->posts()->add($post, $sessionKey);
$author->posts()->add($post2, $sessionKey);
$this->assertEquals(0, DeferredBinding::count());
}

public function testCancelBinding()
{
$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->cancelDeferred($sessionKey);
$this->assertEquals(0, DeferredBinding::count());
}

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());
}

}

0 comments on commit bf961a9

Please sign in to comment.