How to test Controllers using PHPUnit

Published: Wednesday, February 4th, 2015 by Chris Taylor

This is a quick post to show you how I write and test Controllers, although it may not be to a correct standard, the testing principles can be applied to any project. A Controller can be used to direct requests into the correct parts of your application. They can coordinate data from Repositories, Services and […]

Mock third party API’s with PHPUnit

Published: Tuesday, February 3rd, 2015 by Chris Taylor

Well sort of. This is an example of how to use a wrapper class for a third party API to make it easier to write and test your application. In your unit tests you should not actually call any third party API’s, or any kind of API for that matter. You should mock it. In […]

How to test MongoDB Repositories using PHPUnit & Mongounit

Published: Friday, January 16th, 2015 by Chris Taylor

A repository is a way of making a standard interface into your database from your application. In MVC terms, it’s the Model. They should all extend a base repository which defines how they should interact with the DB. In this example it will be MongoDB. They should also all implement an interface which declares functions […]

Write simple but expressive functions

Published: Thursday, January 8th, 2015 by Chris Taylor

This is a quick demonstration of the trade-off of readability and efficiency of coding. Currently I have a function that is well written and commented (in my opinion) that looks something like this: public function constructPayLoad(array $template, array $report, $companyId) { //format the payload to contact the service $payLoad = [ ‘objectName’ => $template[‘objectName’], ‘reportName’ […]

How to generate PHP documentation using Sami

Published: Tuesday, January 6th, 2015 by Chris Taylor

Right, so you’ve got a lovely PHP class, lots of fancy functions and you want to release it so that other people can use it. For this you’ve got to write some documentation. As boring as it is, it is incredibly useful if you want anybody to be able to use your code. It also […]

How to test internal functions of a class

Published: Monday, January 5th, 2015 by Chris Taylor

Using PHPUnit and partial mocks Let’s say you have a super complicated class that has lots of functions that are used to manipulate data or call a third party class or just does something that is repeated thoughout your class so it’s useful to have it as a seperate function. If you’re using a tool […]