Skip to content

Instantly share code, notes, and snippets.

@studioromeo
Last active August 29, 2015 13:57
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 studioromeo/9665180 to your computer and use it in GitHub Desktop.
Save studioromeo/9665180 to your computer and use it in GitHub Desktop.
PHPSpec
<?php
namespace Acme;
use Acme\SubDependency;
class Dependency
{
protected $subDep;
function __construct(SubDependency $subDep)
{
$this->subDep = $subDep;
}
function getRender()
{
return $this->subDep->render();
}
}
<?php
namespace Acme;
use Acme\Dependency;
class RoadRunner
{
protected $dep;
public function __construct(Dependency $dep)
{
$this->dep = $dep;
}
public function letsGo()
{
return $this->dep->getRender();
}
}
<?php
namespace spec\Acme;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Acme\Dependency;
use Acme\SubDependency;
class RoadRunnerSpec extends ObjectBehavior
{
function let(Dependency $dependency)
{
// This injects Dependency (subject) into the RoadRunner object but leaves out SubDep from Dependency?
$this->beConstructedWith($dependency)
}
function it_is_initializable()
{
$this->shouldHaveType('Acme\RoadRunner');
}
function it_should_return_dep()
{
// This should return hello world if all dependencies are set up correctly
$this->letsGo()->shouldReturn('Hello World');
}
}
<?php
namespace Acme;
class SubDependency
{
public function render()
{
return 'Hello world';
}
}
@jakzal
Copy link

jakzal commented Mar 21, 2014

<?php

namespace spec\Acme;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Acme\Dependency;
use Acme\SubDependency;

class RoadRunnerSpec extends ObjectBehavior
{
    function let(Dependency $dependency)
    {
        // You don't care about SubDependency here! You only interact with direct collaborator.
        $this->beConstructedWith($dependency)
    }

    function it_is_initializable()
    {
        $this->shouldHaveType('Acme\RoadRunner');
    }

    function it_renders_a_string(Dependency $dependency)
    {
        $dependency->getRender()->willReturn('Hello World');

        // letGo() returns whatever the Dependency returned, 
        // you should spec Dependency::getRender() in the DependencySpec
        $this->letsGo()->shouldReturn('Hello World');
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment