Skip to content

Instantly share code, notes, and snippets.

@emslade
Created December 8, 2010 12:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save emslade/733208 to your computer and use it in GitHub Desktop.
Save emslade/733208 to your computer and use it in GitHub Desktop.
<?php
/*
phpunit DateFuTest.php
*/
error_reporting(E_ALL);
require 'PHPUnit/Autoload.php';
/**
* Method names need improvement
*/
class DateFu
{
public function getEndTime($startTime, $period)
{
return $startTime + $period;
}
/**
* @param $time integer Time in seconds
* @param $startTime integer Time period commences
*/
public function isBetween($time, $startTime, $period)
{
$endTime = $this->getEndTime($startTime, $period);
if ($time >= $startTime && $time < $endTime) {
return true;
}
return false;
}
/**
* Calculates if a time is within a promotional period
*
* @param $currentTime integer Time in seconds
* @param $promotionalPeriodStartTime integer Start time of promotional period in seconds
* @param $dayOfPromotion integer Day of promotion
*/
public function inPromotionalPeriod($currentTime, $promotionalPeriodStartTime, $dayOfPromotion)
{
//todo period shouldn't be hardcoded
$period = 86400;
$startTime = $promotionalPeriodStartTime + ($dayOfPromotion * $period);
if ($this->isBetween($currentTime, $startTime, $period)) {
return true;
}
return false;
}
}
class DateFuTest extends PHPUnit_Framework_TestCase
{
protected $dateFu;
protected $period;
protected $promotionalStartTime;
protected function setUp()
{
$this->dateFu = new DateFu();
$this->period = 60 * 60 * 24;
$this->promotionalStartTime = mktime(12, 0, 0, 12, 13, 2010); // 13/12/2010 12:00:00
}
public function testInPromotionalPeriodDay1()
{
$time = mktime(12, 1, 0, 12, 13, 2010); // 13/12/2010 12:01:00
$dayOfPromotion = 0;
$result = $this->dateFu->inPromotionalPeriod($time, $this->promotionalStartTime, $dayOfPromotion);
$this->assertTrue($result);
}
public function testInPromotionalPeriodDay4()
{
$time = mktime(11, 59, 59, 12, 17, 2010); // 17/12/2010 11:59:59
$dayOfPromotion = 3;
$result = $this->dateFu->inPromotionalPeriod($time, $this->promotionalStartTime, $dayOfPromotion);
$this->assertTrue($result);
}
public function testNotInPromotionalPeriodDay5()
{
$time = mktime(13, 00, 00, 12, 19, 2010); // 19/12/2010 11:59:59
$dayOfPromotion = 5;
$result = $this->dateFu->inPromotionalPeriod($time, $this->promotionalStartTime, $dayOfPromotion);
$this->assertFalse($result);
}
public function testTimeBetweenPeriodReturnsTrue()
{
$time = mktime(12, 59, 0, 12, 9, 2010); // 9/12/2010 12:59:00
$startTime = mktime(13, 0, 0, 12, 8, 2010); // 8/12/2010 13:00:00
$result = $this->dateFu->isBetween($time, $startTime, $this->period);
$this->assertTrue($result, sprintf('%s is between %s - %s', date('r', $time), date('r', $startTime), date('r', $this->dateFu->getEndTime($startTime, $this->period))));
}
public function testTimeEarlierThanPeriodReturnsFalse()
{
$time = mktime(12, 59, 0, 12, 7, 2010); // 7/12/2010 12:59:00
$startTime = mktime(13, 0, 0, 12, 8, 2010); // 8/12/2010 13:00:00
$result = $this->dateFu->isBetween($time, $startTime, $this->period);
$this->assertFalse($result, sprintf('%s is between %s - %s', date('r', $time), date('r', $startTime), date('r', $this->dateFu->getEndTime($startTime, $this->period))));
}
public function testTimeLaterThanPeriodReturnsFalse()
{
$time = mktime(12, 59, 0, 12, 20, 2010); // 20/12/2010 12:59:00
$startTime = mktime(13, 0, 0, 12, 8, 2010); // 8/12/2010 13:00:00
$result = $this->dateFu->isBetween($time, $startTime, $this->period);
$this->assertFalse($result, sprintf('%s is between %s - %s', date('r', $time), date('r', $startTime), date('r', $this->dateFu->getEndTime($startTime, $this->period))));
}
public function testTimeExactlyOneSecondBeforePeriodReturnsFalse()
{
$time = mktime(12, 59, 59, 12, 8, 2010); // 8/12/2010 12:59:59
$startTime = mktime(13, 0, 0, 12, 8, 2010); // 8/12/2010 13:00:00
$result = $this->dateFu->isBetween($time, $startTime, $this->period);
$this->assertFalse($result, sprintf('%s is between %s - %s', date('r', $time), date('r', $startTime), date('r', $this->dateFu->getEndTime($startTime, $this->period))));
}
public function testTimeExactlyOnEndTimeReturnsFalse()
{
$time = mktime(13, 00, 0, 12, 9, 2010); // 9/12/2010 13:00:00
$startTime = mktime(13, 0, 0, 12, 8, 2010); // 8/12/2010 13:00:00
$result = $this->dateFu->isBetween($time, $startTime, $this->period);
$this->assertFalse($result, sprintf('%s is between %s - %s', date('r', $time), date('r', $startTime), date('r', $this->dateFu->getEndTime($startTime, $this->period))));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment