使用 phpspec 在命令处理程序中测试工厂方法
Testing factory method in command handler with phpspec
如何测试实际上是工厂方法的静态方法:
public function hireEmployee(HireEmployeeCommand $command)
{
$username = new EmployeeUsername($command->getUsername());
$firstName = $command->getFirstName();
$lastName = $command->getLastName();
$phoneNumber = new PhoneNumber($command->getPhoneNumber());
$email = new Email($command->getEmail());
$role = new EmployeeRole($command->getRole());
if ($role->isAdmin()) {
$employee = Employee::hireAdmin($username, $firstName, $lastName, $phoneNumber, $email);
} else {
$employee = Employee::hirePollster($username, $firstName, $lastName, $phoneNumber, $email);
}
$this->employeeRepository->add($employee);
}
这里我不能模拟 Employee
对象,但我可以模拟预期雇员的 EmployeeRepository::add()
方法,然后我再次检查雇员的状态:
public function it_hires_an_admin()
{
$this->employeeRepository
->add(Argument::that(/** callback for checking state of Employee object */))
->shouldBeCalled();
$this->hireEmployee(
new HireEmployeeCommand(self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com', EmployeeRole::ROLE_ADMIN)
);
}
我知道我再次模拟了存储库而不是存根。但在这里,我更感兴趣的是员工将被添加到存储库中,而不是如何创建它。因此,我应该模拟存储库,但我不应该关心 Employee
的状态(没有 Argument::that()
)?看起来很合理,但我不能确定创建的 Employee 是否正确。
您真的不需要存根或模拟您的实体或值对象,因为它们在规范中没有表现出任何行为:
public function it_hires_an_admin()
{
$this->employeeRepository
->add(Argument::is(
Employee::hireAdmin(self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com')
))
->shouldBeCalled();
$this->hireEmployee(
new HireEmployeeCommand(
self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com', EmployeeRole::ROLE_ADMIN
)
);
}
如何测试实际上是工厂方法的静态方法:
public function hireEmployee(HireEmployeeCommand $command)
{
$username = new EmployeeUsername($command->getUsername());
$firstName = $command->getFirstName();
$lastName = $command->getLastName();
$phoneNumber = new PhoneNumber($command->getPhoneNumber());
$email = new Email($command->getEmail());
$role = new EmployeeRole($command->getRole());
if ($role->isAdmin()) {
$employee = Employee::hireAdmin($username, $firstName, $lastName, $phoneNumber, $email);
} else {
$employee = Employee::hirePollster($username, $firstName, $lastName, $phoneNumber, $email);
}
$this->employeeRepository->add($employee);
}
这里我不能模拟 Employee
对象,但我可以模拟预期雇员的 EmployeeRepository::add()
方法,然后我再次检查雇员的状态:
public function it_hires_an_admin()
{
$this->employeeRepository
->add(Argument::that(/** callback for checking state of Employee object */))
->shouldBeCalled();
$this->hireEmployee(
new HireEmployeeCommand(self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com', EmployeeRole::ROLE_ADMIN)
);
}
我知道我再次模拟了存储库而不是存根。但在这里,我更感兴趣的是员工将被添加到存储库中,而不是如何创建它。因此,我应该模拟存储库,但我不应该关心 Employee
的状态(没有 Argument::that()
)?看起来很合理,但我不能确定创建的 Employee 是否正确。
您真的不需要存根或模拟您的实体或值对象,因为它们在规范中没有表现出任何行为:
public function it_hires_an_admin()
{
$this->employeeRepository
->add(Argument::is(
Employee::hireAdmin(self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com')
))
->shouldBeCalled();
$this->hireEmployee(
new HireEmployeeCommand(
self::USERNAME, 'John', 'Snow', '123456789', 'john@snow.com', EmployeeRole::ROLE_ADMIN
)
);
}