Laravel (5.7) Mocking
Introduction
When you are testing Laravel applications, you can wish to "mock" certain aspects of your application so they are not actually executed during a given test. For instance, when you are testing a controller that dispatches an event, you may want to mock the event listeners so they are not actually executed during the test. This will allow you to only test the controller's HTTP response without worrying about the execution of the event listeners, since the event listeners can also be tested in their own test case.
Laravel provides you with helpers for mocking events, jobs, and facades out of the box. These helpers primarily provide a convenience layer over Mockery so that you do not have to manually make complicated Mockery method calls. You can also use PHPUnit or Mockery to create your own mocks or spies.
Bus Fake
Alternatively, rather than using mocking, you can use the Bus facade's fake method to prevent jobs from being dispatched. When you are using fakes, assertions will be made after the code under test is executed:
Event Fake
Rather than using mocking, you can use the Event facade's fake method to prevent all event listeners from executing. You can then assert that events were dispatched and even inspect the data they received. When you use fakes, assertions will be made after the code under test is executed:
Faking A Subset Of Events
In the case where you only want to fake event listeners for a specific set of events, you can pass them to the fake or fakeFor method:
Scoped Event Fakes
In cases where you only want to fake event listeners for a portion of your test, you can use the fakeFor method:
Mail Fake
You can use the Mail facade's fake method to prevent mail from being sent. You can then assert that mailables were sent to users and even inspect the data they received. When you use fakes, assertions will be made after the code under test is executed:
In cases where you are queueing mailables for delivery in the background, you will need to use the assertQueued method instead of assertSent:
Notification Fake
You can use the Notification facade's fake method to prevent notifications from being sent. You can then assert that notifications were sent to users and even inspect the data that they received. When you are using fakes, assertions will be made after the code under test is executed:
Queue Fake
Rather than mocking, you can use the Queue facade's fake method to prevent jobs from being queued. Then you can assert that jobs were pushed to the queue and even inspect the data that they received. When you use fakes, assertions will be made after the code under test is executed:
Storage Fake
The Storage facade's fake method will allow you to easily generate a fake disk that, when combined with the file generation utilities of the UploadedFile class, will greatly simplifies the testing of file uploads. For instance:
Facades
Facades can be mocked, unlike the traditional static method calls. This will provide a great advantage over traditional static methods and grants you the same testability you would have if you were to use dependency injection. When testing, you often may want to mock a call to a Laravel facade in one of your controllers. For instance, consider the following controller action below:
The call to the Cache facade can be mocked by using the shouldReceive method, which returns an instance of a Mockery mock. Since facades are actually managed and resolved by the Laravel service container, they have much more testability than a typical static class would have. For instance, let us mock our call to the Cache facade's get method: