Entries with the tag "phpunit"

PHPUnit Stop On Failure

From time to time, I get thrown onto a project that has many, many errors in the unit tests. It can be hard to try to figure out what to fix first with so many errors on the screen. Sadly, I’ve had my scroll-back buffer filled many times. (more…)

PHPUnit Test Helpers for Mockery and Non-Public Properties

I’ve written a few helpful methods and systems that help out my unit testing. There are many arguments for and against these tools (don’t test protected methods, don’t introduce global namespace methods, etc) - and those are discussions for another day - but perhaps these might help you out in the mean time. (more…)

Easily Test JSON Keys in Laravel API Response

In my Laravel application, I have an end point that will retrieve a collection of Client models. I have many other unit tests that validate that my repository returns the proper clients when requested, that my client model is sound. My last test is a feature test checks that if I retrieve a list of clients from the end point there is proper pagination and client models exist. I don’t really need to test the... (more…)

Use Anonymous Classes to Test Traits

I’m guilty of creating stub-like classes in my tests to unit test traits, sometimes. So, you end up with a special class inside your unit test file, perhaps at the bottom, that is empty but only extends the trait or something like that. This is not a good idea, but it was my only way that I could figure out how to unit-test traits separately - especially if they were made of protected methods. (more…)

Testing Error 500 Pages in Zend Framework

For the most part, ZF can capture any of your hard errors. It will generate an error 500. You’ve seen them, don’t lie… I’ve seen them way too many times. However, in my production application, I capture these with a specific controller. And because I love Unit Testing, I want to make sure that I test my implementation of my omg-this-is-broken setup. (more…)

PHPUnit Mock Objects: Only Mock What You Need

I was looking at mock objects in PHPUnit the other day and started to get confused. When I asked for a Mock object, the entire object was a mock. In all actuality, it wasn’t so much a Mock of the object but a complete shell of it. Each method needed to be defined. So, if I was testing an object that required three methods to execute, I would have to mock each method. (Yeah, some... (more…)

Add PHPUnit Listeners to Watch for Long Running Tests

One of the under-utilized features of PHPUnit probably is the listeners interface. You can see the configuration options here: php.net/appendixes.configuration.html. So, I decided that I want to use this to know if a Unit Test takes longer than 2 seconds to run. That’s super over-kill in my opinion, but that’s my hard limit. If it takes longer than 2 seconds to run, something is wrong! So, I added the following to my configuration: (more…)

Go to All Posts

Get the RSS for phpunit