Skip to content

Instantly share code, notes, and snippets.

@davidyell
Last active May 31, 2018 08:29
Show Gist options
  • Save davidyell/2498236846a8d71c4f84e46ff7eae483 to your computer and use it in GitHub Desktop.
Save davidyell/2498236846a8d71c4f84e46ff7eae483 to your computer and use it in GitHub Desktop.
{
"name": "david/telesales-window",
"type": "project",
"require": {
"phpunit/phpunit": "^7.1"
}
}
<?php
require 'vendor/autoload.php';
class TelesalesWindow
{
private $now;
/**
* TelesalesWindow constructor.
*
* @param \DateTimeImmutable $now
* @throws \Exception
*/
public function __construct(DateTimeImmutable $now)
{
$this->now = $now;
}
/**
* @return bool
* @throws \Exception
*/
public function isWindowOpen()
{
$day = (int)$this->now->format('N');
$hour = (int)$this->now->format('G');
if ($day === 1 && $hour >= 9) {
return true;
} else if ($day >= 2 && $day <= 5) {
return true;
} else if ($day === 6 && $hour <= 8) {
return true;
}
return false;
}
}
use PHPUnit\Framework\TestCase;
class TelesalesWindowTest extends TestCase
{
/**
* @return array
* @throws \Exception
*/
public function providerData()
{
return [
'Monday 6am' => [
new DateTimeImmutable('2018-05-14T06:00:00', new DateTimeZone('Europe/London')),
false
],
'Monday 9am' => [
new DateTimeImmutable('2018-05-14T09:00:00', new DateTimeZone('Europe/London')),
true
],
'Tuesday 4pm' => [
new DateTimeImmutable('2018-05-15T14:00:00', new DateTimeZone('Europe/London')),
true
],
'Wednesday 5pm' => [
new DateTimeImmutable('2018-05-16T17:00:00', new DateTimeZone('Europe/London')),
true
],
'Saturday 7am' => [
new DateTimeImmutable('2018-05-19T07:00:00', new DateTimeZone('Europe/London')),
true
],
'Saturday 8am' => [
new DateTimeImmutable('2018-05-19T08:00:00', new DateTimeZone('Europe/London')),
true
],
'Saturday 10am' => [
new DateTimeImmutable('2018-05-19T10:00:00', new DateTimeZone('Europe/London')),
false
],
'Sunday 11pm' => [
new DateTimeImmutable('2018-05-20T23:00:00', new DateTimeZone('Europe/London')),
false
],
];
}
/**
* @dataProvider providerData
* @param \DateTimeImmutable $dateTime
* @param $expected
* @throws \Exception
*/
public function testIsWindowOpen(DateTimeImmutable $dateTime, $expected)
{
$window = new TelesalesWindow($dateTime);
$result = $window->isWindowOpen();
$this->assertEquals($expected, $result);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment