Fake Test Double Using Prophecy

Fake test double is a simplified version of a dependency. A good example is an in memory or file system repository in exchange for database repository. Fake objects implement the interface of dependency.

Below is the class which has a constructor dependency:

<?php

namespace App;

class Questioner
{
    /** @var OutputInterface */
    private $output;

    public function __construct(OutputInterface $output)
    {
        $this->output = $output;
    }
}

The OutputInterface is:

<?php

namespace App;

interface OutputInterface
{
    /**
     * Save the data.
     *
     * @param  array  $data
     * @param  string $name
     * @return boolean
     */
    public function save(array $data, $name = null);
}

In production you would like to use a database output for persistence. The following object might be used:

<?php

namespace App;

class DatabaseOutput implements OutputInterface
{
    /** @inheritDoc */
    public function save(array $data, $name = null)
    {
        // call database or do other time consuming tasks
    }
}

This will make unit testing impossible. To test the code you would need to use functional tests which would require a database. Code using the database will run much longer and the longer tests run the higher the chance they won't be run at all.

For this reason in unit testing a simpler version of dependency can be used:

<?php

namespace App;

class InMemoryOutput implements OutputInterface
{
    /** @inheritDoc */
    public function save(array $data, $name = null)
    {
        // simple, fast implementation for testing
    }
}

This article is from the Test doubles using Prophecy series which is made from following articles:

  • Dummy Test Double Using Prophecy
  • Stub Test Double Using Prophecy
  • Spy Test Double Using Prophecy
  • Mock Test Double Using Prophecy
  • Fake Test Double Using Prophecy
  • Posted in: PHP, Prophecy, TDD, Technical

    Comments