dongxiaoxing3058 2015-06-27 07:36
浏览 18
已采纳

使用phpspec测试命令处理程序

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?

  • 写回答

1条回答 默认 最新

  • dsfs1233 2015-06-28 19:02
    关注

    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);
        }
    }    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在