2

Lately I'm giving a try to phpspec. It works great, but I have got a problem with testing command handlers. For example in PHPUnit I test it that way:

/**
 * @test
 */
public function it_should_change_an_email()
{
    $this->repository->add($this->employee);

    $this->handler->changeEmail(
        new ChangeEmailCommand(
            $this->employee->username()->username(),
            'new@email.com'
        )
    );

    Asserts::assertEquals(new Email('new@email.com'), $this->employee->email());
}

and setup:

protected function setUp()
{
    $this->repository = new InMemoryEmployeeRepository();
    $this->createEmployee();

    $this->handler = new EmployeeCommandHandler($this->repository);
}

The main point is that this test make assertions on the Employee object to check if CommandHandler is working good. But in phpspec I can't make assertion on different object than the specifying one, in this case I can only make assertion on my CommandHandler. So how I can test a command handler in phpspec?

EDIT

Maybe spies are the way to go:

class EmployeeCommandHandlerSpec extends ObjectBehavior
{
    const USERNAME = 'johnny';

    /** @var EmployeeRepository */
    private $employeeRepository;

    public function let(EmployeeRepository $employeeRepository)
    {
        $this->employeeRepository = $employeeRepository;
        $this->beConstructedWith($employeeRepository);
    }

    public function it_changes_the_employee_email(Employee $employee)
    {
        $this->givenEmployeeExists($employee);

        $this->changeEmail(
            new ChangeEmailCommand(self::USERNAME, 'new@email.com')
        );

        $employee->changeEmail(new Email('new@email.com'))->shouldHaveBeenCalled();
    }

    private function givenEmployeeExists(Employee $employee)
    {
        $this->employeeRepository->employeeWithUsername(new EmployeeUsername(self::USERNAME))
             ->shouldBeCalled()
             ->willReturn($employee);
    }
}    

Employee class I've already speced. So, maybe, in command handler it'll be enough to just check if the method of the Employee has been called. What do you think about it? Am I going in good direction?

tommy
  • 388
  • 2
  • 14

1 Answers1

4

Messaging

Indeed, you shouldn't verify the state, but expect certain interactions between objects. That's what OOP is about afterall - messaging.

The way you've done it in PHPUnit is state verification. It forces you to expose the state as you need to provide a "getter", which is not always desired. What you're interested in is that Employee's email was updated:

$employee->updateEmail(new Email('new@email.com'))->shouldBeCalled();

The same can be achieved with spies if you prefer:

$employee->updateEmail(new Email('new@email.com'))->shouldHaveBeenCalled();

Command/Query Separation

We usually only need to state our expectations against methods that have side effects (command methods from Command/Query separation). We mock them.

Query methods do not need to be mocked, but stubbed. You don't really expect that EmployeeRepository::employeeWithUsername() should be called. Doing so we're making assumptions about implementation which in turn will make refactoring harder. All you need is stubbing it, so if a method is called it returns a result:

$employeeRepository->employeeWithUsername(new EmployeeUsername(self::USERNAME))
    ->willReturn($employee);

Full example

class EmployeeCommandHandlerSpec extends ObjectBehavior
{
    const USERNAME = 'johnny';

    public function let(EmployeeRepository $employeeRepository)
    {
        $this->beConstructedWith($employeeRepository);
    }

    public function it_changes_the_employee_email(
        EmployeeRepository $employees, Employee $employee
    ) {
        $this->givenEmployeeExists($employees, $employee);

        $this->changeEmail(
            new ChangeEmailCommand(self::USERNAME, 'new@email.com')
        );

        $employee->changeEmail(new Email('new@email.com'))->shouldHaveBeenCalled();
    }

    private function givenEmployeeExists(
        EmployeeRepository $employees, Employee $employee
    ) {
        $employees->employeeWithUsername(new EmployeeUsername(self::USERNAME))
             ->willReturn($employee);
    }
}    
Jakub Zalas
  • 35,761
  • 9
  • 93
  • 125
  • I've found out that with mocks if I use any non-mocked method in code then spec will fail. But that's not true for spies. Which one is better? Should I expect that `updateEmail` should be called and don't care about any oher calls which may or may not happen. Or should I be more strict and expect that only that particular method should be calles? – tommy Jun 30 '15 at 12:21
  • I've added one more question to the first post. – tommy Jun 30 '15 at 12:24
  • Until you stub a method call your object will be a dummy - any call will return null. As soon as you start stubbing you need to stub any method that might be called. Spies don't have this feature as they work retrospectively. Please ask new questions as a new StackOverflow questions. Otherwise this content won't be easy to find. – Jakub Zalas Jun 30 '15 at 13:50
  • Ok, I asked a new question here: http://stackoverflow.com/questions/31140741/testing-factory-method-in-command-handler-with-phpspec – tommy Jun 30 '15 at 14:14