159 lines
6.0 KiB
PHP
159 lines
6.0 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace KupShop\ReservationBundle\Tests;
|
|
|
|
use KupShop\ContentBundle\View\Exception\ValidationException;
|
|
use KupShop\DevelopmentBundle\MailArchive;
|
|
use KupShop\KupShopBundle\Context\ContextManager;
|
|
use KupShop\KupShopBundle\Context\CurrencyContext;
|
|
use KupShop\KupShopBundle\Context\LanguageContext;
|
|
use KupShop\OrderingBundle\Util\Order\OrderInfo;
|
|
use KupShop\ProductReservationBundle\Util\ProductReservationUtil;
|
|
use KupShop\ReservationBundle\Util\ReservationUtil;
|
|
|
|
class ReservationTest extends \DatabaseTestCase
|
|
{
|
|
use ReservationTestTrait;
|
|
private ContextManager $contextManager;
|
|
private MailArchive $mailArchive;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
parent::setUp();
|
|
|
|
$this->reservationUtil = $this->get(ReservationUtil::class);
|
|
$this->productReservationUtil = $this->get(ProductReservationUtil::class);
|
|
$this->contextManager = $this->get(ContextManager::class);
|
|
$this->mailArchive = $this->get(MailArchive::class);
|
|
|
|
$dbcfg = \Settings::getDefault();
|
|
|
|
// setup settings for tests
|
|
$dbcfg->reservations = [
|
|
'merge_interval' => 20,
|
|
'delivery_type' => 1,
|
|
];
|
|
}
|
|
|
|
public function testCreateReservation(): void
|
|
{
|
|
$this->createTestReservation(1, 16);
|
|
}
|
|
|
|
/** @dataProvider data_testCreateReservationWithInStoreCheck */
|
|
public function testCreateReservationWithInStoreCheck(int $productId, ?int $variationId, bool $expectException): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
// zakazu objednat zbozi, ktere neni skladem
|
|
$dbcfg->order_availability = \Settings::ORDER_AVAILABILITY_IN_STORE_OR_SUPPLIER_STORE;
|
|
|
|
if ($expectException) {
|
|
// produkt neni skladem na vybrane prodejne a je zakazano objednat zbozi, ktere neni skladem, takze to musi hodit chybu
|
|
$this->expectException(ValidationException::class);
|
|
}
|
|
|
|
$this->createTestReservation($productId, $variationId);
|
|
}
|
|
|
|
public function testCreateReservationWithImmediatelyMerge(): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$dbcfg->reservations['merge_interval'] = 0;
|
|
|
|
$reservation = $this->createTestReservation(1, 16, false);
|
|
|
|
$this->assertNotEmpty($reservation->orderId, 'Pokud je merge interval nastaven na 0, tak musi byt mergnuti provedeno ihned.');
|
|
}
|
|
|
|
public function testCreateReservationWithInvalidProduct(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->createTestReservation(999, null, false);
|
|
}
|
|
|
|
public function testCreateReservationInForeignCurrency(): void
|
|
{
|
|
// vytvorim rezervaci v jazyce sk a v mene EUR
|
|
$reservation = $this->contextManager->activateContexts(
|
|
[
|
|
LanguageContext::class => 'sk',
|
|
CurrencyContext::class => 'EUR',
|
|
],
|
|
fn () => $this->createTestReservation(2, 10)
|
|
);
|
|
|
|
$this->assertEquals('sk', $reservation->language->getId(), 'Rezervace musi byt provedena na SK mutaci');
|
|
$this->assertEquals('EUR', $reservation->currency->getId(), 'Rezervace musi byt provedena v EUR mene');
|
|
$this->assertEquals(86.55, $reservation->getPrice()->getPriceWithVat()->asFloat(), '(2500 - 10% = 2250) / 26 = 86.55 (zaokrouhleno na 5 haleru)');
|
|
}
|
|
|
|
public function testMergeReservationsIntoOrder(): void
|
|
{
|
|
$dbcfg = \Settings::getDefault();
|
|
$dbcfg->reservations['merge_interval'] = 0;
|
|
|
|
$createdOrders = $this->reservationUtil->mergeReservationsIntoOrders();
|
|
|
|
$this->assertCount(2, $createdOrders, 'Musi se vytvorit 2 rezervacni objednavky');
|
|
|
|
foreach ($createdOrders as $order) {
|
|
$this->assertEquals($dbcfg->reservations['delivery_type'], $order->getDeliveryType()->id, 'Musi se nastavit DeliveryType na hodnotu, ktera je nastavena v settingach');
|
|
$this->assertEquals(0, $order->status, 'Objednavka by mela byt ve stavu nova');
|
|
|
|
$this->assertEquals(
|
|
[
|
|
'delivery_firm' => 'Testovací prodejna',
|
|
'delivery_street' => 'Ulice 32',
|
|
'delivery_city' => 'Vrchlabí',
|
|
'delivery_zip' => '10000',
|
|
'delivery_country' => 'CZ',
|
|
],
|
|
[
|
|
'delivery_firm' => $order->delivery_firm,
|
|
'delivery_street' => $order->delivery_street,
|
|
'delivery_city' => $order->delivery_city,
|
|
'delivery_zip' => $order->delivery_zip,
|
|
'delivery_country' => $order->delivery_country,
|
|
],
|
|
'Objednavka musi mit vyplnene dodaci udaje podle prodejny'
|
|
);
|
|
|
|
$this->assertEquals(OrderInfo::ORDER_SOURCE_RESERVATION, $order->source);
|
|
}
|
|
|
|
$productReservations = array_map(fn ($x) => $x['id'], sqlQueryBuilder()
|
|
->select('id')
|
|
->from('product_reservations')
|
|
->execute()->fetchAllAssociative());
|
|
|
|
$this->assertEquals([6], $productReservations, 'V tabulce se skladovyma rezervacema by mely zustat jen zaznamy, ktere nepatrili k rezervacim');
|
|
|
|
$this->assertCount(2, $this->mailArchive->getArchive(), 'Maji se odeslat 2 maily, protoze se vytvorili dve rezervace');
|
|
$this->assertEquals(['test@wpj.cz', 'wpj@wpj.cz'], array_map(fn ($x) => $x['to'], $this->mailArchive->getArchive()), 'Dva maily, kazdy na jinou adresu podle adresy vyplneny u vytvorene rezervace');
|
|
}
|
|
|
|
public function testCreateReservationWithMissingUserData(): void
|
|
{
|
|
$this->expectException(\InvalidArgumentException::class);
|
|
|
|
$this->reservationUtil->createReservation([], 1, 16);
|
|
}
|
|
|
|
public function data_testCreateReservationWithInStoreCheck(): array
|
|
{
|
|
return [
|
|
[1, 16, true],
|
|
[11, 20, true],
|
|
[11, 21, false],
|
|
];
|
|
}
|
|
|
|
protected function getDataSet()
|
|
{
|
|
return $this->getJsonDataSetFromFile();
|
|
}
|
|
}
|