r/PHPhelp • u/8draco8 • Sep 26 '16
Best book/tutorial for writing unit tests
I have many years of PHP programming behind my belt, but for some reason I've never got to writing unit tests. When I was starting almost nobody was writing tests in PHP. When tests become industry standard I was senior enough to have junior write them for me. Now I am at the point that I want to finally learn to write them because it's unthinkable that mid/senior dev never wrote a unit test.
Please point me to good (don't have to be free), preferably online resources about how to start with writing tests in PHP.
Recently I am doing project using Codeigniter that I would like to write tests for, so it would be nice if tutorial or book would be framework specific, but don't have to be.
1
u/sagarpodilapu Sep 26 '16
Even I want to learn this. If you have found something please share it.
1
u/8draco8 Sep 27 '16
I found this which looks perfect for my current needs
https://leanpub.com/codeigniter-testing-guide
Some more books about testing PHP code can be found on Leanpub
3
u/nickhelix Sep 26 '16
Don't feel alone, I'm also a senior dev with 10+ years and am trying to learn them too
1
u/halfercode Sep 27 '16 edited Sep 27 '16
A good way to start is to have a simple app to test. I wrote this app a couple of years ago to try my hand at testing, so the tests are not great (or complete) but they might be of interest. There's both unit and function testing there. Feel free to take a look.
My recommended approach would be something like this:
Set up a repo with an app to test
Bring in PHPUnit via Composer
Get a simple dummy test to pass in a single file
Write some test class skeletons with dummy tests
Once you're happy with this, get it building on Travis or similar - it is free for public repos, and it is rewarding to see a new build kicked off on every push
Start implementing your tests, trying to keep to testing one class at a time (and refactoring if this is too difficult - non-testable code is common). Run your tests locally, and if they pass, push your repo to get your remote build running
Learn the basics of PHPUnit configuration and bootstrapping
Then look into mocking e.g. Mockery
FWIW I don't think you need a framework-specific approach - unit tests should not be testing whether something works in a framework, just whether it works. Make sure your framework-specific code and your business logic are decoupled in order to achieve this.
4
u/askageek Sep 26 '16
Because no one wants to pay for you to write them :)