# Getting Started With Phpspec

In this short, yet comprehensive, tutorial, we'll have a look at behavior driven development (BDD) with phpspec. Mostly, it will be an introduction to the phpspec tool, but as we go, we'll touch on different BDD concepts. BDD is a hot topic these days and phpspec has gained a lot of attention in the PHP community recently.

## SpecBDD & Phpspec

BDD is all about describing the behavior of software, in order to get the design right. It is often associated with TDD, but whereas TDD focus on testing your application, BDD is more about describing its behavior. Using a BDD approach will force you to constantly consider the actual requirements and desired behavior of the software you're building.

Two BDD tools have gained a lot of attention in the PHP community recently, Behat and phpspec. Behat helps you describe the external behavior of your application, using the readable Gherkin language. phpspec, on the other hand, helps you describe the internal behavior of your application, by writing small "specs" in the PHP language - hence SpecBDD. These specs are testing that your code has the desired behavior.

## What We Will Do

In this tutorial, we'll cover everything related to getting started with phpspec. On our way, we'll build the fundament of a todo list application, step-by-step, using a SpecBDD approach. As we go, we'll have phpspec lead the way!

Note: This is an intermediate article about PHP. I assume you have a good grasp of object-oriented PHP.

## Installation

For this tutorial, I assume you have the following stuff up and running:

• A working PHP setup (min. 5.3)
• Composer

Installing phpspec through Composer is the easiest way. All you have to do is run the following command in a terminal:

 1 $composer require phpspec/phpspec  2 Please provide a version constraint for the phpspec/phpspec requirement: 2.0.*@dev  This will make a composer.json file for you and install phpspec in a vendor/ directory. In order to make sure that everything is working, run phpspec and see that you get the following output:  1 $ vendor/bin/phpspec run  2 3 0 specs  4 0 examples  5 0ms 

## Configuration

Before we start, we need to do a little bit of configuration. When phpspec runs, it looks for a YAML file named phpspec.yml. Since we will be putting our code in a namespace, we need to make sure that phpspec knows about this. Also, while we are at it, let's make sure that our specs looks nice and pretty when we run them.

Go ahead and make the file with the following content:

 1 formatter.name: pretty  2 suites:  3  todo_suite:  4  namespace: Petersuhm\Todo 

There are many other configuration options available, which you can read about in the documentation.

Another thing we need to do, is to tell Composer how to autoload our code. phpspec will use Composer's autoloader, so this is required for our specs to run.

Add an autoload element to the composer.json file that Composer made for you:

 1 {  2  "require": {  3  "phpspec/phpspec": "2.0.*@dev"  4  },  5  "autoload": {  6  "psr-0": {  7  "Petersuhm\\Todo": "src"  8  }  9  }  10 } 

Running composer dump-autoload will update the autoloader after this change.

## Our First Spec

Now we are ready to write our first spec. We'll start by describing a class called TaskCollection. We'll have phpspec generate a spec class for us by using the describe command (or alternatively the short version desc) .

 1 $vendor/bin/phpspec describe "Petersuhm\Todo\TaskCollection"  2 $ vendor/bin/phpspec run  3 Do you want me to create Petersuhm\Todo\TaskCollection for you? y 

So what happened here? First, we asked phpspec to create a spec for a TaskCollection. Second, we ran our spec suite and then phpspec automagically offered to create the actual TaskCollection class for us. Cool, isn't it?

Go ahead and run the suite again, and you'll see that we already have one example in our spec (we'll see in a moment what an example is):

 1 $vendor/bin/phpspec run  2 3  Petersuhm\Todo\TaskCollection  4 5  10 ✔ is initializable  6 7 8 1 specs  9 1 examples (1 passed)  10 7ms  From this output, we can see that the TaskCollection is initializable. What is this about? Take a look at the spec file that phpspec generated, and it should be clearer:  1 shouldHaveType('Petersuhm\Todo\TaskCollection');  13  }  14 }  The phrase 'is initializable' is derived from a function named it_is_initializable() which phpspec has added to a class called TaskCollectionSpec. This function is what we refer to as an example. In this particular example, we have what we refer to as a matcher called shouldHaveType() that checks the type of our TaskCollection. If you change the parameter passed to this function to something else and run the spec again, you will see that it will fail. Before completely understanding this, I think we need to investigate in what the variable $this refers to in our spec.

