class

PHP Logo

PHP:CSI Get Price In Pence

17th July 2020 - 14 minutes read time

I was looking at some malfunctioning code the other day where the price was pulled out of one API service and sent to another API. The problem stemmed from the fact that the value coming out of the first API was as a string and the second API required the price in pence as an integer.

The difference in formats here meant that the number had to be converted from one format to another. During this process it was found that the value was sometimes out by a single pence.

For example, whilst the first API sent over a value of £20.40, the second API received a value of 2039, which is one penny out. This class did have some unit tests, but the tests but had failed to account for this rounding error.

As it turned out, this wasn't the only problem with the class in question, so I thought I would write up a quick PHP:CSI showing the problems and how I solved them.

PHP Logo

PHPUnit Skeleton Classes

25th July 2011 - 7 minutes read time

If you create classes in PHP then you should be unit testing them as much as you can. Setting up unit testing classes for your code can be time consuming and involve a bunch of copying and pasting. Thankfully, PHPUnit comes with a couple of helper functions that allow the creation of unit testing classes automatically, which can save a bit of copying and pasting.

As an example for this post I will use the following Spider class, which is part of some code I am working on at the moment to create a simple site spider in PHP.