Unit Testing your closure methods with PHP

Today, I am happy to share with you guys with a practical example how to test the business logic of the Closures inside our methods, imagine that you have a method like the next one on your PHP Class:

Since we are running another object inside our closure, we will need to mock this one beyond the Connection class on the example, at least for the methods executeUpdate and commit, to do that we can follow the next example:

As you can see, we are mocking the objects inside the closure, this specific example should return a DateTimeImmutable object, after the shouldReceive, we can use the method with to emulate our closure and test whatever we need to do inside that.

Hope this example can save you a little of your time :)

Software Developer, trying to learn more every day.