Skip to content

Instantly share code, notes, and snippets.

@alexlcdee
Last active August 15, 2017 21:33
Show Gist options
  • Save alexlcdee/eb8e38a0b5db79d72bf3107d45ceeef0 to your computer and use it in GitHub Desktop.
Save alexlcdee/eb8e38a0b5db79d72bf3107d45ceeef0 to your computer and use it in GitHub Desktop.
test for delivery date
<?php
namespace App\Components\DeliveryCalculation;
use App\Components\Interfaces\DeliveryMethod\DeliveryMethodInterface;
class Calculation implements CalculationInterface, \JsonSerializable
{
/**
* @var
*/
private $price;
/**
* @var \DateInterval
*/
private $time;
/**
* @var DeliveryMethodInterface
*/
private $deliveryMethod;
/**
* @var \DateTimeImmutable
*/
private $today;
public function __construct(
$price,
\DateInterval $time,
\DateTimeImmutable $today,
DeliveryMethodInterface $deliveryMethod
)
{
$this->price = $price;
$this->time = $time;
$this->deliveryMethod = $deliveryMethod;
$this->today = $today;
}
/**
* @return float
*/
public function getPrice()
{
return $this->price;
}
/**
* @return \DateInterval
*/
public function getDeliveryInterval()
{
return $this->time;
}
/**
* @return DeliveryMethodInterface
*/
public function getDeliveryMethod()
{
return $this->deliveryMethod;
}
/**
* Specify data which should be serialized to JSON
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 5.4.0
*/
public function jsonSerialize()
{
return [
'price' => $this->getPrice(),
'time' => $this->getDeliveryInterval(),
'deliveryMethod' => $this->getDeliveryMethod(),
];
}
/**
* @return \DateTimeImmutable
*/
public function getDeliveryDate()
{
// TODO: Implement getDeliveryDate() method.
}
}
<?php
namespace App\Tests\Components\DeliveryCalculation;
use App\Components\DeliveryCalculation\Calculation;
use App\Components\Interfaces\DeliveryMethod\DeliveryMethodInterface;
use PHPUnit\Framework\TestCase;
class CalculationTest extends TestCase
{
/**
* @param string $today
* @param int $interval
* @param string $valid
* @dataProvider getDates
*/
public function test_getDeliveryDate($today, $interval, $valid)
{
$todayDate = new \DateTimeImmutable($today);
$intervalObject = new \DateInterval("P{$interval}D");
$validDate = new \DateTimeImmutable($valid);
$calculation = new Calculation(
200,
$intervalObject,
$todayDate,
$this->getDeliveryMethod()
);
$this->assertEquals(
$validDate,
$calculation->getDeliveryDate()
);
}
public function getDates()
{
return [
['2017-08-16', 2, '2017-08-18'],
['2017-08-16', 5, '2017-08-23'],
['2017-08-16', 13, '2017-09-04'],
];
}
public function getDeliveryMethod()
{
return $this->getMockBuilder(DeliveryMethodInterface::class)->getMock();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment