Posted on by & filed under Content - Highlights and Reviews, Web Development.

I recently applied for a job where I was asked to complete a coding project as part of the interview process. This wasn’t the first time, though code samples are more typical. This post is about the process as I experienced it: looking at the test, working through the problems, coming up with a solution, and doing it within the time allotted: 4 hours.

The Problem

The subject of the test is pretty straight forward on first blush. I need to write a PHP class to consume a 3rd party API that provides currency conversion rates. The class needs to retrieve, parse and save the data into an existing mysql table, as well as perform conversions for individual currencies (convert JPY 4000 to USD) and arrays of currencies (convert (“JPY 4000”, “CAN 350”) to (“USD XX.XX”, “USD XX.XX”)).

Here’s a couple things the coding test seems to want to determine:

  1. How will I handle API errors?
  2. What will I do if the API sends me new Currency Codes?
  3. How will I handle unknown currency codes during conversion?

Anytime you’re dealing with money, assumptions acquire significant consequences. Fortunately, this is only a test. Unfortunately, I’ve been eyes deep in Javascript and Perl code for a couple years, so my PHP is a little dated and rusty. No time like the present, right?

Where to start

I decided to start by addressing the immediate questions. For the purpose of the test, if I get a bad response from the API, the script will just exit. If I get new currency codes, I’ll just push them into the DB and move on. If the script is asked to convert to or from a currency it doesn’t know about, it will return the original currency code and amount.

I’ll start by throwing a static XML up in the proper format on my own site so I can pull it down, and begin the first part – extraction, parsing and saving.

Early Lessons

Right away I realized that my PHP rust was thicker than I expected. It took me a while before I was parsing the XML in a way I liked. I found I had to make a couple additional assumptions. I assumed the table I was inserting into had the currency code as a primary key, and that I had access to typical PHP components like PDO. It wasn’t long before I had a tiny bit of code that would pull an XML file, iterate over the rates and shove them into a table:

Testing: Now, or Later

I’m still a long way from a class though, and I haven’t even looked at the parsing. But it’s time to make a choice: write some tests, or press on and write the tests at the end. I decided to give myself a 15 minute time limit to put together a few tests (If I had started with a test framework in the beginning would have been plenty of time.) I settled for some tests that would load different XML rates files and check to see if I was getting expected values. Of course, doing this grew well outside the timebox I had set, as the act of writing the tests showed me that I had aspects of the class I hadn’t thought out well or written yet. For example, storing the rates in the class so I’m not going back to the DB for them every time.


Once I’d gotten a few tests in place, it was time to look at currency conversion. The assignment indicates that it should accept strings or arrays of strings. It seems sensible to use the same function to handle both, handing off individual strings to another method for processing. Unknown currency codes get send back unchanged.

Drawing It Together

With those couple methods written, it’s time to push through some more test data (and subsequently, uncover some bugs I’m glad I caught). With the code finished, I can go back over it, add comments, clean up variable inconsistencies, and look at the tests. I have about a dozen or so tests written, and I can tell I haven’t written a test for everything. I’m going to take a little extra time just to be sure I have the coverage I want – at least invoking each method.

It turns out that was a good thing to do. I had made a couple really dumb mistakes in there. Dumb mistakes like this one:

That method is just there for utility, and wasn’t getting touched by the tests. Clearly it doesn’t do what I want it to. I had also made some methods private that I should have made protected, and I hadn’t realized that until I tried extending the class for testing purposes.


Getting back up to speed after being in a different language for a couple years was more of a barrier than I expected it to be. I don’t think the test itself is complicated, and I may have over thought it. The biggest lesson here was in using the tests as I was working through the problem. Sure, it’s a best practice. But I might not have normally done it while taking a coding test. I’m glad I did this time.

Safari Books Online has the content you need

Here are some good PHP resources found in Safari Books Online:

If you want to write a mobile application using geo-location data, Pro PHP Programming will show you how. Additionally, if you need to make sure that you can write a multilingual indexing application using Sphinx, this book will help you avoid the pitfalls.
PHP: The Good Parts shows you the most useful features of PHP, and explains how you can speed up the web development process with them. You’ll learn why the most commonly used PHP features are often misused or misapplied, and which features add strength to object-oriented programming. You’ll also focus on aspects that will help you integrate your application with databases.
PHP Master: Write Cutting-edge Code is tailor-made for the PHP developer who’s serious about taking their server-side applications to the next level and who wants to really keep ahead of the game by adhering to best practice, employing the most effective object-oriented programming techniques, wrapping projects in layers of security and ensuring their code is doing its job perfectly.
PHP Team Development starts by explaining the need for teams working on complex software projects. You learn how you can divide the complexity of PHP projects with the help of the MVC pattern and the use of frameworks. It then discusses the need for a process and how you can choose the right process. It teaches you how to use agile principles to deliver working software for customers.
PHP Objects, Patterns, and Practice, Second Edition shows you how to meld the power of PHP with the sound enterprise development techniques embraced by professional programmers. Going well beyond the basics of object-oriented development, you’ll learn about advanced topics such as working with static methods and properties, abstract classes, interfaces, design patterns, exception handling, and more.

About the Author

  Duane O’Brien is a tired computer scientist. He has written a number of articles on developing web applications and various PHP frameworks. To learn more about Duane, check out his blog or read his tweets.

Tags: APIs, conversion, php, test,


  1.  PHP Tip: Test Postmortem « Safari Books Online's Official Blog
  2.  PHP Tip: Test Taking Strategies « Safari Books Online's Official Blog