Commit 89667a59 authored by Stephan de Souza's avatar Stephan de Souza

Fix refresh() on EmbedsOne

parent 279b18c7
......@@ -28,6 +28,17 @@ class EmbedsOne extends EmbedsOneOrMany
return $this->toModel($this->getEmbedded());
}
/**
* @inheritdoc
*/
public function getEager()
{
$eager = $this->get();
// EmbedsOne only brings one result, Eager needs a collection!
return $this->toCollection([$eager]);
}
/**
* Save a new model and attach it to the parent model.
* @param Model $model
......
......@@ -614,6 +614,36 @@ class EmbeddedRelationsTest extends TestCase
$this->assertNull($user->father);
}
public function testEmbedsOneRefresh()
{
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$user->father()->associate($father);
$user->save();
$user->refresh();
$this->assertNotNull($user->father);
$this->assertEquals('Mark Doe', $user->father->name);
}
public function testEmbedsOneEmptyRefresh()
{
$user = User::create(['name' => 'John Doe']);
$father = new User(['name' => 'Mark Doe']);
$user->father()->associate($father);
$user->save();
$user->father()->dissociate();
$user->save();
$user->refresh();
$this->assertNull($user->father);
}
public function testEmbedsManyToArray()
{
/** @var User $user */
......@@ -627,6 +657,22 @@ class EmbeddedRelationsTest extends TestCase
$this->assertIsArray($array['addresses']);
}
public function testEmbedsManyRefresh()
{
/** @var User $user */
$user = User::create(['name' => 'John Doe']);
$user->addresses()->save(new Address(['city' => 'New York']));
$user->addresses()->save(new Address(['city' => 'Paris']));
$user->addresses()->save(new Address(['city' => 'Brussels']));
$user->refresh();
$array = $user->toArray();
$this->assertArrayHasKey('addresses', $array);
$this->assertIsArray($array['addresses']);
}
public function testEmbeddedSave()
{
/** @var User $user */
......
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