diff --git a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php index 10e1848a9..67d7eaada 100644 --- a/src/Jenssegers/Mongodb/Relations/EmbedsMany.php +++ b/src/Jenssegers/Mongodb/Relations/EmbedsMany.php @@ -265,12 +265,7 @@ public function create(array $attributes) */ public function createMany(array $records) { - $instances = array(); - - foreach ($records as $record) - { - $instances[] = $this->create($record); - } + $instances = array_map(array($this, 'create'), $records); return $instances; } diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index 279f72ebc..7588886d0 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -78,10 +78,7 @@ public function testBatchInsert() $users = DB::collection('users')->get(); $this->assertEquals(2, count($users)); - - $user = $users[0]; - $this->assertEquals('Jane Doe', $user['name']); - $this->assertTrue(is_array($user['tags'])); + $this->assertTrue(is_array($users[0]['tags'])); } public function testFind() @@ -118,8 +115,10 @@ public function testUpdate() DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100)); $users = DB::collection('users')->get(); - $this->assertEquals(20, $users[0]['age']); - $this->assertEquals(100, $users[1]['age']); + $john = DB::collection('users')->where('name', 'John Doe')->first(); + $jane = DB::collection('users')->where('name', 'Jane Doe')->first(); + $this->assertEquals(100, $john['age']); + $this->assertEquals(20, $jane['age']); } public function testDelete() @@ -326,7 +325,7 @@ public function testSkip() array('name' => 'spoon', 'type' => 'round', 'amount' => 14) )); - $items = DB::collection('items')->skip(2)->get(); + $items = DB::collection('items')->orderBy('name')->skip(2)->get(); $this->assertEquals(2, count($items)); $this->assertEquals('spoon', $items[0]['name']); } @@ -471,7 +470,9 @@ public function testOperators() $results = DB::collection('users')->where('age', 'exists', true)->get(); $this->assertEquals(2, count($results)); - $this->assertEquals('John Doe', $results[0]['name']); + $resultsNames = array($results[0]['name'], $results[1]['name']); + $this->assertContains('John Doe', $resultsNames); + $this->assertContains('Robert Roe', $resultsNames); $results = DB::collection('users')->where('age', 'exists', false)->get(); $this->assertEquals(1, count($results)); diff --git a/tests/RelationsTest.php b/tests/RelationsTest.php index c4fe9da92..32ac42673 100644 --- a/tests/RelationsTest.php +++ b/tests/RelationsTest.php @@ -146,13 +146,13 @@ public function testBelongsToMany() $this->assertTrue(array_key_exists('user_ids', $client->getAttributes())); $this->assertTrue(array_key_exists('client_ids', $user->getAttributes())); - $clients = $client->getRelation('users'); - $users = $user->getRelation('clients'); + $users = $client->getRelation('users'); + $clients = $user->getRelation('clients'); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); - $this->assertInstanceOf('Client', $users[0]); - $this->assertInstanceOf('User', $clients[0]); + $this->assertInstanceOf('Client', $clients[0]); + $this->assertInstanceOf('User', $users[0]); $this->assertCount(2, $user->clients); $this->assertCount(1, $client->users); @@ -355,6 +355,18 @@ public function testEmbedsManyCreate() $this->assertInstanceOf('MongoID', $address->_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'));