### What Is $this? Of course, $this refers to the instance of the class TaskCollectionSpec, since this is just regular PHP code. But with phpspec, you have to treat $this different from what you normally do, since under the hood, it's actually referring to the object under test, which is in fact the TaskCollection class. This behavior is inherited from the class ObjectBehavior, which makes sure that function calls are proxied to the spec'ed class. This means that SomeClassSpec will proxy method calls to an instance of SomeClass. phpspec will wrap these method calls in order to run their return values against matchers like the one you just saw. You don't need a deep understanding of this in order to use phpspec, just remember that as far as you are concerned, $this actually refers to the object under test.

So far, we haven't done anything ourself. But phpspec have made an empty TaskCollection class for us to use. Now is the time to fill in some code and make this class useful. We will add two methods: an add() method, to add tasks, and a count() method, to count the number of tasks in the collection.

Before we write any real code, we should write an example in our spec. In our example, we want to try to add a task to the collection, and then afterwards make sure that the task is in fact added. In order to do this, we need an instance of the (so far non-existing) Task class. If we add this dependency as a parameter to our spec function, phpspec will automatically give us an instance that we can use. Actually, the instance isn't a real instance, but what phpspec refers to as a Collaborator. This object will act as the real object, but phpspec allows us to do more fancy stuff with this, which we'll see soon. Even though the Task class doesn't exist yet, for now, just pretend it does. Open up the TaskCollectionSpec and add a use statement for the Task class and then add the example it_adds_a_task_to_the_collection():

 1 use Petersuhm\Todo\Task;  2 3 ...  4 5 function it_adds_a_task_to_the_collection(Task $task)  6 {  7 $this->add($task);  8 $this->tasks[0]->shouldBe($task);  9 }  In our example, we write the code "we wish we had". We call the add() method and then try to give it a $task. Then we check that the task was in fact added to the instance variable $tasks. The matcher shouldBe() is an identity matcher similar to the PHP === comparator. You can use either shouldBe(), shouldBeEqualTo(), shouldEqual() or shouldReturn() - they all do the same. Running phpspec will yield some errors, since we don't have a class named Task yet. Let's have phpspec fix that for us:  1 $ vendor/bin/phpspec describe "Petersuhm\Todo\Task"  2 $vendor/bin/phpspec run  3 Do you want me to create Petersuhm\Todo\Task for you? y  Running phpspec again, something interesting happens:  1 $ vendor/bin/phpspec run  2 Do you want me to create Petersuhm\Todo\TaskCollection::add() for you? y 

Perfect! If you take a look at the TaskCollection.php file, you will see that phpspec made an add() function for us to fill out:

 1 

phpspec is still complaining, though. We don't have a $tasks array, so let's make one and add the task to it:  1 tasks[] =$task;  12  }  13 } 

Now our specs are all nice and green. Note that I made sure to typehint the $task parameter. Just to make sure we got it right, let's add another task:  1 function it_adds_a_task_to_the_collection(Task$task, Task $anotherTask)  2 {  3 $this->add($task);  4 $this->tasks[0]->shouldBe($task);  5 6 $this->add($anotherTask);  7 $this->tasks[1]->shouldBe($anotherTask);  8 }  Running phpspec, it looks like we're all good. ### Implementing the Countable Interface We want to know how many tasks are in a collection, which is a great reason for using one of the interfaces from the Standard PHP Library (SPL), namely the Countable interface. This interface dictates that a class implementing it must have a count() method. Earlier, we used the matcher shouldHaveType(), which is a type matcher. It uses the PHP comparator instanceof to validate that an object is in fact an instance of a given class. There are 4 type matchers, which all does the same. One of them is shouldImplement(), which is perfect for our purpose, so let's go ahead and use that in an example:  1 function it_is_countable()  2 {  3 $this->shouldImplement('Countable');  4 } 

See how beautiful that reads? Let's run the example and have phpspec lead the way for us:

 1 $vendor/bin/phpspec run  2 3  Petersuhm/Todo/TaskCollection  4  25 ✘ is countable  5  expected an instance of Countable, but got [obj:Petersuhm\Todo\TaskCollection].  Okay, so our class isn't an instance of Countable since we haven't implemented it yet. Let's update the code for our TaskCollection class:  1 class TaskCollection implements \Countable  Our tests won't run, since the Countable interface has an abstract method, count(), which we have to implement. An empty method will do the trick for now:  1 public function count()  2 {  3  // ...  4 }  And we're back to green. At the moment our count() method doesn't do much, and it's actually pretty useless. Let's write a spec for the behavior that we wish it to have. First, with no tasks, our count function is expected to return zero:  1 function it_counts_elements_of_the_collection()  2 {  3 $this->count()->shouldReturn(0);  4 } 

It returns null, not 0. To get a green test, let's fix this the TDD/BDD way:

 1 public function count()  2 {  3  return 0;  4 } 

We're green and all is good, but this is probably not the behavior we want. Instead, let's expand our spec and add something to the $tasks array:  1 function it_counts_elements_of_the_collection()  2 {  3 $this->count()->shouldReturn(0);  4 5  $this->tasks = ['foo'];  6 $this->count()->shouldReturn(1);  7 } 

Of course, our code is still returning 0, and we have a red step. Fixing this is not too difficult and our TaskCollection class should now look like this:

 1 tasks[] = $task;  12  }  13 14  public function count()  15  {  16  return count($this->tasks);  17  }  18 } 

