Skip to content

Commit

Permalink
Implemented Usage
Browse files Browse the repository at this point in the history
  • Loading branch information
SilverFire authored and hiqsol committed Sep 24, 2021
1 parent 6e710f3 commit a80202b
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 3 deletions.
51 changes: 51 additions & 0 deletions src/usage/Usage.php
@@ -0,0 +1,51 @@
<?php
declare(strict_types=1);

namespace hiqdev\php\billing\usage;

use DateTimeImmutable;
use hiqdev\php\billing\target\TargetInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\Quantity;

/**
* Class Usage represents a consumption of some metered resource $type at $time
* by the $target with the given $amount.
*
* @author Dmytro Naumenko <d.naumenko.a@gmail.com>
*/
class Usage implements UsageInterface
{
private TargetInterface $target;
private DateTimeImmutable $time;
private TypeInterface $type;
private Quantity $amount;

public function __construct(TargetInterface $target, DateTimeImmutable $time, TypeInterface $type, Quantity $amount)
{
$this->target = $target;
$this->time = $time;
$this->type = $type;
$this->amount = $amount;
}

public function target(): TargetInterface
{
return $this->target;
}

public function time(): DateTimeImmutable
{
return $this->time;
}

public function type(): TypeInterface
{
return $this->type;
}

public function amount(): Quantity
{
return $this->amount;
}
}
20 changes: 20 additions & 0 deletions src/usage/UsageInterface.php
@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);

namespace hiqdev\php\billing\usage;

use DateTimeImmutable;
use hiqdev\php\billing\target\TargetInterface;
use hiqdev\php\billing\type\TypeInterface;
use hiqdev\php\units\Quantity;

interface UsageInterface
{
public function target(): TargetInterface;

public function time(): DateTimeImmutable;

public function type(): TypeInterface;

public function amount(): Quantity;
}
14 changes: 14 additions & 0 deletions src/usage/UsageRecorderInterface.php
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace hiqdev\php\billing\usage;

use hiqdev\php\billing\Exception\RuntimeException;

interface UsageRecorderInterface
{
/**
* @throws RuntimeException when unable to record the passed usage
*/
public function record(Usage $usage): void;
}
36 changes: 35 additions & 1 deletion tests/behat/bootstrap/BillingContext.php
Expand Up @@ -10,6 +10,7 @@

namespace hiqdev\php\billing\tests\behat\bootstrap;

use Behat\Gherkin\Node\TableNode;
use BehatExpectException\ExpectException;
use DateTimeImmutable;
use hiqdev\php\billing\bill\BillInterface;
Expand Down Expand Up @@ -129,14 +130,33 @@ public function sale($target, $plan, $time): void
}

/**
* @Given /purchase target (\S+) by plan (\S+) at (.+)$/
* @Given /purchase target (\S+) by plan (\S+) at (\S+)$/
*/
public function purchaseTarget(string $target, string $plan, string $time): void
{
$time = $this->prepareTime($time);
$this->builder->buildPurchase($target, $plan, $time);
}

/**
* @Given /^purchase target "([^"]*)" by plan "([^"]*)" at "([^"]*)" with the following initial uses:$/
*/
public function purchaseTargetWithInitialUses(string $target, string $plan, string $time, TableNode $usesTable): void
{
$time = $this->prepareTime($time);
$uses = array_map(static function (array $row) {
return [
'type' => $row['type'],
'unit' => $row['unit'],
'amount' => $row['amount'],
];
}, $usesTable->getColumnsHash());

$this->mayFail(
fn() => $this->builder->buildPurchase($target, $plan, $time, $uses)
);
}

/**
* @Given /resource consumption for (\S+) is +(\S+) (\S+) for target (\S+) at (.+)$/
*/
Expand Down Expand Up @@ -413,4 +433,18 @@ public function flushEntitiesCache()
{
$this->builder->flushEntitiesCache();
}

/**
* @Given /^target "([^"]*)" has the following uses:$/
*/
public function targetHasTheFollowingUses(string $target, TableNode $usesTable)
{
foreach ($usesTable->getColumnsHash() as $row) {
$uses = $this->builder->findUsage($row['time'], $target, $row['type']);
Assert::assertCount(1, $uses);

$use = reset($uses);
Assert::assertEquals($row['amount'], $use['total']);
}
}
}
4 changes: 3 additions & 1 deletion tests/behat/bootstrap/BuilderInterface.php
Expand Up @@ -30,7 +30,7 @@ public function recreatePlan(string $name);

public function buildSale(string $target, string $plan, string $time);

public function buildPurchase(string $target, string $plan, string $time);
public function buildPurchase(string $target, string $plan, string $time, ?array $uses = []);

public function findBills(array $data): array;

Expand All @@ -48,4 +48,6 @@ public function targetChangePlan(string $target, string $planName, string $date,
public function findHistoricalSales(array $params);

public function flushEntitiesCache(): void;

public function findUsage(string $time, string $targetName, string $typeName): array;
}
8 changes: 7 additions & 1 deletion tests/behat/bootstrap/FactoryBasedBuilder.php
Expand Up @@ -155,13 +155,14 @@ public function setAction(string $type, int $amount, string $unit, string $targe
]);
}

public function buildPurchase(string $target, string $plan, string $time)
public function buildPurchase(string $target, string $plan, string $time, ?array $uses = [])
{
$this->performAction([
'sale' => $this->buildSale($target, $plan, $time),
'type' => 'monthly,cdn_traf95_max',
'quantity' => '1 items',
'target' => $target,
'initial_uses' => $uses
]);
}

Expand Down Expand Up @@ -266,4 +267,9 @@ public function flushEntitiesCache(): void
{
$this->factory->clearEntitiesCache();
}

public function findUsage(string $time, string $targetName, string $typeName): array
{
throw new RuntimeException('Not implemented yet');
}
}

0 comments on commit a80202b

Please sign in to comment.