thephpleague / factory-muffin

Enables the rapid creation of objects for testing
https://factory-muffin.thephpleague.com/
MIT License
531 stars 72 forks source link

[Laravel5] deleteSaved throws Illuminate\Container\BindingResolutionException #383

Closed bcdennis closed 9 years ago

bcdennis commented 9 years ago

public static function tearDownAfterClass() { static::$muffin->deleteSaved(); }

Fatal error: Uncaught exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.' in /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 785

Illuminate\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 785

Call Stack: 1.6720 17856736 1. Illuminate\Foundation\Bootstrap\HandleExceptions->handleException() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:0 1.6720 17856784 2. Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:73 1.6720 17856832 3. Illuminate\Foundation\Application->make() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:151 1.6720 17856968 4. Illuminate\Container\Container->make() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:572 1.6721 17856968 5. Illuminate\Container\Container->build() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php:656

Fatal error: Uncaught exception 'Illuminate\Container\BindingResolutionException' with message 'Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.' in /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 785

Illuminate\Container\BindingResolutionException: Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable. in /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php on line 785

Call Stack: 1.6827 17881624 1. Illuminate\Foundation\Bootstrap\HandleExceptions->handleShutdown() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:0 1.6891 17915192 2. Illuminate\Foundation\Bootstrap\HandleExceptions->handleException() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:116 1.6891 17915192 3. Illuminate\Foundation\Bootstrap\HandleExceptions->getExceptionHandler() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:73 1.6891 17915240 4. Illuminate\Foundation\Application->make() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:151 1.6891 17915376 5. Illuminate\Container\Container->make() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Foundation/Application.php:572 1.6891 17915376 6. Illuminate\Container\Container->build() /home/vagrant/projects/fry/vendor/laravel/framework/src/Illuminate/Container/Container.php:656

When I comment out the deleteSaved line, no error.

GrahamCampbell commented 9 years ago

You need to catch the exception. :)

GrahamCampbell commented 9 years ago

You can inspect the exceptions that were wrapped by it: https://github.com/thephpleague/factory-muffin/blob/master/src/Exceptions/DeletingFailedException.php#L80.

GrahamCampbell commented 9 years ago

To see what went wrong.

bcdennis commented 9 years ago

Thank you Graham, I'll troubleshoot and report back in the morning.

On Wed, Feb 4, 2015 at 4:31 PM, Graham Campbell notifications@github.com wrote:

To see what went wrong.

— Reply to this email directly or view it on GitHub https://github.com/thephpleague/factory-muffin/issues/383#issuecomment-72954460 .

bcdennis commented 9 years ago

Ok, so by placing the call in the static tearDownAfterClass function the IOC container couldn't resolve the app['config'] setting needed for the database connection when the models were being deleted. I don't know enough about how that all works to understand why, but moving the $muffin->deleteSaved() call into the tearDown method it worked.

It was throwing a reflection error with the message 'Class config does not exist'.

Edit**

Nevermind. This problem is still occurring. I have the database configured in the phpunit.xml so I can only assume now it's some laravel5 bug and not an issue with factory-muffin.

Update**

Boy, this was a dumb one. I was calling muffin->deleteSaved() after parent::tearDown().

Thanks Graham, there isn't an issue anywhere.

GrahamCampbell commented 9 years ago

Nice. If you have any other suggestions or questions, feel free to open another issue.

Also, sorry the 3.0 release is so delayed. I haven't had the time to write a proper upgrading guide from 2.1 -> 3.0 yet. As soon as that's done, I'll be cleaning up the usage docs, and releasing.

bcdennis commented 9 years ago

Just an FYI, it was an order of operations issue dealing with both Factory Muffin and Mockery when using the Laravel Testcase. Any exceptions that are thrown after tearDown in Illuminate\Foundation\Testing\TestCase is called (like a Mocking exception or a FM exception when deleting) result in a binding resolution error because the IOC container doesn't have an error handler bound to the error handling contract. This completely obfuscates what is happening and makes it nearly impossible to troubleshoot, or in the case of a mocking exception, for the testcase to fail naturally as it should.

Putting both Mockery::close() and $fm->deleteSaved() before the parent::tearDown() in my testcases resolved this issue.

I'm writing this information here for posterity.

GrahamCampbell commented 9 years ago

@bcdennis This might be useful to note in our docs. Is there any chance you could send a pull request to add it please?