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.5] Better BadMethodCallException message when dealing with Eloquent #20196

Merged
merged 1 commit into from
Jul 20, 2017
Merged

[5.5] Better BadMethodCallException message when dealing with Eloquent #20196

merged 1 commit into from
Jul 20, 2017

Conversation

ThibaudDauce
Copy link
Contributor

I think for newcomers in Laravel and PHP beginners, the message "Call to undefined method Illuminate\Database\Query\Builder::badMethod()" could be confusing.

Wrapping the call to the builder and throwing a new BadMethodCallException with the correct message ("Call to undefined method App\User::badMethod()") could be helpful.

I chose to assert the entire string in the test but I can change it to just asserting that Illuminate\Tests\Database\EloquentModelStub is contains in the message.

@deleugpn
Copy link
Contributor

I think even experienced developers can benefit from this

@@ -453,6 +453,28 @@ public function testFromDateTime()
$this->assertEquals('2015-04-17 22:59:01', $model->fromDateTime($value));
}

public function testBadMethodCallException()
Copy link
Contributor

Choose a reason for hiding this comment

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

Use this docblock instead:

/**
 * @expectedException \BadMethodCallException
 * @expectedExceptionMessage Call to undefined method Illuminate\Tests\Database\EloquentModelStub::badMethod()
 */

return $this->newQuery()->$method(...$parameters);
try {
return $this->newQuery()->$method(...$parameters);
} catch (\BadMethodCallException $e) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Import \BadMethodCallException

@taylorotwell taylorotwell merged commit 8003dfe into laravel:master Jul 20, 2017
@BrandonSurowiec
Copy link
Contributor

@ThibaudDauce I was playing around with this idea earlier last week. Your implementation is brilliant. Way to go!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants