Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
<?php
class DefaultMyStuffMaker implements MyStuffMaker
{
private $dependency;
public function __construct(SomeDependency $dependency)
{
$this->dependency = $dependency;
}
public function makeStuff(string $someArg) : SomeResult
{
// do some stuff
}
}
<?php
class DefaultMyStuffMakerSpec extends ObjectSpec
{
public function let(SomeDependency $dependency)
{
$this->beConstructedWith($dependecy);
}
public function it_makes_stuff()
{
$this->makeStuff()->shouldReturnAnInstanceOf(SomeResult::class)
}
}
<?php
class LoggableMyStuffMaker implements MyStuffMaker
{
private $next;
private $logger;
public function __construct(MyStuffMaker $maker, Logger $logger)
{
$this->next = $next;
$this->logger = $logger;
}
public function makeStuff(string $someArg) : SomeResult
{
$this->logger->log("Maker called")
$result = $this->next->makeStuff($someArg);
$this->logger->log("Maker return result");
return $result;
}
}
<?php
class DefaultMyStuffMakerSpec extends ObjectSpec
{
public function let(MyStuffMaker $maker, Logger $logger)
{
$this->beConstructedWith($maker, $logger);
}
public function it_makes_stuff(MyStuffMaker $maker, Logger $logger)
{
$maker->makeStuff('stuff')->shouldBeCalled()->willReturn(new SomeResult());
$logger->log(Argument::type('string'))->shouldBeCalledTimes(2);
$this->makeStuff('stuff');
}
}
<?php
interface MyStuffMaker
{
public function makeStuff(string $someArg) : SomeResult
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment