diff --git a/phpunit.xml b/phpunit.xml
index 6d9692d367590cbe08ed4835f82a4714ab83a063..8f7848d64582ad360fdb5006bcf836a9cd30ae87 100644
--- a/phpunit.xml
+++ b/phpunit.xml
@@ -34,6 +34,7 @@
</testsuite>
<testsuite name="relations">
<directory>tests/RelationsTest.php</directory>
+ <directory>tests/EmbeddedRelationsTest.php</directory>
</testsuite>
<testsuite name="mysqlrelations">
<directory>tests/RelationsTest.php</directory>
diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php
index 727efcf313e0b3671e7cfeec97071643ac42090d..d1814429bc50b9812a9c029e499ef3792757abbb 100644
--- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php
+++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php
@@ -19,36 +19,17 @@ class EmbedsMany extends EmbedsOneOrMany {
}
/**
- * Find an embedded model by its primary key.
+ * Simulate order by method.
*
- * @param mixed $id
- * @return \Illuminate\Database\Eloquent\Collection
+ * @param string $column
+ * @param string $direction
+ * @return Illuminate\Database\Eloquent\Collection
*/
- public function find($id)
+ public function orderBy($column, $direction = 'asc')
{
- if ($id instanceof Model) $id = $id->getKey();
-
- $primaryKey = $this->related->getKeyName();
+ $descending = strtolower($direction) == 'desc';
- // Traverse all embedded records and find the first record
- // that matches the given primary key.
- $record = array_first($this->getEmbedded(), function($itemKey, $record) use ($primaryKey, $id)
- {
- return $record[$primaryKey] == $id;
- });
-
- return $record ? $this->toModel($record) : null;
- }
-
- /**
- * Check if a model is already embedded.
- *
- * @param mixed $key
- * @return bool
- */
- public function contains($key)
- {
- return ! is_null($this->find($key));
+ return $this->getResults()->sortBy($column, SORT_REGULAR, $descending);
}
/**
@@ -314,4 +295,22 @@ class EmbedsMany extends EmbedsOneOrMany {
return parent::setEmbedded(array_values($models));
}
+ /**
+ * Handle dynamic method calls to the relationship.
+ *
+ * @param string $method
+ * @param array $parameters
+ * @return mixed
+ */
+ public function __call($method, $parameters)
+ {
+ // Collection methods
+ if (method_exists('Illuminate\Database\Eloquent\Collection', $method))
+ {
+ return call_user_func_array(array($this->getResults(), $method), $parameters);
+ }
+
+ return parent::__call($method, $parameters);
+ }
+
}
diff --git a/tests/EmbeddedRelationsTest.php b/tests/EmbeddedRelationsTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..cf7e4290411f451f12e52f521ebf4d76275016c4
--- /dev/null
+++ b/tests/EmbeddedRelationsTest.php
@@ -0,0 +1,427 @@
+<?php
+
+class EmbeddedRelationsTest extends TestCase {
+
+ public function tearDown()
+ {
+ Mockery::close();
+
+ User::truncate();
+ Book::truncate();
+ Item::truncate();
+ Role::truncate();
+ Client::truncate();
+ Group::truncate();
+ Photo::truncate();
+ }
+
+ public function testEmbedsManySave()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'London'));
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($address), $address);
+ $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($address), $address);
+
+ $address = $user->addresses()->save($address);
+ $address->unsetEventDispatcher();
+
+ $this->assertNotNull($user->_addresses);
+ $this->assertEquals(array('London'), $user->addresses->lists('city'));
+ $this->assertInstanceOf('DateTime', $address->created_at);
+ $this->assertInstanceOf('DateTime', $address->updated_at);
+ $this->assertNotNull($address->_id);
+ $this->assertTrue(is_string($address->_id));
+
+ $raw = $address->getAttributes();
+ $this->assertInstanceOf('MongoId', $raw['_id']);
+
+ $address = $user->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $user = User::find($user->_id);
+ $this->assertEquals(array('London', 'Paris'), $user->addresses->lists('city'));
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.updated: '.get_class($address), $address);
+ $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($address), $address);
+
+ $address->city = 'New York';
+ $user->addresses()->save($address);
+ $address->unsetEventDispatcher();
+
+ $this->assertEquals(2, count($user->addresses));
+ $this->assertEquals(2, count($user->addresses()->get()));
+ $this->assertEquals(2, $user->addresses->count());
+ $this->assertEquals(2, $user->addresses()->count());
+ $this->assertEquals(array('London', 'New York'), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->_id);
+ $this->assertEquals(array('London', 'New York'), $freshUser->addresses->lists('city'));
+
+ $address = $user->addresses->first();
+ $this->assertEquals('London', $address->city);
+ $this->assertInstanceOf('DateTime', $address->created_at);
+ $this->assertInstanceOf('DateTime', $address->updated_at);
+ $this->assertInstanceOf('User', $address->user);
+ $this->assertEmpty($address->relationsToArray()); // prevent infinite loop
+
+ $user = User::find($user->_id);
+ $user->addresses()->save(new Address(array('city' => 'Bruxelles')));
+ $this->assertEquals(array('London', 'New York', 'Bruxelles'), $user->addresses->lists('city'));
+ $address = $user->addresses[1];
+ $address->city = "Manhattan";
+ $user->addresses()->save($address);
+ $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->_id);
+ $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyAssociate()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'London'));
+
+ $address = $user->addresses()->associate($address);
+ $this->assertNotNull($user->_addresses);
+ $this->assertEquals(array('London'), $user->addresses->lists('city'));
+ $this->assertNotNull($address->_id);
+
+ $freshUser = User::find($user->_id);
+ $this->assertEquals(array(), $freshUser->addresses->lists('city'));
+
+ $address->city = 'Londinium';
+ $user->addresses()->associate($address);
+ $this->assertEquals(array('Londinium'), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->_id);
+ $this->assertEquals(array(), $freshUser->addresses->lists('city'));
+ }
+
+ public function testEmbedsManySaveMany()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol'))));
+ $this->assertEquals(array('London', 'Bristol'), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(array('London', 'Bristol'), $freshUser->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyDuplicate()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'London'));
+ $user->addresses()->save($address);
+ $user->addresses()->save($address);
+ $this->assertEquals(1, $user->addresses->count());
+ $this->assertEquals(array('London'), $user->addresses->lists('city'));
+
+ $user = User::find($user->id);
+ $this->assertEquals(1, $user->addresses->count());
+
+ $address->city = 'Paris';
+ $user->addresses()->save($address);
+ $this->assertEquals(1, $user->addresses->count());
+ $this->assertEquals(array('Paris'), $user->addresses->lists('city'));
+
+ $user->addresses()->create(array('_id' => $address->_id, 'city' => 'Bruxelles'));
+ $this->assertEquals(1, $user->addresses->count());
+ $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyCreate()
+ {
+ $user = User::create(array());
+ $address = $user->addresses()->create(array('city' => 'Bruxelles'));
+ $this->assertInstanceOf('Address', $address);
+ $this->assertTrue(is_string($address->_id));
+ $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
+
+ $raw = $address->getAttributes();
+ $this->assertInstanceOf('MongoId', $raw['_id']);
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
+
+ $user = User::create(array());
+ $address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
+ $this->assertTrue(is_string($address->_id));
+
+ $raw = $address->getAttributes();
+ $this->assertInstanceOf('MongoId', $raw['_id']);
+ }
+
+ public function testEmbedsManyCreateMany()
+ {
+ $user = User::create(array());
+ list($bruxelles, $paris) = $user->addresses()->createMany(array(array('city' => 'Bruxelles'), array('city' => 'Paris')));
+ $this->assertInstanceOf('Address', $bruxelles);
+ $this->assertEquals('Bruxelles', $bruxelles->city);
+ $this->assertEquals(array('Bruxelles', 'Paris'), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyDestroy()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
+
+ $address = $user->addresses->first();
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.deleting: '.get_class($address), Mockery::mustBe($address))->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.deleted: '.get_class($address), Mockery::mustBe($address));
+
+ $user->addresses()->destroy($address->_id);
+ $this->assertEquals(array('Bristol', 'Bruxelles'), $user->addresses->lists('city'));
+
+ $address->unsetEventDispatcher();
+
+ $address = $user->addresses->first();
+ $user->addresses()->destroy($address);
+ $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
+
+ $user->addresses()->create(array('city' => 'Paris'));
+ $user->addresses()->create(array('city' => 'San Francisco'));
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(array('Bruxelles', 'Paris', 'San Francisco'), $freshUser->addresses->lists('city'));
+
+ $ids = $user->addresses->lists('_id');
+ $user->addresses()->destroy($ids);
+ $this->assertEquals(array(), $user->addresses->lists('city'));
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(array(), $freshUser->addresses->lists('city'));
+
+ list($london, $bristol, $bruxelles) = $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
+ $user->addresses()->destroy(array($london, $bruxelles));
+ $this->assertEquals(array('Bristol'), $user->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyDissociate()
+ {
+ $user = User::create(array());
+ $cordoba = $user->addresses()->create(array('city' => 'Cordoba'));
+
+ $user->addresses()->dissociate($cordoba->id);
+
+ $freshUser = User::find($user->id);
+ $this->assertEquals(0, $user->addresses->count());
+ $this->assertEquals(1, $freshUser->addresses->count());
+ }
+
+ public function testEmbedsManyAliases()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'London'));
+
+ $address = $user->addresses()->attach($address);
+ $this->assertEquals(array('London'), $user->addresses->lists('city'));
+
+ $user->addresses()->detach($address);
+ $this->assertEquals(array(), $user->addresses->lists('city'));
+ }
+
+ public function testEmbedsManyCreatingEventReturnsFalse()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'London'));
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($address), $address)->andReturn(false);
+
+ $this->assertFalse($user->addresses()->save($address));
+ $address->unsetEventDispatcher();
+ }
+
+ public function testEmbedsManySavingEventReturnsFalse()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'Paris'));
+ $address->exists = true;
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(false);
+
+ $this->assertFalse($user->addresses()->save($address));
+ $address->unsetEventDispatcher();
+ }
+
+ public function testEmbedsManyUpdatingEventReturnsFalse()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address = new Address(array('city' => 'New York'));
+ $user->addresses()->save($address);
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($address), $address)->andReturn(false);
+
+ $address->city = 'Warsaw';
+
+ $this->assertFalse($user->addresses()->save($address));
+ $address->unsetEventDispatcher();
+ }
+
+ public function testEmbedsManyDeletingEventReturnsFalse()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $user->addresses()->save(new Address(array('city' => 'New York')));
+
+ $address = $user->addresses->first();
+
+ $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.deleting: '.get_class($address), Mockery::mustBe($address))->andReturn(false);
+
+ $this->assertEquals(0, $user->addresses()->destroy($address));
+ $this->assertEquals(array('New York'), $user->addresses->lists('city'));
+
+ $address->unsetEventDispatcher();
+ }
+
+ public function testEmbedsManyFindOrContains()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $address1 = $user->addresses()->save(new Address(array('city' => 'New York')));
+ $address2 = $user->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $address = $user->addresses()->find($address1->_id);
+ $this->assertEquals($address->city, $address1->city);
+
+ $address = $user->addresses()->find($address2->_id);
+ $this->assertEquals($address->city, $address2->city);
+
+ $this->assertTrue($user->addresses()->contains($address2->_id));
+ $this->assertFalse($user->addresses()->contains('123'));
+ }
+
+ public function testEmbedsManyEagerLoading()
+ {
+ $user1 = User::create(array('name' => 'John Doe'));
+ $user1->addresses()->save(new Address(array('city' => 'New York')));
+ $user1->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $user2 = User::create(array('name' => 'Jane Doe'));
+ $user2->addresses()->save(new Address(array('city' => 'Berlin')));
+ $user2->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $user = User::find($user1->id);
+ $relations = $user->getRelations();
+ $this->assertFalse(array_key_exists('addresses', $relations));
+
+ $user = User::with('addresses')->get()->first();
+ $relations = $user->getRelations();
+ $this->assertTrue(array_key_exists('addresses', $relations));
+ $this->assertEquals(2, $relations['addresses']->count());
+ }
+
+ public function testEmbedsManyDelete()
+ {
+ $user1 = User::create(array('name' => 'John Doe'));
+ $user1->addresses()->save(new Address(array('city' => 'New York')));
+ $user1->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $user2 = User::create(array('name' => 'Jane Doe'));
+ $user2->addresses()->save(new Address(array('city' => 'Berlin')));
+ $user2->addresses()->save(new Address(array('city' => 'Paris')));
+
+ $user1->addresses()->delete();
+ $this->assertEquals(0, $user1->addresses()->count());
+ $this->assertEquals(0, $user1->addresses->count());
+ $this->assertEquals(2, $user2->addresses()->count());
+ $this->assertEquals(2, $user2->addresses->count());
+
+ $user1 = User::find($user1->id);
+ $user2 = User::find($user2->id);
+ $this->assertEquals(0, $user1->addresses()->count());
+ $this->assertEquals(0, $user1->addresses->count());
+ $this->assertEquals(2, $user2->addresses()->count());
+ $this->assertEquals(2, $user2->addresses->count());
+ }
+
+ public function testEmbedsManyCollectionMethods()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $user->addresses()->save(new Address(array('city' => 'New York')));
+ $user->addresses()->save(new Address(array('city' => 'Paris')));
+ $user->addresses()->save(new Address(array('city' => 'Brussels')));
+
+ $this->assertEquals(array('New York', 'Paris', 'Brussels'), $user->addresses()->lists('city'));
+ $this->assertEquals(array('Brussels', 'New York', 'Paris'), $user->addresses()->sortBy('city')->lists('city'));
+ $this->assertEquals(array('Brussels', 'New York', 'Paris'), $user->addresses()->orderBy('city')->lists('city'));
+ $this->assertEquals(array('Paris', 'New York', 'Brussels'), $user->addresses()->orderBy('city', 'desc')->lists('city'));
+ }
+
+ public function testEmbedsOne()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $father = new User(array('name' => 'Mark Doe'));
+
+ $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($father), $father);
+ $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
+
+ $father = $user->father()->save($father);
+ $father->unsetEventDispatcher();
+
+ $this->assertNotNull($user->_father);
+ $this->assertEquals('Mark Doe', $user->father->name);
+ $this->assertInstanceOf('DateTime', $father->created_at);
+ $this->assertInstanceOf('DateTime', $father->updated_at);
+ $this->assertNotNull($father->_id);
+ $this->assertTrue(is_string($father->_id));
+
+ $raw = $father->getAttributes();
+ $this->assertInstanceOf('MongoId', $raw['_id']);
+
+ $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.updated: '.get_class($father), $father);
+ $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
+
+ $father->name = 'Tom Doe';
+ $user->father()->save($father);
+ $father->unsetEventDispatcher();
+
+ $this->assertNotNull($user->_father);
+ $this->assertEquals('Tom Doe', $user->father->name);
+
+ $father = new User(array('name' => 'Jim Doe'));
+
+ $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
+ $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($father), $father)->andReturn(true);
+ $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($father), $father);
+ $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
+
+ $father = $user->father()->save($father);
+ $father->unsetEventDispatcher();
+
+ $this->assertNotNull($user->_father);
+ $this->assertEquals('Jim Doe', $user->father->name);
+ }
+
+ public function testEmbedsOneDelete()
+ {
+ $user = User::create(array('name' => 'John Doe'));
+ $father = $user->father()->save(new User(array('name' => 'Mark Doe')));
+
+ $user->father()->delete();
+ $this->assertNull($user->_father);
+ $this->assertNull($user->father);
+ }
+
+}
diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php
index bb736b51e60dd7b25290b1d965ca783e39d9b0c4..f6afd46ab815913c7934a1e8b598f54a9ec51de3 100644
--- a/tests/RelationsTest.php
+++ b/tests/RelationsTest.php
@@ -303,400 +303,4 @@ class RelationsTest extends TestCase {
$this->assertInstanceOf('Client', $relations['imageable']);
}
- public function testEmbedsManySave()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'London'));
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($address), $address);
- $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($address), $address);
-
- $address = $user->addresses()->save($address);
- $address->unsetEventDispatcher();
-
- $this->assertNotNull($user->_addresses);
- $this->assertEquals(array('London'), $user->addresses->lists('city'));
- $this->assertInstanceOf('DateTime', $address->created_at);
- $this->assertInstanceOf('DateTime', $address->updated_at);
- $this->assertNotNull($address->_id);
- $this->assertTrue(is_string($address->_id));
-
- $raw = $address->getAttributes();
- $this->assertInstanceOf('MongoId', $raw['_id']);
-
- $address = $user->addresses()->save(new Address(array('city' => 'Paris')));
-
- $user = User::find($user->_id);
- $this->assertEquals(array('London', 'Paris'), $user->addresses->lists('city'));
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.updated: '.get_class($address), $address);
- $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($address), $address);
-
- $address->city = 'New York';
- $user->addresses()->save($address);
- $address->unsetEventDispatcher();
-
- $this->assertEquals(2, count($user->addresses));
- $this->assertEquals(2, count($user->addresses()->get()));
- $this->assertEquals(2, $user->addresses->count());
- $this->assertEquals(2, $user->addresses()->count());
- $this->assertEquals(array('London', 'New York'), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->_id);
- $this->assertEquals(array('London', 'New York'), $freshUser->addresses->lists('city'));
-
- $address = $user->addresses->first();
- $this->assertEquals('London', $address->city);
- $this->assertInstanceOf('DateTime', $address->created_at);
- $this->assertInstanceOf('DateTime', $address->updated_at);
- $this->assertInstanceOf('User', $address->user);
- $this->assertEmpty($address->relationsToArray()); // prevent infinite loop
-
- $user = User::find($user->_id);
- $user->addresses()->save(new Address(array('city' => 'Bruxelles')));
- $this->assertEquals(array('London', 'New York', 'Bruxelles'), $user->addresses->lists('city'));
- $address = $user->addresses[1];
- $address->city = "Manhattan";
- $user->addresses()->save($address);
- $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->_id);
- $this->assertEquals(array('London', 'Manhattan', 'Bruxelles'), $freshUser->addresses->lists('city'));
- }
-
- public function testEmbedsManyAssociate()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'London'));
-
- $address = $user->addresses()->associate($address);
- $this->assertNotNull($user->_addresses);
- $this->assertEquals(array('London'), $user->addresses->lists('city'));
- $this->assertNotNull($address->_id);
-
- $freshUser = User::find($user->_id);
- $this->assertEquals(array(), $freshUser->addresses->lists('city'));
-
- $address->city = 'Londinium';
- $user->addresses()->associate($address);
- $this->assertEquals(array('Londinium'), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->_id);
- $this->assertEquals(array(), $freshUser->addresses->lists('city'));
- }
-
- public function testEmbedsManySaveMany()
- {
- $user = User::create(array('name' => 'John Doe'));
- $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol'))));
- $this->assertEquals(array('London', 'Bristol'), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->id);
- $this->assertEquals(array('London', 'Bristol'), $freshUser->addresses->lists('city'));
- }
-
- public function testEmbedsManyDuplicate()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'London'));
- $user->addresses()->save($address);
- $user->addresses()->save($address);
- $this->assertEquals(1, $user->addresses->count());
- $this->assertEquals(array('London'), $user->addresses->lists('city'));
-
- $user = User::find($user->id);
- $this->assertEquals(1, $user->addresses->count());
-
- $address->city = 'Paris';
- $user->addresses()->save($address);
- $this->assertEquals(1, $user->addresses->count());
- $this->assertEquals(array('Paris'), $user->addresses->lists('city'));
-
- $user->addresses()->create(array('_id' => $address->_id, 'city' => 'Bruxelles'));
- $this->assertEquals(1, $user->addresses->count());
- $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
- }
-
- public function testEmbedsManyCreate()
- {
- $user = User::create(array());
- $address = $user->addresses()->create(array('city' => 'Bruxelles'));
- $this->assertInstanceOf('Address', $address);
- $this->assertTrue(is_string($address->_id));
- $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
-
- $raw = $address->getAttributes();
- $this->assertInstanceOf('MongoId', $raw['_id']);
-
- $freshUser = User::find($user->id);
- $this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
-
- $user = User::create(array());
- $address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
- $this->assertTrue(is_string($address->_id));
-
- $raw = $address->getAttributes();
- $this->assertInstanceOf('MongoId', $raw['_id']);
- }
-
- public function testEmbedsManyCreateMany()
- {
- $user = User::create(array());
- list($bruxelles, $paris) = $user->addresses()->createMany(array(array('city' => 'Bruxelles'), array('city' => 'Paris')));
- $this->assertInstanceOf('Address', $bruxelles);
- $this->assertEquals('Bruxelles', $bruxelles->city);
- $this->assertEquals(array('Bruxelles', 'Paris'), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->id);
- $this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
- }
-
- public function testEmbedsManyDestroy()
- {
- $user = User::create(array('name' => 'John Doe'));
- $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
-
- $address = $user->addresses->first();
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.deleting: '.get_class($address), Mockery::mustBe($address))->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.deleted: '.get_class($address), Mockery::mustBe($address));
-
- $user->addresses()->destroy($address->_id);
- $this->assertEquals(array('Bristol', 'Bruxelles'), $user->addresses->lists('city'));
-
- $address->unsetEventDispatcher();
-
- $address = $user->addresses->first();
- $user->addresses()->destroy($address);
- $this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
-
- $user->addresses()->create(array('city' => 'Paris'));
- $user->addresses()->create(array('city' => 'San Francisco'));
-
- $freshUser = User::find($user->id);
- $this->assertEquals(array('Bruxelles', 'Paris', 'San Francisco'), $freshUser->addresses->lists('city'));
-
- $ids = $user->addresses->lists('_id');
- $user->addresses()->destroy($ids);
- $this->assertEquals(array(), $user->addresses->lists('city'));
-
- $freshUser = User::find($user->id);
- $this->assertEquals(array(), $freshUser->addresses->lists('city'));
-
- list($london, $bristol, $bruxelles) = $user->addresses()->saveMany(array(new Address(array('city' => 'London')), new Address(array('city' => 'Bristol')), new Address(array('city' => 'Bruxelles'))));
- $user->addresses()->destroy(array($london, $bruxelles));
- $this->assertEquals(array('Bristol'), $user->addresses->lists('city'));
- }
-
- public function testEmbedsManyDissociate()
- {
- $user = User::create(array());
- $cordoba = $user->addresses()->create(array('city' => 'Cordoba'));
-
- $user->addresses()->dissociate($cordoba->id);
-
- $freshUser = User::find($user->id);
- $this->assertEquals(0, $user->addresses->count());
- $this->assertEquals(1, $freshUser->addresses->count());
- }
-
- public function testEmbedsManyAliases()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'London'));
-
- $address = $user->addresses()->attach($address);
- $this->assertEquals(array('London'), $user->addresses->lists('city'));
-
- $user->addresses()->detach($address);
- $this->assertEquals(array(), $user->addresses->lists('city'));
- }
-
- public function testEmbedsManyCreatingEventReturnsFalse()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'London'));
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($address), $address)->andReturn(false);
-
- $this->assertFalse($user->addresses()->save($address));
- $address->unsetEventDispatcher();
- }
-
- public function testEmbedsManySavingEventReturnsFalse()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'Paris'));
- $address->exists = true;
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(false);
-
- $this->assertFalse($user->addresses()->save($address));
- $address->unsetEventDispatcher();
- }
-
- public function testEmbedsManyUpdatingEventReturnsFalse()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address = new Address(array('city' => 'New York'));
- $user->addresses()->save($address);
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($address), $address)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($address), $address)->andReturn(false);
-
- $address->city = 'Warsaw';
-
- $this->assertFalse($user->addresses()->save($address));
- $address->unsetEventDispatcher();
- }
-
- public function testEmbedsManyDeletingEventReturnsFalse()
- {
- $user = User::create(array('name' => 'John Doe'));
- $user->addresses()->save(new Address(array('city' => 'New York')));
-
- $address = $user->addresses->first();
-
- $address->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.deleting: '.get_class($address), Mockery::mustBe($address))->andReturn(false);
-
- $this->assertEquals(0, $user->addresses()->destroy($address));
- $this->assertEquals(array('New York'), $user->addresses->lists('city'));
-
- $address->unsetEventDispatcher();
- }
-
- public function testEmbedsManyFindOrContains()
- {
- $user = User::create(array('name' => 'John Doe'));
- $address1 = $user->addresses()->save(new Address(array('city' => 'New York')));
- $address2 = $user->addresses()->save(new Address(array('city' => 'Paris')));
-
- $address = $user->addresses()->find($address1->_id);
- $this->assertEquals($address->city, $address1->city);
-
- $address = $user->addresses()->find($address2->_id);
- $this->assertEquals($address->city, $address2->city);
-
- $this->assertTrue($user->addresses()->contains($address2->_id));
- $this->assertFalse($user->addresses()->contains('123'));
- }
-
- public function testEmbedsManyEagerLoading()
- {
- $user1 = User::create(array('name' => 'John Doe'));
- $user1->addresses()->save(new Address(array('city' => 'New York')));
- $user1->addresses()->save(new Address(array('city' => 'Paris')));
-
- $user2 = User::create(array('name' => 'Jane Doe'));
- $user2->addresses()->save(new Address(array('city' => 'Berlin')));
- $user2->addresses()->save(new Address(array('city' => 'Paris')));
-
- $user = User::find($user1->id);
- $relations = $user->getRelations();
- $this->assertFalse(array_key_exists('addresses', $relations));
-
- $user = User::with('addresses')->get()->first();
- $relations = $user->getRelations();
- $this->assertTrue(array_key_exists('addresses', $relations));
- $this->assertEquals(2, $relations['addresses']->count());
- }
-
- public function testEmbedsManyDelete()
- {
- $user1 = User::create(array('name' => 'John Doe'));
- $user1->addresses()->save(new Address(array('city' => 'New York')));
- $user1->addresses()->save(new Address(array('city' => 'Paris')));
-
- $user2 = User::create(array('name' => 'Jane Doe'));
- $user2->addresses()->save(new Address(array('city' => 'Berlin')));
- $user2->addresses()->save(new Address(array('city' => 'Paris')));
-
- $user1->addresses()->delete();
- $this->assertEquals(0, $user1->addresses()->count());
- $this->assertEquals(0, $user1->addresses->count());
- $this->assertEquals(2, $user2->addresses()->count());
- $this->assertEquals(2, $user2->addresses->count());
-
- $user1 = User::find($user1->id);
- $user2 = User::find($user2->id);
- $this->assertEquals(0, $user1->addresses()->count());
- $this->assertEquals(0, $user1->addresses->count());
- $this->assertEquals(2, $user2->addresses()->count());
- $this->assertEquals(2, $user2->addresses->count());
- }
-
- public function testEmbedsOne()
- {
- $user = User::create(array('name' => 'John Doe'));
- $father = new User(array('name' => 'Mark Doe'));
-
- $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($father), $father);
- $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
-
- $father = $user->father()->save($father);
- $father->unsetEventDispatcher();
-
- $this->assertNotNull($user->_father);
- $this->assertEquals('Mark Doe', $user->father->name);
- $this->assertInstanceOf('DateTime', $father->created_at);
- $this->assertInstanceOf('DateTime', $father->updated_at);
- $this->assertNotNull($father->_id);
- $this->assertTrue(is_string($father->_id));
-
- $raw = $father->getAttributes();
- $this->assertInstanceOf('MongoId', $raw['_id']);
-
- $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.updating: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.updated: '.get_class($father), $father);
- $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
-
- $father->name = 'Tom Doe';
- $user->father()->save($father);
- $father->unsetEventDispatcher();
-
- $this->assertNotNull($user->_father);
- $this->assertEquals('Tom Doe', $user->father->name);
-
- $father = new User(array('name' => 'Jim Doe'));
-
- $father->setEventDispatcher($events = Mockery::mock('Illuminate\Events\Dispatcher'));
- $events->shouldReceive('until')->once()->with('eloquent.saving: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('until')->once()->with('eloquent.creating: '.get_class($father), $father)->andReturn(true);
- $events->shouldReceive('fire')->once()->with('eloquent.created: '.get_class($father), $father);
- $events->shouldReceive('fire')->once()->with('eloquent.saved: '.get_class($father), $father);
-
- $father = $user->father()->save($father);
- $father->unsetEventDispatcher();
-
- $this->assertNotNull($user->_father);
- $this->assertEquals('Jim Doe', $user->father->name);
- }
-
- public function testEmbedsOneDelete()
- {
- $user = User::create(array('name' => 'John Doe'));
- $father = $user->father()->save(new User(array('name' => 'Mark Doe')));
-
- $user->father()->delete();
- $this->assertNull($user->_father);
- $this->assertNull($user->father);
- }
-
}