PhpRiot
News Archive
PhpRiot Newsletter
Your Email Address:

More information

Where To Put Your Component's Unit Tests

Note: This article was originally published at Planet PHP on 26 March 2011.
Planet PHP

In my Beyond Frameworks talk, I explained how a component-based architecture can help answer some of the important (i.e. expensive!) questions you might face when creating long-lived apps that rely on a PHP framework. In this series of blog posts, I'm going to look at how to go about creating and working with components.

I've started work on creating the components for my simple app. The first component is repustateApi, which will be a PHP client for Repustate.com's semantic analysis API. With the component's metadata all done, and the decision made to start with unit tests, let's get the unit tests written and running.

The First Unit Tests

Repustate's API is extremely simple to use, making it the ideal choice for my first semantic analysis client. It's very low risk to knock up a client and see if their API does the job; if the API isn't suitable under heavy testing, and I'm forced to try someone else's semantic analysis API, I haven't lost much time in the attempt.

As a result, my initial unit tests can be extremely simple:

namespace Gradwell\RepustateApi;

class ClientTest extends \PHPUnit_Framework_TestCase
{
A A A A public function setUp()
A A A A {
A A A A A A A A $this-_apiKey = a;
A A A A }

A A A A public function testCanCreateClient()
A A A A {
A A A A A A A A // create the client with a valid key
A A A A A A A A $client = new Client($this-_apiKey);

A A A A A A A A // did it work?
A A A A A A A A $this-assertTrue($client instanceof Client);
A A A A }

A A A A public function testCanRetrieveSentimentScoreForTextByJson()
A A A A {
A A A A A A A A // setup
A A A A A A A A $client = new Client($this-_apiKey);
A A A A A A A A $text = "this is a happy piece of text";

A A A A A A A A // do the test
A A A A A A A A $score

Truncated by Planet PHP, read more at the original (another 9190 bytes)