How to Test Abstract PHP Classes With PHPUnit
How do you test abstract classes in PHP? Find out how to test them in this post by using PHPUnit’s getMockForAbstractClass method.
PhpStorm offers so much functionality. From syntax highlighting to Docker integration, it’s an extremely comprehensive tool. However, have you ever thought of using it to run your unit tests? In this article, I step you through running tests, from an entire suite to an individual test.
Back on November 3rd, last year, I wrote a tutorial showing how to set up PHP debugging with PhpStorm, Xdebug, and PHPUnit. In this article, I’m taking that tutorial further by showing how to run tests in PhpStorm.
Now, you might wonder why you’d run tests in PhpStorm instead of from the command-line, that is if you’re a command-line junkie like myself. Well, while running them from the command-line is likely the best way — especially from an automation perspective — there are some time-saving features of running your PHP tests in PhpStorm.
To provide a little more context, while I was working on a small side-project recently, I came to appreciate the simplicity that PhpStorm offers, because I didn’t have to remember the command-line syntax for running either an individual test or all the tests in one class. Using PhpStorm can be an excellent way to “just get going”, quickly, simply, and efficiently.
Anyway, let’s get into each option. To run tests in you:
Now, let’s step through each of these.
For example, you can see in the image above, that I’ve right-clicked (or context-tapped) on a directory (Response) and have scrolled down to the test options. Depending on the option that I choose, it will run or debug all of the tests in that directory.
If I wanted to run all tests in a single class, I could either right-click on the class file in the project view (as in the image above) or right-click on the tab of an open file, and then choose the desired test option from the context menu that appears.
Running individual tests is a little different, however. In the image above, you can see a right-facing, green arrow next to the test’s signature. Clicking on this opens a context menu specifically for testing, as in the image below. As with the previous approaches, you then choose the option which best suits your needs.
Regardless of the option that you choose, however, when the tests are run, the Run panel opens (by default at the bottom of your PhpStorm window).
It is split into two panels. The panel on the left lists all of the tests that were run categorised based on how the tests were initiated. The panel on the right shows the test output.
In the image above, you can see some of the reasons why some of the tests failed. The image isn’t big enough to show all of the failed test output, but like most windows, you can scroll through the listed output. One advantage of PhpStorm’s Run panel is how it can compact output, making it easier to read and work with.
For example, if you have a test that asserts if two items were the same, such as two strings, it will hide the output, but let it be displayed in a popup window, as in the screenshot below.
Now here’s where it starts to get even more interesting — not to mention time-saving. Once you’ve run some tests for the first time, likely the majority will pass, but some will fail.
In the upper left-hand side of the Run panel, you can choose to re-run just the failed tests. That saves you from having to look through a large set of output if you have an extensive test suite.
Besides, you can (these are the buttons, from left to right, in the image above):
An especially handy option is to toggle auto-testing. This option automatically triggers tests to run when they’re updated. I don’t know of a way to do that on the command-line. I suspect it might be especially handy if I did know how to do it.
And that’s an overview of how to run tests from within PhpStorm. PhpStorm offers more functionality, but what I’ve covered here will get you off to a great start.
I strongly encourage you to experiment and play around with the functionality on offer and find a workflow that works best for you. If you’ve been running tests in PhpStorm for a long time, what are your tips and suggestions? I’d love to know more about them in the comments.
How do you test abstract classes in PHP? Find out how to test them in this post by using PHPUnit’s getMockForAbstractClass method.
Debugging requests can be a time-consuming process. However, there’s a tool that makes doing so a lot simpler. It’s called Postman. In this tutorial, I step you through its core features and show you how to use them.
If you love using data providers in PHPUnit, but find defining them verbose (and potentially buggy) then you’ll love the attribute-based approach in PHPUnit 10. Learn the essentials in this short post.
Substitutability or the Liskov Substitution Principle (LSP) is a concept that I’ve tried to adhere to for some years when writing code. It’s beneficial for many reasons, but particularly when testing, as it can indirectly force you to write code that is more testable. Recently, I’ve started appreciating how it works in Go, and will step through how in this short article.
Please consider buying me a coffee. It really helps me to keep producing new tutorials.
Join the discussion
comments powered by Disqus