Commit 9c8bf642 authored by Jens Segers's avatar Jens Segers

Merge pull request #146 from neoxia/master-test-and-refactor-create-many-embedded

Test and refactor EmbedsMany::createMany
parents 652ef4b0 1c07ca16
...@@ -265,12 +265,7 @@ class EmbedsMany extends Relation { ...@@ -265,12 +265,7 @@ class EmbedsMany extends Relation {
*/ */
public function createMany(array $records) public function createMany(array $records)
{ {
$instances = array(); $instances = array_map(array($this, 'create'), $records);
foreach ($records as $record)
{
$instances[] = $this->create($record);
}
return $instances; return $instances;
} }
......
...@@ -78,10 +78,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -78,10 +78,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$users = DB::collection('users')->get(); $users = DB::collection('users')->get();
$this->assertEquals(2, count($users)); $this->assertEquals(2, count($users));
$this->assertTrue(is_array($users[0]['tags']));
$user = $users[0];
$this->assertEquals('Jane Doe', $user['name']);
$this->assertTrue(is_array($user['tags']));
} }
public function testFind() public function testFind()
...@@ -118,8 +115,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -118,8 +115,10 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100)); DB::collection('users')->where('name', 'John Doe')->update(array('age' => 100));
$users = DB::collection('users')->get(); $users = DB::collection('users')->get();
$this->assertEquals(20, $users[0]['age']); $john = DB::collection('users')->where('name', 'John Doe')->first();
$this->assertEquals(100, $users[1]['age']); $jane = DB::collection('users')->where('name', 'Jane Doe')->first();
$this->assertEquals(100, $john['age']);
$this->assertEquals(20, $jane['age']);
} }
public function testDelete() public function testDelete()
...@@ -326,7 +325,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -326,7 +325,7 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
array('name' => 'spoon', 'type' => 'round', 'amount' => 14) 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(2, count($items));
$this->assertEquals('spoon', $items[0]['name']); $this->assertEquals('spoon', $items[0]['name']);
} }
...@@ -471,7 +470,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase { ...@@ -471,7 +470,9 @@ class QueryBuilderTest extends PHPUnit_Framework_TestCase {
$results = DB::collection('users')->where('age', 'exists', true)->get(); $results = DB::collection('users')->where('age', 'exists', true)->get();
$this->assertEquals(2, count($results)); $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(); $results = DB::collection('users')->where('age', 'exists', false)->get();
$this->assertEquals(1, count($results)); $this->assertEquals(1, count($results));
......
...@@ -146,13 +146,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -146,13 +146,13 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertTrue(array_key_exists('user_ids', $client->getAttributes())); $this->assertTrue(array_key_exists('user_ids', $client->getAttributes()));
$this->assertTrue(array_key_exists('client_ids', $user->getAttributes())); $this->assertTrue(array_key_exists('client_ids', $user->getAttributes()));
$clients = $client->getRelation('users'); $users = $client->getRelation('users');
$users = $user->getRelation('clients'); $clients = $user->getRelation('clients');
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $users);
$this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients); $this->assertInstanceOf('Illuminate\Database\Eloquent\Collection', $clients);
$this->assertInstanceOf('Client', $users[0]); $this->assertInstanceOf('Client', $clients[0]);
$this->assertInstanceOf('User', $clients[0]); $this->assertInstanceOf('User', $users[0]);
$this->assertCount(2, $user->clients); $this->assertCount(2, $user->clients);
$this->assertCount(1, $client->users); $this->assertCount(1, $client->users);
...@@ -355,6 +355,18 @@ class RelationsTest extends PHPUnit_Framework_TestCase { ...@@ -355,6 +355,18 @@ class RelationsTest extends PHPUnit_Framework_TestCase {
$this->assertInstanceOf('MongoID', $address->_id); $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() public function testEmbedsManyDestroy()
{ {
$user = User::create(array('name' => 'John Doe')); $user = User::create(array('name' => 'John Doe'));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment