test.ical.ly | getting the web by the balls

Jan/11

12

The ideal use case to give TDD a try!

What I called my “little experience” during the last two days is actually the attempt to rewrite the image manipulation library ImageTransform by Stuart Lowes, which itself is the attempt to turn the symfony plugin sfImageTransformPlugin into a standalone library (unfinished). It’s not at all official and it might be binned again at some point. Until I know that it stays my personal toy.

One of the goals I set myself is to make the code as decoupled as possible and to prove that I try to maintain a high unit test coverage.

Today I just wrote my first transformation using this new architecture and it was just perfect to do it test driven: the resize transformation.The resize transformation resizes an image considering whether to keep proportions, whether inflation or deflation is allowed and if the target dimension sets the maximum or minimum frame.

So four things have to be considered that effect the outcome. That’s 16 different combinations!

Then the original image can be smaller or bigger than the target dimensions or it can be smaller on one side and bigger on the other or it can be the the same. That’s 5 starting situations!

All in all that’s 80 different results!

And it is extremely tedious to test them all by hand of course..

Now I said a few times that I never really got the hang of TDD. Writing tests first still doesn’t feel natural to me though I intellectually accept it being superior to my way of coding.

But in a situation like this even I started writing a test case before a single line of code!

The test itself could not be simpler.

Much more complicated was to write down the correct results for examples for all 80 situations. But PHPUnits awesome @dataProvider feature is a big big help here!

With this test in place I was able to implement a rather complicated algorithm in very little time and what’s more I am very confident that it’s correct because the test says so! ;)

· · · · · · ·



<<

>>

Theme Design by devolux.nh2.me