Testing Domain Services

Given the user authentication example from multiple Domain Service implementations, it's extremely beneficial to be able to easily test the service. Typically, however, testing the Template Method implementations can be tricky. As a result, we'll be using a plain password hashing implementation for testing purposes:

class PlainPasswordHashing implements PasswordHashing
{
public function verify($plainPassword, $hash)
{
return $plainPassword === $hash;
}
}

Now we can test all cases in the Domain Service:

class SignUpTest extends PHPUnit_Framework_TestCase
{
private $signUp;
private $userRepository;

protected function setUp()
{
$this->userRepository = new InMemoryUserRepository();
$this->signUp = new SignUp(
$this->userRepository,
new PlainPasswordHashing()
);
}

/**
* @test
* @expectedException InvalidArgumentException
*/
public function itShouldComplainIfTheUserDoesNotExist()
{
$this->signUp->execute('test-username', 'test-password');
}

/**
* @test
* @expectedException BadCredentialsException
*/
public function itShouldTellIfThePasswordDoesNotMatch()
{
$this->userRepository->add(
new User(
'test-username',
'test-password'
)
);

$this->signUp->execute('test-username', 'no-matching-password')
}

/**
* @test
*/
public function itShouldTellIfTheUserMatchesProvidedPassword()
{
$this->userRepository->add(
new User(
'test-username',
'test-password'
)
);

$this->assertInstanceOf(
'DddDomainModelUserUser',
$this->signUp->execute('test-username', 'test-password')
);
}
}
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.135.201.209