diff --git a/src/Exception/CustomerStateException.php b/src/Exception/CustomerStateException.php new file mode 100644 index 00000000..861e62ef --- /dev/null +++ b/src/Exception/CustomerStateException.php @@ -0,0 +1,9 @@ + */ -abstract class AbstractAction implements \JsonSerializable, ActionInterface +abstract class AbstractAction implements ActionInterface { /** @var int */ protected $id; @@ -168,9 +167,9 @@ public function setFinished(): void $this->state = ActionState::finished(); } - public function isFinished(): ?bool + public function isNotActive(): ?bool { - return $this->state === null ? null : $this->state->isFinished(); + return $this->state === null ? null : $this->state->isNotActive(); } /** diff --git a/src/action/ActionFactory.php b/src/action/ActionFactory.php index 6162725f..0b6cf681 100644 --- a/src/action/ActionFactory.php +++ b/src/action/ActionFactory.php @@ -17,12 +17,18 @@ */ class ActionFactory implements ActionFactoryInterface { - /** - * Creates action object. - * @return Action - */ - public function create(ActionCreationDto $dto) + public function create(ActionCreationDto $dto): AbstractAction { - return new Action($dto->id, $dto->type, $dto->target, $dto->quantity, $dto->customer, $dto->time, $dto->sale, $dto->state, $dto->parent); + return new Action( + $dto->id, + $dto->type, + $dto->target, + $dto->quantity, + $dto->customer, + $dto->time, + $dto->sale, + $dto->state, + $dto->parent, + ); } } diff --git a/src/action/ActionFactoryInterface.php b/src/action/ActionFactoryInterface.php index 93650e4c..ab63d669 100644 --- a/src/action/ActionFactoryInterface.php +++ b/src/action/ActionFactoryInterface.php @@ -17,9 +17,5 @@ */ interface ActionFactoryInterface { - /** - * Creates action object. - * @return Action - */ - public function create(ActionCreationDto $dto); + public function create(ActionCreationDto $dto): AbstractAction; } diff --git a/src/action/ActionInterface.php b/src/action/ActionInterface.php index 4662c1b0..1cf08807 100644 --- a/src/action/ActionInterface.php +++ b/src/action/ActionInterface.php @@ -71,7 +71,7 @@ public function getSale(): ?SaleInterface; /** * Returns null if the action state is not set. */ - public function isFinished(): ?bool; + public function isNotActive(): ?bool; public function getParent(): ?ActionInterface; diff --git a/src/action/ActionState.php b/src/action/ActionState.php index 5dfbbb49..4d2bb38b 100644 --- a/src/action/ActionState.php +++ b/src/action/ActionState.php @@ -17,51 +17,78 @@ */ class ActionState { - const STATE_NEW = 'new'; - const STATE_FINISHED = 'finished'; - const STATE_FAILED = 'failed'; + private const STATE_NEW = 'new'; - /** @var string */ - protected $state; + private const STATE_FINISHED = 'finished'; - private function __construct(string $state = self::STATE_NEW) + private const STATE_FAILED = 'failed'; + + private const STATE_PREMATURE = 'premature'; + + private const STATE_FUTURE = 'future'; + + private const STATE_CANCELED = 'cancelled'; + + private function __construct(protected string $state = self::STATE_NEW) { - $this->state = $state; } - public function getName() + public function getName(): string { return $this->state; } - public function isNew() + public function isNew(): bool { return $this->state === self::STATE_NEW; } - public function isFinished() + public function isNotActive(): bool { - return $this->state === self::STATE_FINISHED; + return !$this->isNew(); } - public static function new() + public static function new(): self { return new self(self::STATE_NEW); } - public static function finished() + public static function finished(): self { return new self(self::STATE_FINISHED); } - public static function failed() + public static function failed(): self { return new self(self::STATE_FAILED); } - public static function fromString(string $name) + public static function premature(): self + { + return new self(self::STATE_PREMATURE); + } + + public static function future(): self + { + return new self(self::STATE_FUTURE); + } + + public static function canceled(): self + { + return new self(self::STATE_CANCELED); + } + + public static function fromString(string $name): self { - foreach ([self::STATE_NEW, self::STATE_FINISHED, self::STATE_FAILED] as $state) { + $allowedStates = [ + self::STATE_NEW, + self::STATE_FINISHED, + self::STATE_FAILED, + self::STATE_PREMATURE, + self::STATE_FUTURE, + self::STATE_CANCELED, + ]; + foreach ($allowedStates as $state) { if ($state === $name) { return new self($state); } @@ -69,4 +96,9 @@ public static function fromString(string $name) throw new \Exception("wrong action state '$name'"); } + + public function equals(ActionState $other): bool + { + return $this->state === $other->getName(); + } } diff --git a/src/bill/BillFactory.php b/src/bill/BillFactory.php index aa89afd3..725a44ce 100644 --- a/src/bill/BillFactory.php +++ b/src/bill/BillFactory.php @@ -17,11 +17,7 @@ */ class BillFactory implements BillFactoryInterface { - /** - * Creates bill object. - * @return Bill - */ - public function create(BillCreationDto $dto) + public function create(BillCreationDto $dto): BillInterface { return new Bill( $dto->id, diff --git a/src/bill/BillFactoryInterface.php b/src/bill/BillFactoryInterface.php index f5598ebb..7061081e 100644 --- a/src/bill/BillFactoryInterface.php +++ b/src/bill/BillFactoryInterface.php @@ -17,9 +17,5 @@ */ interface BillFactoryInterface { - /** - * Creates bill object. - * @return Bill - */ - public function create(BillCreationDto $dto); + public function create(BillCreationDto $dto): BillInterface; } diff --git a/src/charge/ChargeFactory.php b/src/charge/ChargeFactory.php index 70a9b9f8..1bb01148 100644 --- a/src/charge/ChargeFactory.php +++ b/src/charge/ChargeFactory.php @@ -17,11 +17,7 @@ */ class ChargeFactory implements ChargeFactoryInterface { - /** - * Creates charge object. - * @return Charge - */ - public function create(ChargeCreationDto $dto) + public function create(ChargeCreationDto $dto): ChargeInterface { return new Charge( $dto->id, diff --git a/src/charge/ChargeFactoryInterface.php b/src/charge/ChargeFactoryInterface.php index c9aef9e0..e49a75f5 100644 --- a/src/charge/ChargeFactoryInterface.php +++ b/src/charge/ChargeFactoryInterface.php @@ -17,9 +17,5 @@ */ interface ChargeFactoryInterface { - /** - * Creates charge object. - * @return Charge - */ - public function create(ChargeCreationDto $dto); + public function create(ChargeCreationDto $dto): ChargeInterface; } diff --git a/src/customer/Customer.php b/src/customer/Customer.php index 4b02c668..96abaf97 100755 --- a/src/customer/Customer.php +++ b/src/customer/Customer.php @@ -37,11 +37,14 @@ class Customer implements CustomerInterface */ protected $sellers = []; - public function __construct($id, $login, CustomerInterface $seller = null) + protected ?CustomerState $state = null; + + public function __construct($id, $login, CustomerInterface $seller = null, ?CustomerState $state = null) { $this->id = $id; $this->login = $login; $this->seller = $seller; + $this->state = $state; } public function getId() @@ -73,6 +76,26 @@ public function getSeller() return $this->seller; } + /** + * {@inheritdoc} + */ + public function getState(): ?CustomerState + { + return $this->state; + } + + public function setState(CustomerState $state): self + { + $this->state = $state; + + return $this; + } + + public function isDeleted(): bool + { + return CustomerState::isDeleted($this); + } + public static function fromArray(array $info) { if (!empty($info['seller_id']) && !empty($info['seller'])) { @@ -81,7 +104,12 @@ public static function fromArray(array $info) $seller = null; } - return new static($info['id'], $info['login'], $seller); + return new static( + $info['id'], + $info['login'], + $seller, + isset($info['state']) ? CustomerState::fromString($info['state']) : null, + ); } public function jsonSerialize(): array diff --git a/src/customer/CustomerFactory.php b/src/customer/CustomerFactory.php index 1333a530..d11df89f 100644 --- a/src/customer/CustomerFactory.php +++ b/src/customer/CustomerFactory.php @@ -17,11 +17,7 @@ */ class CustomerFactory implements CustomerFactoryInterface { - /** - * Creates customer object. - * @return Customer - */ - public function create(CustomerCreationDto $dto) + public function create(CustomerCreationDto $dto): CustomerInterface { return new Customer($dto->id, $dto->login, $dto->seller); } diff --git a/src/customer/CustomerFactoryInterface.php b/src/customer/CustomerFactoryInterface.php index da92a7c2..dba621bf 100644 --- a/src/customer/CustomerFactoryInterface.php +++ b/src/customer/CustomerFactoryInterface.php @@ -17,9 +17,5 @@ */ interface CustomerFactoryInterface { - /** - * Creates customer object. - * @return Customer - */ - public function create(CustomerCreationDto $dto); + public function create(CustomerCreationDto $dto): CustomerInterface; } diff --git a/src/customer/CustomerInterface.php b/src/customer/CustomerInterface.php index d121d8c4..11bc3805 100644 --- a/src/customer/CustomerInterface.php +++ b/src/customer/CustomerInterface.php @@ -35,4 +35,14 @@ public function getLogin(); * @return static|null */ public function getSeller(); + + /** + * Get Customer state. + * @return null|CustomerState + */ + public function getState(): ?CustomerState; + + public function setState(CustomerState $state): self; + + public function isDeleted(): bool; } diff --git a/src/customer/CustomerState.php b/src/customer/CustomerState.php new file mode 100644 index 00000000..ffbad0b7 --- /dev/null +++ b/src/customer/CustomerState.php @@ -0,0 +1,67 @@ +state; + } + + public static function isDeleted(CustomerInterface $customer): bool + { + return $customer->getState()?->getName() === self::DELETED; + } + + public static function deleted(): CustomerState + { + return new self(self::DELETED); + } + + public static function blocked(): CustomerState + { + return new self(self::BLOCKED); + } + + public static function new(): CustomerState + { + return new self(self::NEW); + } + + public static function ok(): CustomerState + { + return new self(self::OK); + } + + public static function fromString(string $name): self + { + $allowedStates = [ + self::BLOCKED, + self::DELETED, + self::NEW, + self::OK, + ]; + foreach ($allowedStates as $state) { + if ($state === $name) { + return new self($state); + } + } + + throw new CustomerStateException("wrong customer state '$name'"); + } +} diff --git a/src/order/Calculator.php b/src/order/Calculator.php index 15d2ee57..c5d91667 100644 --- a/src/order/Calculator.php +++ b/src/order/Calculator.php @@ -102,7 +102,7 @@ public function calculatePrice(PriceInterface $price, ActionInterface $action): $charges = [$charge]; } - if ($action->isFinished()) { + if ($action->isNotActive()) { foreach ($charges as $charge) { $charge->setFinished(); } diff --git a/src/plan/Plan.php b/src/plan/Plan.php index 6f79dabf..f85ebb48 100755 --- a/src/plan/Plan.php +++ b/src/plan/Plan.php @@ -83,6 +83,11 @@ public function getId() return $this->id; } + public function setId(int $id): void + { + $this->id = $id; + } + /** * @return string */ diff --git a/src/plan/PlanFactory.php b/src/plan/PlanFactory.php index 9a99055a..5755d1e5 100644 --- a/src/plan/PlanFactory.php +++ b/src/plan/PlanFactory.php @@ -17,16 +17,12 @@ */ class PlanFactory implements PlanFactoryInterface { - /** - * Creates plan object. - * @return Plan - */ - public function create(PlanCreationDto $dto) + public function create(PlanCreationDto $dto): PlanInterface { return $this->createAnyPlan($dto); } - protected function createAnyPlan(PlanCreationDto $dto, string $class = null) + protected function createAnyPlan(PlanCreationDto $dto, string $class = null): PlanInterface { $class = $class ?? Plan::class; diff --git a/src/plan/PlanFactoryInterface.php b/src/plan/PlanFactoryInterface.php index 32e26ac1..4f697b9a 100644 --- a/src/plan/PlanFactoryInterface.php +++ b/src/plan/PlanFactoryInterface.php @@ -17,9 +17,5 @@ */ interface PlanFactoryInterface { - /** - * Creates plan object. - * @return Plan - */ - public function create(PlanCreationDto $dto); + public function create(PlanCreationDto $dto): PlanInterface; } diff --git a/src/plan/PlanInterface.php b/src/plan/PlanInterface.php index d3c01ecb..6982f3d5 100644 --- a/src/plan/PlanInterface.php +++ b/src/plan/PlanInterface.php @@ -28,6 +28,8 @@ interface PlanInterface extends EntityInterface */ public function getId(); + public function setId(int $id): void; + /** * Globally unique ID. * @return int|string @@ -38,6 +40,7 @@ public function getUniqueId(); * @return PriceInterface[] */ public function getPrices(): array; + public function hasPrices(): bool; /** @@ -45,9 +48,13 @@ public function hasPrices(): bool; * @throws CannotReassignException when prices are already set */ public function setPrices(array $prices): void; + public function getSeller(): ?CustomerInterface; + public function getName(): string; + public function setName(string $name): void; + public function getType(): ?TypeInterface; public function getParentId(): ?int; public function setParentId(int $parentId): void; diff --git a/src/plan/PlanRepositoryInterface.php b/src/plan/PlanRepositoryInterface.php index 735a2236..e1ca90d7 100644 --- a/src/plan/PlanRepositoryInterface.php +++ b/src/plan/PlanRepositoryInterface.php @@ -42,4 +42,6 @@ public function findByIds(array $ids); * @throws EntityNotFoundException */ public function getById(int $id): PlanInterface; + + public function save(PlanInterface $action): void; } diff --git a/src/price/PriceFactory.php b/src/price/PriceFactory.php index 02e794d8..eb65351b 100644 --- a/src/price/PriceFactory.php +++ b/src/price/PriceFactory.php @@ -42,9 +42,6 @@ public function __construct(array $types = [], $defaultClass = null) } - /** - * Creates price object. - */ public function create(PriceCreationDto $dto): PriceInterface { $class = $this->findClassForTypes([ diff --git a/src/sale/Sale.php b/src/sale/Sale.php index 538f0afb..02f0d9fc 100755 --- a/src/sale/Sale.php +++ b/src/sale/Sale.php @@ -105,7 +105,7 @@ public function getCloseTime(): ?DateTimeImmutable return $this->closeTime; } - public function close(DateTimeImmutable $closeTime): void + public function close(DateTimeImmutable $closeTime): SaleInterface { if ($this->closeTime !== null) { throw new InvariantException('Sale is already closed'); @@ -116,6 +116,8 @@ public function close(DateTimeImmutable $closeTime): void } $this->closeTime = $closeTime; + + return $this; } public function setId($id) diff --git a/src/sale/SaleFactory.php b/src/sale/SaleFactory.php index fcd725f2..8c368796 100644 --- a/src/sale/SaleFactory.php +++ b/src/sale/SaleFactory.php @@ -17,11 +17,7 @@ */ class SaleFactory implements SaleFactoryInterface { - /** - * Creates sale object. - * @return Sale - */ - public function create(SaleCreationDto $dto) + public function create(SaleCreationDto $dto): SaleInterface { $sale = new Sale($dto->id, $dto->target, $dto->customer, $dto->plan, $dto->time, $dto->data); diff --git a/src/sale/SaleFactoryInterface.php b/src/sale/SaleFactoryInterface.php index 25c777ed..8b357c39 100644 --- a/src/sale/SaleFactoryInterface.php +++ b/src/sale/SaleFactoryInterface.php @@ -17,9 +17,5 @@ */ interface SaleFactoryInterface { - /** - * Creates sale object. - * @return Sale - */ - public function create(SaleCreationDto $dto); + public function create(SaleCreationDto $dto): SaleInterface; } diff --git a/src/sale/SaleInterface.php b/src/sale/SaleInterface.php index ef2727b1..ce97734f 100644 --- a/src/sale/SaleInterface.php +++ b/src/sale/SaleInterface.php @@ -71,7 +71,7 @@ public function getData(): ?array; * @throws InvariantException * @throws ConstraintException */ - public function close(DateTimeImmutable $closeTime): void; + public function close(DateTimeImmutable $closeTime): SaleInterface; public function cancelClosing(): void; } diff --git a/src/target/TargetFactory.php b/src/target/TargetFactory.php index 652ab7b8..906c989f 100644 --- a/src/target/TargetFactory.php +++ b/src/target/TargetFactory.php @@ -17,10 +17,7 @@ */ class TargetFactory implements TargetFactoryInterface { - /** - * @return Target - */ - public function create(TargetCreationDto $dto) + public function create(TargetCreationDto $dto): TargetInterface { return new Target($dto->id, $dto->type, $dto->name); } diff --git a/src/target/TargetFactoryInterface.php b/src/target/TargetFactoryInterface.php index 5bb16169..1e2744e8 100644 --- a/src/target/TargetFactoryInterface.php +++ b/src/target/TargetFactoryInterface.php @@ -17,10 +17,7 @@ */ interface TargetFactoryInterface { - /** - * @return Target - */ - public function create(TargetCreationDto $dto); + public function create(TargetCreationDto $dto): TargetInterface; /** * Returns class that represents target with $type. diff --git a/src/tools/Factory.php b/src/tools/Factory.php index 95be85ad..aeef2b15 100644 --- a/src/tools/Factory.php +++ b/src/tools/Factory.php @@ -11,10 +11,21 @@ namespace hiqdev\php\billing\tools; use DateTimeImmutable; +use hiqdev\php\billing\action\ActionInterface; +use hiqdev\php\billing\bill\BillInterface; +use hiqdev\php\billing\charge\ChargeInterface; +use hiqdev\php\billing\customer\CustomerInterface; use hiqdev\php\billing\Exception\UnknownEntityException; +use hiqdev\php\billing\plan\PlanInterface; +use hiqdev\php\billing\price\PriceInterface; +use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\TargetCollection; +use hiqdev\php\billing\target\TargetInterface; +use hiqdev\php\billing\type\TypeInterface; use hiqdev\php\units\Quantity; +use hiqdev\php\units\QuantityInterface; use hiqdev\php\units\Unit; +use hiqdev\php\units\UnitInterface; use Money\Currency; use Money\Money; @@ -25,21 +36,21 @@ */ class Factory implements FactoryInterface { - private $entities = []; + private array $entities = []; - private $factories = []; + private array $factories; public function __construct(array $factories) { $this->factories = $factories; } - public function getMoney($data) + public function getMoney($data): Money { return $this->get('money', $data); } - public function getSums($data) + public function getSums($data): array { $res = []; foreach ($data as $key => $value) { @@ -49,7 +60,7 @@ public function getSums($data) return $res; } - public function parseMoney($str) + public function parseMoney(string $str): array { [$amount, $currency] = explode(' ', $str); @@ -59,22 +70,22 @@ public function parseMoney($str) ]; } - public function createMoney($data) + public function createMoney($data): Money { return new Money($data['amount'], new Currency(strtoupper($data['currency']))); } - public function getCurrency($data) + public function getCurrency($data): Currency { return new Currency($data); } - public function getQuantity($data) + public function getQuantity($data): QuantityInterface { return $this->get('quantity', $data); } - public function parseQuantity($str) + public function parseQuantity(string $str): array { [$quantity, $unit] = explode(' ', $str); @@ -84,32 +95,32 @@ public function parseQuantity($str) ]; } - public function createQuantity($data) + public function createQuantity($data): QuantityInterface { return Quantity::create($data['unit'], $data['quantity']); } - public function getUnit($data) + public function getUnit($data): UnitInterface { return $this->get('unit', $data); } - public function createUnit($data) + public function createUnit($data): UnitInterface { return Unit::create($data['name']); } - public function getType($data) + public function getType($data): TypeInterface { return $this->get('type', $data); } - public function getTime($data) + public function getTime($data): DateTimeImmutable { return $this->get('time', $data); } - public function createTime($data) + public function createTime($data): DateTimeImmutable { if (empty($data['date'])) { return new DateTimeImmutable(); @@ -123,12 +134,12 @@ public function createTime($data) return new DateTimeImmutable($str); } - public function getTargets($data) + public function getTargets($data): TargetCollection { return $this->get('targets', $data); } - public function createTargets($data) + public function createTargets(array $data): TargetCollection { $targets = []; foreach ($data as $one) { @@ -138,17 +149,21 @@ public function createTargets($data) return new TargetCollection($targets); } - public function getTarget($data) + public function getTarget($data): TargetInterface { return $this->get('target', $data); } - public function getAction($data) + public function getAction($data): ActionInterface { return $this->get('action', $data); } - public function getCharges($data) + /** + * @param array $data + * @return ChargeInterface[] + */ + public function getCharges(array $data): array { $res = []; foreach ($data as $key => $row) { @@ -158,36 +173,43 @@ public function getCharges($data) return $res; } - public function getCharge($data) + public function getCharge($data): ChargeInterface { return $this->get('charge', $data); } - public function getPrice($data) + public function getPrice($data): PriceInterface { return $this->get('price', $data); } - public function getBill($data) + public function getBill($data): BillInterface { return $this->get('bill', $data); } - public function getPlan($data) + public function getPlan($data): PlanInterface { return $this->get('plan', $data); } - public function getSale($data) + public function getSale($data): SaleInterface { return $this->get('sale', $data); } - public function getCustomer($data) + public function getCustomer($data): CustomerInterface { return $this->get('customer', $data); } + /** + * @deprecated In the future the function will be private. + * Please use a specific method instead (e.g., getSale) + * @param string $entity + * @param $data + * @return mixed + */ public function get(string $entity, $data) { if (is_scalar($data)) { @@ -270,9 +292,7 @@ public function create(string $entity, $data) throw new FactoryNotFoundException($entity); } - $factory = $this->factories[$entity]; - - return $factory->create($this->createDto($entity, $data)); + return $this->factories[$entity]->create($this->createDto($entity, $data)); } public function createDto(string $entity, array $data) diff --git a/src/tools/FactoryInterface.php b/src/tools/FactoryInterface.php index f39f0546..7e7c31de 100644 --- a/src/tools/FactoryInterface.php +++ b/src/tools/FactoryInterface.php @@ -1,5 +1,5 @@ - */ @@ -20,4 +35,44 @@ interface FactoryInterface * Create billing object by entity name and data. */ public function create(string $entity, $data); + + public function getSale($data): SaleInterface; + + public function getAction($data): ActionInterface; + + public function getPlan($data): PlanInterface; + + public function getCustomer($data): CustomerInterface; + + public function getCharge($data): ChargeInterface; + + public function getPrice($data): PriceInterface; + + public function getBill($data): BillInterface; + + public function getTarget($data): TargetInterface; + + public function getType($data): TypeInterface; + + public function getUnit($data): UnitInterface; + + public function createUnit($data): UnitInterface; + + public function getQuantity($data): QuantityInterface; + + public function createQuantity($data): QuantityInterface; + + public function parseQuantity(string $str): array; + + public function getMoney($data): Money; + + public function createMoney($data): Money; + + public function parseMoney(string $str): array; + + public function getCurrency($data): Currency; + + public function createTime($data): DateTimeImmutable; + + public function getTime($data): DateTimeImmutable; } diff --git a/src/type/TypeFactory.php b/src/type/TypeFactory.php index 5184356e..d3ae7470 100644 --- a/src/type/TypeFactory.php +++ b/src/type/TypeFactory.php @@ -15,10 +15,7 @@ */ class TypeFactory implements TypeFactoryInterface { - /** - * @return Type - */ - public function create(TypeCreationDto $dto) + public function create(TypeCreationDto $dto): TypeInterface { return new Type($dto->id, $dto->name); } diff --git a/src/type/TypeFactoryInterface.php b/src/type/TypeFactoryInterface.php index 55710965..2c91a6da 100644 --- a/src/type/TypeFactoryInterface.php +++ b/src/type/TypeFactoryInterface.php @@ -17,8 +17,5 @@ */ interface TypeFactoryInterface { - /** - * @return Type - */ - public function create(TypeCreationDto $dto); + public function create(TypeCreationDto $dto): TypeInterface; } diff --git a/tests/behat/bootstrap/BillingContext.php b/tests/behat/bootstrap/BillingContext.php index 38711a9a..d8793c73 100644 --- a/tests/behat/bootstrap/BillingContext.php +++ b/tests/behat/bootstrap/BillingContext.php @@ -27,6 +27,8 @@ class BillingContext extends BaseContext protected $saleTime; + protected $saleCloseTime; + protected $bill; protected $charges = []; @@ -164,8 +166,19 @@ public function recreatePlan($plan) public function sale($target, $plan, $time): void { $this->saleTime = $this->prepareTime($time); - $this->builder->buildSale($target, $plan, $this->saleTime); + $this->sale = $this->builder->buildSale($target, $plan, $this->saleTime); + } + + /** + * @Given /sale target (\S+) by plan (\S+) at (\S+) and close at (\S+)/ + */ + public function saleWithCloseTime($target, $plan, $time, $closeTime): void + { + $this->saleTime = $this->prepareTime($time); + $this->saleCloseTime = $this->prepareTime($closeTime); + $this->builder->buildSale($target, $plan, $this->saleTime, $this->saleCloseTime); } + /** * @When /^sale close is requested for target "([^"]*)" at "([^"]*)", assuming current time is "([^"]*)"$/ */ diff --git a/tests/behat/bootstrap/BuilderInterface.php b/tests/behat/bootstrap/BuilderInterface.php index 0c97de6c..8f561ba6 100644 --- a/tests/behat/bootstrap/BuilderInterface.php +++ b/tests/behat/bootstrap/BuilderInterface.php @@ -28,7 +28,9 @@ public function buildTarget(string $name); public function recreatePlan(string $name); - public function buildSale(string $target, string $plan, string $time); + public function buildSale(string $target, string $planName, string $time, ?string $closeTime = null); + + public function createSale(string $target, string $planName, string $time, ?string $closeTime = null); public function buildPurchase(string $target, string $plan, string $time, ?array $uses = []); diff --git a/tests/behat/bootstrap/FactoryBasedBuilder.php b/tests/behat/bootstrap/FactoryBasedBuilder.php index f9fa76be..923bac82 100644 --- a/tests/behat/bootstrap/FactoryBasedBuilder.php +++ b/tests/behat/bootstrap/FactoryBasedBuilder.php @@ -13,10 +13,12 @@ use hiqdev\billing\hiapi\plan\PlanFactory; use hiqdev\billing\hiapi\tests\support\order\SimpleCalculator; +use hiqdev\php\billing\action\ActionInterface; use hiqdev\php\billing\price\EnumPrice; use hiqdev\php\billing\price\PriceFactory; use hiqdev\php\billing\price\RatePrice; use hiqdev\php\billing\price\SinglePrice; +use hiqdev\php\billing\sale\SaleInterface; use hiqdev\php\billing\target\Target; use hiqdev\php\billing\tests\support\order\SimpleBilling; use hiqdev\php\billing\tests\support\tools\SimpleFactory; @@ -32,11 +34,12 @@ class FactoryBasedBuilder implements BuilderInterface private $plan; - private $sale; + private SaleInterface $sale; private $prices = []; - private $actions = []; + /** @var ActionInterface[] */ + private array $actions = []; private $factory; @@ -121,14 +124,20 @@ public function recreatePlan(string $name) $plan->setPrices($this->prices); } - public function buildSale(string $target, $plan, string $time = null) + public function buildSale(string $target, $planName, string $time = null, ?string $closeTime = null) + { + return $this->createSale($target, $planName, $time, $closeTime); + } + + public function createSale(string $target, $planName, string $time = null, ?string $closeTime = null) { $this->time = $time; - $this->sale = $this->factory->get('sale', array_filter([ + $this->sale = $this->factory->getSale(array_filter([ 'customer' => $this->customer, 'target' => $target, - 'plan' => $plan, + 'plan' => $planName, 'time' => $time, + 'closeTime' => $closeTime, ])); return $this->sale; @@ -162,7 +171,7 @@ public function buildPurchase(string $target, string $plan, string $time, ?array 'type' => 'monthly,cdn_traf95_max', 'quantity' => '1 items', 'target' => $target, - 'initial_uses' => $uses + 'initial_uses' => $uses, ]); } @@ -191,15 +200,15 @@ public function performAction(array $data) $this->getBilling()->perform($action); } - public function buildAction(array $data) + public function buildAction(array $data): ActionInterface { $data['time'] = $data['time'] ?? $this->time; $data['customer'] = $data['customer'] ?? $this->customer; if (!empty($data['targets'])) { - $data['target'] = $this->factory->get('targets', $data['targets']); + $data['target'] = $this->factory->getTargets($data['targets']); } - return $this->factory->get('action', $data); + return $this->factory->getAction($data); } public function findBills(array $data): array diff --git a/tests/support/plan/SimplePlanRepository.php b/tests/support/plan/SimplePlanRepository.php index 09255d07..55b6f2f4 100644 --- a/tests/support/plan/SimplePlanRepository.php +++ b/tests/support/plan/SimplePlanRepository.php @@ -51,4 +51,9 @@ public function getById(int $id): PlanInterface { throw new \Exception('not implemented'); } + + public function save(PlanInterface $action): void + { + throw new \Exception('not implemented'); + } } diff --git a/tests/support/tools/SimpleFactory.php b/tests/support/tools/SimpleFactory.php index 19f63ef6..d3b09dcc 100644 --- a/tests/support/tools/SimpleFactory.php +++ b/tests/support/tools/SimpleFactory.php @@ -26,7 +26,7 @@ class SimpleFactory extends Factory { public function __construct(array $factories = []) { - return parent::__construct(array_merge(self::simpleFactories(), $factories)); + parent::__construct(array_merge(self::simpleFactories(), $factories)); } public static function simpleFactories(): array