From the course: PHP: Design Patterns

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Exploring a use case for mock objects

Exploring a use case for mock objects - PHP Tutorial

From the course: PHP: Design Patterns

Start my 1-month free trial

Exploring a use case for mock objects

- This is the code we start with. We have a simple snippet that uses our Email class to send messages. Behind the scenes, this Email class just uses PHP's default email-sending mechanism. Now, we're not actually sending a message here, I've commented that section out. But we can see it would have sent a message and we go ahead an echo a statement saying that. While this works as expected, we don't have a good way of testing it until we insert a mock object. So if we load this file in the browser. Here's what we get. We get the echo statement of "This email has been sent." This is how we modify the object. We extend our base object and override the individual methods that we need to. In this case, we want to change how the send method words, as in we don't actually want it to send. You can see here the MockEmail class has extended the Email class. We've written our own version of send(), which just echos a statement, "This would not have sent a message." Therefore, we implement that…

Contents