Skip to content

Instantly share code, notes, and snippets.

@tomphp
Created December 20, 2013 10:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomphp/8052832 to your computer and use it in GitHub Desktop.
Save tomphp/8052832 to your computer and use it in GitHub Desktop.
<?php
class BaseAdder
{
public function add($a, $b)
{
return $a + $b;
}
}
class BaseAdderSpec extends \PhpSpec\ObjectBehavior
{
function it_adds()
{
$this->add(1, 2)->shouldReturn(3);
}
}
class LogAdder extends BaseAdder
{
protected $logger;
public function __construct($logger)
{
$this->logger = $logger;
}
public function add($a, $b)
{
$this->logger->log("adding $a and $b");
parent::add($a + $b);
}
}
class LogAddedSpec extends \PhpSpec\ObjectBehavior
{
function it_is_a_added()
{
$this->shouldBeAnInstanceOf('BaseAdded');
}
function it_logs()
{
$this->logger->log("adding 1 and 2")->shouldBeCalled();
$this->add(1, 2);
}
/*
* This test appears in BaseAdderSpec too but if it is left out of here
* there's no proof that the actually adding functionality works.
*
* Having it in 2 places also means that we have to remember to update
* here if the base class add() functionality is changed otherwise tests
* will still pass with a broken codebase.
*/
function it_adds()
{
$this->add(1, 2)->shouldReturn(3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment