Skip to content
Permalink
Browse files

Episode 003 Spies

Adding logging functionality to the DrivingLicenceGenerator using Spy Test Doubles to ensure
the code interact with the LoggerInterface dependency as expected.
  • Loading branch information...
braddle committed May 19, 2019
1 parent 7c15a7c commit 3ce7b759194a722207e0e11579306b2c13031037
@@ -3,17 +3,30 @@
namespace Braddle;
use Psr\Log\LoggerInterface;
class DrivingLicenceGenerator
{
private $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function generateNumber(LicenceApplicant $applicant)
{
if ($applicant->getAge() < 17) {
$this->logger->notice("Under age application user: " . $applicant->getId());
throw new InvalidDriverException(
"Applicant is too young"
);
}
if ($applicant->holdsLicence()) {
$this->logger->notice("duplicate application user: " . $applicant->getId());
throw new InvalidDriverException(
"Cannot hold more than one licence"
);
@@ -6,4 +6,5 @@ interface LicenceApplicant
{
public function getAge() : int;
public function holdsLicence() : bool;
public function getId(): int;
}
@@ -9,15 +9,40 @@
class DrivingLicenceGeneratorTest extends TestCase
{
private $logger;
protected function setUp()
{
parent::setUp();
$this->logger = new SpyLogger();
$this->generator = new DrivingLicenceGenerator($this->logger);
}
public function testUnderAgeApplicantCannotGenerateLicence()
{
$this->expectException(InvalidDriverException::class);
$this->expectExceptionMessage("Applicant is too young");
$applicant = new UnderAgeApplicant();
$generator = new DrivingLicenceGenerator();
$generator->generateNumber($applicant);
$this->generator->generateNumber($applicant);
}
public function testUnderAgeApplicationsAreLogged()
{
$applicant = new UnderAgeApplicant();
try {
$this->generator->generateNumber($applicant);
} catch (InvalidDriverException $e) {
}
$this->assertEquals(1, $this->logger->noticeCalledCount);
$this->assertEquals("Under age application user: 123", $this->logger->noticeLastMessage);
}
public function testLicenceHolderCannotGenerateLicence()
@@ -27,15 +52,28 @@ public function testLicenceHolderCannotGenerateLicence()
$applicant = new LicenceHolderApplicant();
$generator = new DrivingLicenceGenerator();
$generator->generateNumber($applicant);
$this->generator->generateNumber($applicant);
}
public function testValidApplicantCanGenerateLicence()
public function testLicenceHolderAttemtsLogged()
{
$applicant = new ValidApplicant();
$applicant = new LicenceHolderApplicant();
try {
$this->generator->generateNumber($applicant);
} catch (InvalidDriverException $e) {
$generator = new DrivingLicenceGenerator();
$generator->generateNumber($applicant);
}
$this->assertEquals(1, $this->logger->noticeCalledCount);
$this->assertEquals("duplicate application user: 123", $this->logger->noticeLastMessage);
}
// public function testValidApplicantCanGenerateLicence()
// {
// $applicant = new ValidApplicant();
//
// $generator = new DrivingLicenceGenerator();
// $generator->generateNumber($applicant);
// }
}
@@ -16,4 +16,9 @@ public function holdsLicence(): bool
{
return true;
}
public function getId(): int
{
return 123;
}
}
@@ -0,0 +1,142 @@
<?php
declare(strict_types=1);
namespace Braddle\Test;
use Psr\Log\LoggerInterface;
class SpyLogger implements LoggerInterface
{
/**
* System is unusable.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function emergency($message, array $context = [])
{
// TODO: Implement emergency() method.
}
/**
* Action must be taken immediately.
*
* Example: Entire website down, database unavailable, etc. This should
* trigger the SMS alerts and wake you up.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function alert($message, array $context = [])
{
// TODO: Implement alert() method.
}
/**
* Critical conditions.
*
* Example: Application component unavailable, unexpected exception.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function critical($message, array $context = [])
{
// TODO: Implement critical() method.
}
/**
* Runtime errors that do not require immediate action but should typically
* be logged and monitored.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function error($message, array $context = [])
{
// TODO: Implement error() method.
}
/**
* Exceptional occurrences that are not errors.
*
* Example: Use of deprecated APIs, poor use of an API, undesirable things
* that are not necessarily wrong.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function warning($message, array $context = [])
{
// TODO: Implement warning() method.
}
public $noticeCalledCount = 0;
public $noticeLastMessage = "";
/**
* Normal but significant events.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function notice($message, array $context = [])
{
$this->noticeCalledCount++;
$this->noticeLastMessage = $message;
}
/**
* Interesting events.
*
* Example: User logs in, SQL logs.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function info($message, array $context = [])
{
// TODO: Implement info() method.
}
/**
* Detailed debug information.
*
* @param string $message
* @param array $context
*
* @return void
*/
public function debug($message, array $context = [])
{
// TODO: Implement debug() method.
}
/**
* Logs with an arbitrary level.
*
* @param mixed $level
* @param string $message
* @param array $context
*
* @return void
*/
public function log($level, $message, array $context = [])
{
// TODO: Implement log() method.
}
}
@@ -16,4 +16,9 @@ public function holdsLicence(): bool
{
return false;
}
public function getId(): int
{
return 123;
}
}
@@ -16,4 +16,9 @@ public function holdsLicence(): bool
{
return false;
}
public function getId(): int
{
return 123;
}
}

0 comments on commit 3ce7b75

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