Typed PHP, Stronger Types for Cleaner Code (2014)
How To Test
Testing is an essential part of supplanting the native type handling system. The good news is that it will be easy to do!
PHPUnit is a unit-testing library which makes the process of testing small units of code super easy:
1 class StringObjectTest extends PHPUnit_Framework_TestCase
3 /** @test */
4 public function trimWorksWithStrings()
6 $subject = Type\toStringObject("Hello World...");
9 "Hello World",
This kind of testing can be done with a number of testing frameworks. At the end of the day, as long as you are writing tests, whichever testing framework you use is up to you.
What Should Be Tested?
The short answer is: everything.
We’re aiming to build something solid, and it’s pretty low-level. That means we need to be sure things continue to work as expected. It’s not even that hard when you consider how simple the methods are that we are going to make.
We should aim to have good coverage of the namespaced functions, and a few tests to ensure that these are correctly called from the (proxy) classes.
When Should Tests Be Written?
This is really up to you. Maybe you want to write your tests first, and follow the red-green-refactor cycle. Maybe you want to write all of your library code first, and then make sure everything works as you expected it to (by writing tests for the library code).
The important thing is to write tests.
I highly recommend the following books:
· Clean Code, by Robert C. Martin
· The Grumpy Programmer’s PHPUnit Cookbook, by Chris Hartjes