Skip to content
Permalink
Browse files

Episode 004 Mocks

Adding functionality to generate licence number for valid applicants.
We use a mock of the RandomNumbersGenerator interface to give us control of the dependecy whilst we test.
  • Loading branch information...
braddle committed May 21, 2019
1 parent 3ce7b75 commit fddedcd3aed1602ac7a6d4fbf62caafbe633388e
@@ -7,13 +7,15 @@
class DrivingLicenceGenerator
{
private $logger;
private $randomNumbersGenerator;
public function __construct(LoggerInterface $logger)
{
public function __construct(
LoggerInterface $logger,
RandomNumbersGenerator $randomNumbersGenerator
) {
$this->logger = $logger;
$this->randomNumbersGenerator = $randomNumbersGenerator;
}
public function generateNumber(LicenceApplicant $applicant)
@@ -31,5 +33,13 @@ public function generateNumber(LicenceApplicant $applicant)
"Cannot hold more than one licence"
);
}
$licence = $applicant->getInitials() .
$applicant->getDateOfBirth()->format("dmY");
$numberOfDigits = 15 - strlen($licence);
$numberOfDigits = ($numberOfDigits < 4) ? 4 : $numberOfDigits;
return $licence . $this->randomNumbersGenerator->generate($numberOfDigits);
}
}
@@ -7,4 +7,6 @@ interface LicenceApplicant
public function getAge() : int;
public function holdsLicence() : bool;
public function getId(): int;
public function getDateOfBirth() : \DateTime;
public function getInitials() : string;
}
@@ -0,0 +1,8 @@
<?php
namespace Braddle;
interface RandomNumbersGenerator
{
public function generate(int $numberOfDigits) : string;
}
@@ -17,8 +17,12 @@ protected function setUp()
parent::setUp();
$this->logger = new SpyLogger();
$this->random = new MockRandomNumbersGenerator();
$this->generator = new DrivingLicenceGenerator($this->logger);
$this->generator = new DrivingLicenceGenerator(
$this->logger,
$this->random
);
}
public function testUnderAgeApplicantCannotGenerateLicence()
@@ -69,11 +73,47 @@ public function testLicenceHolderAttemtsLogged()
$this->assertEquals("duplicate application user: 123", $this->logger->noticeLastMessage);
}
// public function testValidApplicantCanGenerateLicence()
// {
// $applicant = new ValidApplicant();
//
// $generator = new DrivingLicenceGenerator();
// $generator->generateNumber($applicant);
// }
public function testValidApplicantCanGenerateLicence()
{
$applicant = new ValidApplicant("MDB");
$this->random->mockGenerate([4 => "0123"]);
$licenceNumber = $this->generator->generateNumber($applicant);
$this->assertEquals(
"MDB110719990123",
$licenceNumber
);
}
public function testLicenceNumberAreAtleast15Characters()
{
$applicant1 = new ValidApplicant("M");
$applicant2 = new ValidApplicant("MD");
$applicant4 = new ValidApplicant("MDBF");
$this->random->mockGenerate(
[
4 => "0123",
5 => "01234",
6 => "012345",
]
);
$this->assertEquals(
"M11071999012345",
$this->generator->generateNumber($applicant1)
);
$this->assertEquals(
"MD1107199901234",
$this->generator->generateNumber($applicant2)
);
$this->assertEquals(
"MDBF110719990123",
$this->generator->generateNumber($applicant4)
);
}
}
@@ -21,4 +21,14 @@ public function getId(): int
{
return 123;
}
public function getDateOfBirth(): \DateTime
{
return new \DateTime();
}
public function getInitials(): string
{
return "DEF";
}
}
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
namespace Braddle\Test;
use Braddle\RandomNumbersGenerator;
class MockRandomNumbersGenerator implements RandomNumbersGenerator
{
private $responses;
public function mockGenerate(array $callAndRespond)
{
$this->responses = $callAndRespond;
}
public function generate(int $numberOfDigits): string
{
return $this->responses[$numberOfDigits];
}
}
@@ -21,4 +21,14 @@ public function getId(): int
{
return 123;
}
public function getDateOfBirth(): \DateTime
{
return new \DateTime();
}
public function getInitials(): string
{
return "ABC";
}
}
@@ -7,6 +7,14 @@
class ValidApplicant implements LicenceApplicant
{
private $initials;
public function __construct(string $initials)
{
$this->initials = $initials;
}
public function getAge(): int
{
return 21;
@@ -21,4 +29,14 @@ public function getId(): int
{
return 123;
}
public function getDateOfBirth(): \DateTime
{
return new \DateTime("11-07-1999 00:00:00");
}
public function getInitials(): string
{
return $this->initials;
}
}

0 comments on commit fddedcd

Please sign in to comment.
You can’t perform that action at this time.