Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.2] fix #13527 by setting the model on ModelNotFoundException #13537

Merged
merged 1 commit into from
May 12, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public function firstOrFail($columns = ['*'])
return $model;
}

throw new ModelNotFoundException;
throw (new ModelNotFoundException)->setModel(get_class($this->parent));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public function firstOrFail($columns = ['*'])
return $model;
}

throw new ModelNotFoundException;
throw (new ModelNotFoundException)->setModel(get_class($this->parent));
}

/**
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,38 @@ public function testCreateMethodCreatesNewModelAndInsertsAttachmentRecord()
$this->assertEquals($model, $relation->create(['attributes'], ['joining']));
}

public function testFindOrFailThrowsException()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['find'], $this->getRelationArguments());
$relation->expects($this->once())->method('find')->with('foo')->will($this->returnValue(null));

$this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid these leading slashes when we're not in a namespace.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, will do.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries. Thanks for all the great PRs. ;)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Glad to help mate :)


try {
$relation->findOrFail('foo');
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
$this->assertNotEmpty($e->getModel());

throw $e;
}
}

public function testFirstOrFailThrowsException()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['first'], $this->getRelationArguments());
$relation->expects($this->once())->method('first')->with(['id' => 'foo'])->will($this->returnValue(null));

$this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);

try {
$relation->firstOrFail(['id' => 'foo']);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
$this->assertNotEmpty($e->getModel());

throw $e;
}
}

public function testFindOrNewMethodFindsModel()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\BelongsToMany', ['find'], $this->getRelationArguments());
Expand Down
32 changes: 32 additions & 0 deletions tests/Database/DatabaseEloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,38 @@ public function testFirstMethod()
$this->assertEquals('first', $relation->first());
}

public function testFindOrFailThrowsException()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\HasManyThrough', ['find'], $this->getRelationArguments());
$relation->expects($this->once())->method('find')->with('foo')->will($this->returnValue(null));

$this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);

try {
$relation->findOrFail('foo');
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
$this->assertNotEmpty($e->getModel());

throw $e;
}
}

public function testFirstOrFailThrowsException()
{
$relation = $this->getMock('Illuminate\Database\Eloquent\Relations\HasManyThrough', ['first'], $this->getRelationArguments());
$relation->expects($this->once())->method('first')->with(['id' => 'foo'])->will($this->returnValue(null));

$this->setExpectedException(\Illuminate\Database\Eloquent\ModelNotFoundException::class);

try {
$relation->firstOrFail(['id' => 'foo']);
} catch (\Illuminate\Database\Eloquent\ModelNotFoundException $e) {
$this->assertNotEmpty($e->getModel());

throw $e;
}
}

public function testFindMethod()
{
$relation = m::mock('Illuminate\Database\Eloquent\Relations\HasManyThrough[first]', $this->getRelationArguments());
Expand Down