We have a green test and our count() method works. What a day!

## Expectations & Promises

Remember I told you that phpspec allows you to do cool stuff with instances of the Collaborator class, AKA the instances that are automatically injected by phpspec? If you have been writing unit tests before, you know what mocks and stubs are. If you don't, please don't worry too much about it. It's only jargon. These things refers to "fake" objects that will act as your real objects, but allow you to test in isolation. phpspec will automatically turn these Collaborator instances into mocks and stubs if you need it in your specs.

This is really awesome. Under the hood, phpspec uses the Prophecy library, which is a highly opinionated mocking framework that plays well with phpspec (and is build by the same awesome folks). You can set an expectation on a collaborator (mocking), like "this method should be called", and you can add promises (stubbing), like "this method will return this value". With phpspec this is really easy and we'll do both things next.

Let's make a class, we'll call it TodoList, that can make use of our collection class.

 1 $vendor/bin/phpspec desc "Petersuhm\Todo\TodoList"  2 $ vendor/bin/phpspec run  3 Do you want me to create Petersuhm\Todo\TodoList for you? y 

The first example we'll add, is one for adding tasks. We will make an addTask() method, that does nothing more than adding a task to our collection. It simply directs the call to the add() method on the collection, so this is a perfect place to make use of an expectation. We do not want the method to actually call the add() method, we just want to make sure that it tries to do it. Furthermore, we want to make sure that it calls it only once. Take a look at how we can go about this with phpspec:

 1 shouldHaveType('Petersuhm\Todo\TodoList');  15  }  16 17  function it_adds_a_task_to_the_list(TaskCollection $tasks, Task$task)  18  {  19  $tasks->add($task)->shouldBeCalledTimes(1);  20  $this->tasks =$tasks;  21 22  $this->addTask($task);  23  }  24 } 

First, we have phpspec provide us with the two collaborators we need: a task collection and a task. Then we set an expectation on the task collection collaborator that basically says: "the add() method should be called exactly 1 time with the variable $task as a parameter". This is how we prepare our collaborator, which is now a mock, before we assign it to the $tasks property on the TodoList. Finally, we try to actually call the addTask() method.

 1 $vendor/bin/phpspec run  2 3  Petersuhm/Todo/TodoList  4  17 ! adds a task to the list  5  property tasks not found.  The $tasks property is non-existing - easy one:

 1 

Try again, and have phpspec guide our way:

 1 $vendor/bin/phpspec run  2 Do you want me to create Petersuhm\Todo\TodoList::addTask() for you? y  3 $ vendor/bin/phpspec run  4 5  Petersuhm/Todo/TodoList  6  17 ✘ adds a task to the list  7  some predictions failed:  8  Double\Petersuhm\Todo\TaskCollection\P4:  9  Expected exactly 1 calls that match:  10  Double\Petersuhm\Todo\TaskCollection\P4->add(exact(Double\Petersuhm\Todo\Task\P3:000000002544d76d0000000059fcae53))  11  but none were made. 

Okay, now something interesting happened. See the message "Expected exactly 1 calls that match: ..."? This is our failing expectation. This happens because after calling the addTask() method, the add() method on the collection was not called, which we expected it to be.

In order to get back to green, fill in the following code in the empty addTask() method:

 1 tasks->add($task);  12  }  13 }  Back to green! It feels good, right? ### Checking for Tasks Let's have a look at promises too. We want a method that can tell us if there are any tasks in the collection. For this, we'll simply check the return value of the count() method on the collection. Again, we do not need a real instance with a real count() method. We just need to make sure that our code calls some count() method and do some stuff depending on the return value. Take a look at the following example:  1 function it_checks_whether_it_has_any_tasks(TaskCollection$tasks)  2 {  3  $tasks->count()->willReturn(0);  4 $this->tasks = $tasks;  5 6 $this->hasTasks()->shouldReturn(false);  7 } 

