144 lines
3.5 KiB
PHP
144 lines
3.5 KiB
PHP
<?php
|
|
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: ondra
|
|
* Date: 2.11.17
|
|
* Time: 16:07.
|
|
*/
|
|
|
|
namespace KupShop\DevelopmentBundle\Util\Tests;
|
|
|
|
use KupShop\OrderingBundle\Cart;
|
|
|
|
/**
|
|
* Trait CartTestTrait.
|
|
*/
|
|
trait CartTestTrait
|
|
{
|
|
use OrderFromPStateToggleTrait;
|
|
|
|
/** @var \Cart */
|
|
public $cart;
|
|
|
|
/** @var \Order */
|
|
public $order;
|
|
private $userKey = '123456test123456';
|
|
|
|
public function createCart()
|
|
{
|
|
$this->cart = $this->get(Cart::class);
|
|
\Cart::setCartID($this->userKey);
|
|
|
|
$this->visitStep('delivery');
|
|
|
|
return $this;
|
|
}
|
|
|
|
public function setCartUserKey(string $userKey): void
|
|
{
|
|
$this->userKey = $userKey;
|
|
}
|
|
|
|
public function prepareCart()
|
|
{
|
|
$this->createCart();
|
|
$this->setDeliveryType();
|
|
$this->setInvoice();
|
|
|
|
return $this->cart;
|
|
}
|
|
|
|
public function setDeliveryType($id = 1)
|
|
{
|
|
$this->cart->setTransport($id);
|
|
}
|
|
|
|
public function setInvoice()
|
|
{
|
|
$this->cart->updateAddresses([
|
|
'email' => 'test@wpj.cz',
|
|
'name' => 'Wpj',
|
|
'surname' => 'Wpj',
|
|
'street' => 'Wpj 123',
|
|
'city' => 'Vrchlabi',
|
|
'zip' => 12345,
|
|
'country' => 'CZ',
|
|
'phone' => '123456789',
|
|
], null);
|
|
}
|
|
|
|
public function insertProduct($id_product, $id_variation = null, $pieces = 1, $note = '')
|
|
{
|
|
$item = [
|
|
'id_product' => $id_product,
|
|
'id_variation' => $id_variation,
|
|
'pieces' => $pieces,
|
|
'note' => $note,
|
|
];
|
|
|
|
$itemId = $this->cart->addItem($item);
|
|
$this->assertGreaterThan(0, $itemId);
|
|
|
|
return $itemId;
|
|
}
|
|
|
|
public function visitStep($name)
|
|
{
|
|
$this->cart->findNextStep($name);
|
|
$this->cart->actualStep = $name;
|
|
}
|
|
|
|
public function checkOrderPriceIsSameAsCart()
|
|
{
|
|
$this->order = $this->submitOrder();
|
|
|
|
$this->assertEquals($this->cart->totalPricePay->asFloat(), $this->order->total_price->asFloat(), '', 0.01);
|
|
}
|
|
|
|
protected function loginUser($id_user)
|
|
{
|
|
$this->assertNull($this->cart, 'loginUser se musi volat jeste pred vytvorenim kosiku');
|
|
|
|
$user = \User::createFromId($id_user);
|
|
$user->activateUser();
|
|
}
|
|
|
|
public function saveCart()
|
|
{
|
|
$this->cart->save();
|
|
}
|
|
|
|
public function submitOrder(): \Order
|
|
{
|
|
$this->recalculateCart();
|
|
|
|
$this->order = $this->cart->submitOrder();
|
|
|
|
$this->assertInstanceOf(\Order::class, $this->order);
|
|
|
|
return $this->order;
|
|
}
|
|
|
|
public function recalculateCart(): \Decimal
|
|
{
|
|
// je to fuj, ale je to kvuli spravnym cenam... aby se zapocitala i cena DeliveryType
|
|
$this->cart->totalDiscountPrice = \DecimalConstants::zero();
|
|
$this->cart->totalDiscountPriceNoVat = \DecimalConstants::zero();
|
|
$this->cart->totalPricePay = \DecimalConstants::zero();
|
|
$this->cart->totalPricePayNoVat = \DecimalConstants::zero();
|
|
$this->cart->totalPriceNoVat = \DecimalConstants::zero();
|
|
$this->cart->totalPriceWithVat = \DecimalConstants::zero();
|
|
$this->cart->delivery_types = [];
|
|
|
|
// HACK: Donutit to znova spocitat
|
|
$prop = new \ReflectionProperty(\CartBase::class, 'initialized');
|
|
$prop->setAccessible(true);
|
|
$prop->setValue($this->cart, false);
|
|
$this->cart->only_virtual_products = null;
|
|
$this->cart->createFromDB();
|
|
|
|
return $this->cart->totalPricePay;
|
|
}
|
|
}
|