Skip to content

Instantly share code, notes, and snippets.

@shadowhand
Last active August 29, 2015 14:01
Show Gist options
  • Save shadowhand/2b16cc44cc03ca5a1039 to your computer and use it in GitHub Desktop.
Save shadowhand/2b16cc44cc03ca5a1039 to your computer and use it in GitHub Desktop.
Testing side effects with phpspec
<?php
namespace spec\Ushahidi\Usecase\Admin\Tag;
use Ushahidi\Entity\Tag;
use Ushahidi\Tool\Validator;
use Ushahidi\Usecase\Admin\Tag\CreateTagRepository;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
class CreateSpec extends ObjectBehavior
{
/* snip */
function it_does_fill_in_the_slug_and_created_time_is_now($repo, $valid)
{
$tag = new Tag([
'tag' => 'Generate Slug',
'type' => 'status',
'parent_id' => 2,
]);
$valid->check($tag)->shouldBeCalled()->willReturn(true);
$repo->create($tag->tag, 'generate-slug', $tag->type, $tag->parent_id, time())->shouldBeCalled()->willReturn(3);
$this->interact($tag)->shouldReturn(3);
}
}
@jakzal
Copy link

jakzal commented May 16, 2014

If you wanted to continue with your design, you'd have to do sth like:

function it_does_fill_in_the_slug(Tag $tag)
{
    $tag->getTag()->willReturn('Make A Slug');
    $tag->getType()->willReturn('status');

    $tag->setSlug('make-a-slug')->shouldBeCalled();

    $this->interact($tag);
}

However, I think it shouldn't be possible to create a Tag instance without a slug. It's simply an invalid object. Object shouldn't be put in an invalid state. So, it's construction should look more like:

new Tag('My tag', 'my-tag', 'status')

The slug should be generated even before the Tag is created.

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