We have a task collection collaborator that has a count() method that will return zero. This is our promise. What this means is that every time someone calls the count() method, it will return zero. We then assign the prepared collaborator to the $tasks property on our object. Finally, we try to call a method, hasTasks(), and make sure it returns false. What does phspec have to say about this?  1 $ vendor/bin/phpspec run  2 Do you want me to create Petersuhm\Todo\TodoList::hasTasks() for you? y  3 $vendor/bin/phpspec run  4 5  Petersuhm/Todo/TodoList  6  25 ✘ checks whether it has any tasks  7  expected false, but got null.  Cool. phpspec made us a hasTasks() method and not surprisingly, it returns null, not false. Once again, this is an easy one to fix:  1 public function hasTasks()  2 {  3  return false;  4 }  We are back to green, but this is not quite what we want. Let's check for tasks when there are 20 of them. This should return true:  1 function it_checks_whether_it_has_any_tasks(TaskCollection$tasks)  2 {  3  $tasks->count()->willReturn(0);  4 $this->tasks = $tasks;  5 6 $this->hasTasks()->shouldReturn(false);  7 8  $tasks->count()->willReturn(20);  9 $this->tasks = $tasks;  10 11 $this->hasTasks()->shouldReturn(true);  12 } 

Run phspec and we'll get:

 1 $vendor/bin/phpspec run  2 3  Petersuhm/Todo/TodoList  4  25 ✘ checks whether it has any tasks  5  expected true, but got false.  Okay, false is not true, so we need to improve our code. Let's use that count() method to see if there are tasks or not:  1 public function hasTasks()  2 {  3  if ($this->tasks->count() > 0)  4  return true;  5 6  return false;  7 } 

Tah dah! Back to green!

## Building Custom Matchers

Part of writing good specs is to make them as readable as possible. Our last example can actually be improved a tiny bit, thanks to phpspec's custom matchers. It's easy to implement custom matchers - all we have to do is to overwrite the getMatchers() method that is inherited from ObjectBehavior. By implementing two custom matchers, our spec can be changed to look like this:

 1 function it_checks_whether_it_has_any_tasks(TaskCollection $tasks)  2 {  3 $tasks->count()->willReturn(0);  4  $this->tasks =$tasks;  5 6  $this->hasTasks()->shouldBeFalse();  7 8 $tasks->count()->willReturn(20);  9  $this->tasks =$tasks;  10 11  $this->hasTasks()->shouldBeTrue();  12 }  13 14 function getMatchers()  15 {  16  return [  17  'beTrue' => function($subject) {  18  return $subject === true;  19  },  20  'beFalse' => function($subject) {  21  return $subject === false;  22  },  23  ];  24 }  I think this looks pretty good. Remember, that refactoring your specs is important in order to keep them up to date. Implementing your own custom matchers can clean up your specs and make them more readable. Actually, we can use the negation of the matchers as well:  1 function it_checks_whether_it_has_any_tasks(TaskCollection$tasks)  2 {  3  $tasks->count()->willReturn(0);  4 $this->tasks = $tasks;  5 6 $this->hasTasks()->shouldNotBeTrue();  7 8  $tasks->count()->willReturn(20);  9 $this->tasks = $tasks;  10 11 $this->hasTasks()->shouldNotBeFalse();  12 } 

Yeah. Pretty cool!

## Conclusion

All our specs are green and look at how nicely they document our code!

 1  Petersuhm\Todo\TaskCollection  2 3  10 ✔ is initializable  4  15 ✔ adds a task to the collection  5  24 ✔ is countable  6  29 ✔ counts elements of the collection  7 8  Petersuhm\Todo\Task  9 10  10 ✔ is initializable  11 12  Petersuhm\Todo\TodoList  13 14  11 ✔ is initializable  15  16 ✔ adds a task to the list  16  24 ✔ checks whether it has any tasks  17 18 19 3 specs  20 8 examples (8 passed)  21 16ms 

We have effectively described and achieved the desired behavior of our code. Not to mention, our code is 100 % covered by our specs, which means that refactoring won't be a fear-inducing experience.

By following along, I hope you got inspired to give phpspec a try. It is more than a testing tool - it's a design tool. Once you get used to using phpspec (and its awesome code generation tools), you'll have a hard time letting go of it again! People often complain that doing TDD or BDD slows them down. After incorporating phpspec in my work flow, I really feel the opposite way - my productivity is significantly improved. And my code is more solid!