Skip to content

Commit 9c8bf64

Browse files
committed
Merge pull request #146 from neoxia/master-test-and-refactor-create-many-embedded
Test and refactor EmbedsMany::createMany
2 parents 652ef4b + 1c07ca1 commit 9c8bf64

File tree

3 files changed

+26
-18
lines changed

3 files changed

+26
-18
lines changed

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,12 +265,7 @@ public function create(array $attributes)
265265
*/
266266
public function createMany(array $records)
267267
{
268-
$instances = array();
269-
270-
foreach ($records as $record)
271-
{
272-
$instances[] = $this->create($record);
273-
}
268+
$instances = array_map(array($this, 'create'), $records);
274269

275270
return $instances;
276271
}

tests/QueryBuilderTest.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,7 @@ public function testBatchInsert()
7878

7979
$users = DB::collection('users')->get();
8080
$this->assertEquals(2, count($users));
81-
82-
$user = $users[0];
83-
$this->assertEquals('Jane Doe', $user['name']);
84-
$this->assertTrue(is_array($user['tags']));
81+
$this->assertTrue(is_array($users[0]['tags']));
8582
}
8683

8784
public function testFind()
@@ -118,8 +115,10 @@ public function testUpdate()
118115
DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
119116
$users = DB::collection('users')->get();
120117

121-
$this->assertEquals(20, $users[0]['age']);
122-
$this->assertEquals(100, $users[1]['age']);
118+
$john = DB::collection('users')->where('name', 'John Doe')->first();
119+
$jane = DB::collection('users')->where('name', 'Jane Doe')->first();
120+
$this->assertEquals(100, $john['age']);
121+
$this->assertEquals(20, $jane['age']);
123122
}
124123

125124
public function testDelete()
@@ -326,7 +325,7 @@ public function testSkip()
326325
array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
327326
));
328327

329-
$items = DB::collection('items')->skip(2)->get();
328+
$items = DB::collection('items')->orderBy('name')->skip(2)->get();
330329
$this->assertEquals(2, count($items));
331330
$this->assertEquals('spoon', $items[0]['name']);
332331
}
@@ -471,7 +470,9 @@ public function testOperators()
471470

472471
$results = DB::collection('users')->where('age', 'exists', true)->get();
473472
$this->assertEquals(2, count($results));
474-
$this->assertEquals('John Doe', $results[0]['name']);
473+
$resultsNames = array($results[0]['name'], $results[1]['name']);
474+
$this->assertContains('John Doe', $resultsNames);
475+
$this->assertContains('Robert Roe', $resultsNames);
475476

476477
$results = DB::collection('users')->where('age', 'exists', false)->get();
477478
$this->assertEquals(1, count($results));

tests/RelationsTest.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,13 +146,13 @@ public function testBelongsToMany()
146146
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
147147
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
148148

149-
$clients = $client->getRelation('users');
150-
$users = $user->getRelation('clients');
149+
$users = $client->getRelation('users');
150+
$clients = $user->getRelation('clients');
151151

152152
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
153153
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
154-
$this->assertInstanceOf('Client', $users[0]);
155-
$this->assertInstanceOf('User', $clients[0]);
154+
$this->assertInstanceOf('Client', $clients[0]);
155+
$this->assertInstanceOf('User', $users[0]);
156156
$this->assertCount(2, $user->clients);
157157
$this->assertCount(1, $client->users);
158158

@@ -355,6 +355,18 @@ public function testEmbedsManyCreate()
355355
$this->assertInstanceOf('MongoID', $address->_id);
356356
}
357357

358+
public function testEmbedsManyCreateMany()
359+
{
360+
$user = User::create(array());
361+
list($bruxelles, $paris) = $user->addresses()->createMany(array(array('city' => 'Bruxelles'), array('city' => 'Paris')));
362+
$this->assertInstanceOf('Address', $bruxelles);
363+
$this->assertEquals('Bruxelles', $bruxelles->city);
364+
$this->assertEquals(array('Bruxelles', 'Paris'), $user->addresses->lists('city'));
365+
366+
$freshUser = User::find($user->id);
367+
$this->assertEquals(array('Bruxelles', 'Paris'), $freshUser->addresses->lists('city'));
368+
}
369+
358370
public function testEmbedsManyDestroy()
359371
{
360372
$user = User::create(array('name' => 'John Doe'));

0 commit comments

Comments
 (0)