Skip to content

Commit

Permalink
Avoid model getting overwritten when calling with()
Browse files Browse the repository at this point in the history
  • Loading branch information
tswestendorp committed Jul 16, 2018
1 parent d92e351 commit fb91b1a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/Repository/Eloquent/BaseRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function __construct(ApplicationContract $app, Collection $collection, $m
$this->app = $app;
$this->criteria = $collection;

if (null !== $model && true === $model instanceof Model) {
if (null !== $model && true === \is_object($model)) {
$this->model = $model;
}

Expand Down Expand Up @@ -177,7 +177,9 @@ public function with($relations)
{
$this->initiateModel();

return parent::with($relations);
$this->model->with($relations);

return $this;
}

public function withCount($relations)
Expand Down
18 changes: 18 additions & 0 deletions tests/BaseRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,24 @@ public function testResetModelCreatesNewModel()
$this->assertNotSame($model, $property->getValue($repository));
}

public function testWithDoesntOverwrite()
{
$repository = new RepositoryStub(
$this->application,
new \Illuminate\Support\Collection(),
$model = m::mock('alias:ModelStub')
);

$model->shouldReceive('with')->andReturn(m::mock(\Illuminate\Database\Eloquent\Builder::class));

$reflectionClass = $this->repositoryReflector();
$property = $reflectionClass->getProperty('model');
$property->setAccessible(true);

$this->assertInstanceOf(RepositoryStub::class, $repository->with(['relation']));
$this->assertSame($model, $property->getValue($repository));
}

public function providerTestConstructor(): array
{
return [
Expand Down

0 comments on commit fb91b1a

Please sign in to comment.