Skip to content

Commit a54ef6d

Browse files
committed
Merge branch 'master' into develop
2 parents e647782 + 9c8bf64 commit a54ef6d

File tree

6 files changed

+56
-40
lines changed

6 files changed

+56
-40
lines changed

src/Jenssegers/Mongodb/Model.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ public function getIdAttribute($value)
5757
*/
5858
protected function embedsMany($related, $localKey = null, $foreignKey = null, $relation = null)
5959
{
60-
if (is_null($localKey))
61-
{
62-
$localKey = snake_case(str_plural($related)) . '_ids';
63-
}
64-
65-
if (is_null($foreignKey))
66-
{
67-
$foreignKey = snake_case(class_basename($this));
68-
}
69-
7060
// If no relation name was given, we will use this debug backtrace to extract
7161
// the calling method's name and use that as the relationship name as most
7262
// of the time this will be what we desire to use for the relatinoships.
@@ -77,6 +67,16 @@ protected function embedsMany($related, $localKey = null, $foreignKey = null, $r
7767
$relation = $caller['function'];
7868
}
7969

70+
if (is_null($localKey))
71+
{
72+
$localKey = '_' . $relation;
73+
}
74+
75+
if (is_null($foreignKey))
76+
{
77+
$foreignKey = snake_case(class_basename($this));
78+
}
79+
8080
$query = $this->newQuery();
8181

8282
$instance = new $related;

src/Jenssegers/Mongodb/Relations/EmbedsMany.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public function get()
141141
public function save(Model $model)
142142
{
143143
// Insert a new document.
144-
if (!$model->exists)
144+
if ( ! $model->exists)
145145
{
146146
return $this->performInsert($model);
147147
}
@@ -162,7 +162,7 @@ public function save(Model $model)
162162
protected function performInsert(Model $model)
163163
{
164164
// Create a new key.
165-
if (!isset($model['_id']))
165+
if ( ! $model->getAttribute('_id'))
166166
{
167167
$model->setAttribute('_id', new MongoId);
168168
}
@@ -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/ModelTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ public function testAll()
104104
$all = User::all();
105105

106106
$this->assertEquals(2, count($all));
107-
$this->assertEquals('John Doe', $all[0]->name);
108-
$this->assertEquals('Jane Doe', $all[1]->name);
107+
$this->assertContains('John Doe', $all->lists('name'));
108+
$this->assertContains('Jane Doe', $all->lists('name'));
109109
}
110110

111111
public function testFind()

tests/QueryBuilderTest.php

Lines changed: 13 additions & 11 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()
@@ -312,9 +311,9 @@ public function testTake()
312311
array('name' => 'spoon', 'type' => 'round', 'amount' => 14)
313312
));
314313

315-
$items = DB::collection('items')->take(2)->get();
314+
$items = DB::collection('items')->orderBy('name')->take(2)->get();
316315
$this->assertEquals(2, count($items));
317-
$this->assertEquals('knife', $items[0]['name']);
316+
$this->assertEquals('fork', $items[0]['name']);
318317
}
319318

320319
public function testSkip()
@@ -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
}
@@ -352,8 +351,9 @@ public function testList()
352351
));
353352

354353
$list = DB::collection('items')->lists('name');
354+
sort($list);
355355
$this->assertEquals(4, count($list));
356-
$this->assertEquals(array('knife', 'fork', 'spoon', 'spoon'), $list);
356+
$this->assertEquals(array('fork', 'knife', 'spoon', 'spoon'), $list);
357357

358358
$list = DB::collection('items')->lists('type', 'name');
359359
$this->assertEquals(3, count($list));
@@ -470,7 +470,9 @@ public function testOperators()
470470

471471
$results = DB::collection('users')->where('age', 'exists', true)->get();
472472
$this->assertEquals(2, count($results));
473-
$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);
474476

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

tests/QueryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,18 +70,18 @@ public function testLike()
7070

7171
public function testSelect()
7272
{
73-
$user = User::select('name')->first();
73+
$user = User::where('name', 'John Doe')->select('name')->first();
7474

7575
$this->assertEquals('John Doe', $user->name);
7676
$this->assertEquals(null, $user->age);
7777

78-
$user = User::select('name', 'title')->first();
78+
$user = User::where('name', 'John Doe')->select('name', 'title')->first();
7979

8080
$this->assertEquals('John Doe', $user->name);
8181
$this->assertEquals('admin', $user->title);
8282
$this->assertEquals(null, $user->age);
8383

84-
$user = User::get(array('name'))->first();
84+
$user = User::where('name', 'John Doe')->get(array('name'))->first();
8585

8686
$this->assertEquals('John Doe', $user->name);
8787
$this->assertEquals(null, $user->age);

tests/RelationsTest.php

Lines changed: 25 additions & 6 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

@@ -289,6 +289,7 @@ public function testEmbedsManySave()
289289
$address = new Address(array('city' => 'London'));
290290

291291
$address = $user->addresses()->save($address);
292+
$this->assertNotNull($user->_addresses);
292293
$this->assertEquals(array('London'), $user->addresses->lists('city'));
293294
$this->assertInstanceOf('DateTime', $address->created_at);
294295
$this->assertInstanceOf('DateTime', $address->updated_at);
@@ -340,12 +341,30 @@ public function testEmbedsManySaveMany()
340341

341342
public function testEmbedsManyCreate()
342343
{
343-
$user = User::create(array('name' => 'John Doe'));
344-
$user->addresses()->create(array('city' => 'Bruxelles'));
344+
$user = User::create(array());
345+
$address = $user->addresses()->create(array('city' => 'Bruxelles'));
346+
$this->assertInstanceOf('Address', $address);
347+
$this->assertInstanceOf('MongoID', $address->_id);
345348
$this->assertEquals(array('Bruxelles'), $user->addresses->lists('city'));
346349

347350
$freshUser = User::find($user->id);
348351
$this->assertEquals(array('Bruxelles'), $freshUser->addresses->lists('city'));
352+
353+
$user = User::create(array());
354+
$address = $user->addresses()->create(array('_id' => '', 'city' => 'Bruxelles'));
355+
$this->assertInstanceOf('MongoID', $address->_id);
356+
}
357+
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'));
349368
}
350369

351370
public function testEmbedsManyDestroy()

0 commit comments

Comments
 (0)