Skip to content

Commit

Permalink
[project-base] fixed GetOrdersAsAuthenticatedCustomerUserTest (#2805)
Browse files Browse the repository at this point in the history
  • Loading branch information
grossmannmartin authored Sep 16, 2023
1 parent d6b00e7 commit 9de323e
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 232 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,244 +4,101 @@

namespace Tests\FrontendApiBundle\Functional\Order;

use App\DataFixtures\Demo\VatDataFixture;
use Shopsys\FrameworkBundle\Component\Translation\Translator;
use App\DataFixtures\Demo\OrderDataFixture;
use Tests\FrontendApiBundle\Test\GraphQlWithLoginTestCase;

class GetOrdersAsAuthenticatedCustomerUserTest extends GraphQlWithLoginTestCase
{
use OrderTestTrait;

public function testGetAllCustomerUserOrders(): void
{
$this->markTestSkipped('This test is skipped because of the issue with rounding');
private const EXPECTED_ORDER_IDS = [4, 5, 3, 1, 2, 6];

/**
* @dataProvider getOrdersDataProvider
* @param array $queryVariables
* @param int|null $offsetInExpected
* @param int|null $lengthInExpected
*/
public function testGetAllCustomerUserOrders(
array $queryVariables,
?int $offsetInExpected,
?int $lengthInExpected,
): void {
$response = $this->getResponseContentForGql(__DIR__ . '/graphql/getOrders.graphql', $queryVariables);

$responseData = $this->getResponseDataForGraphQlType($response, 'orders');

// @phpstan-ignore-next-line Test is skipped
foreach ($this->getOrdersDataProvider() as $datasetIndex => $dataSet) {
[$query, $expectedOrdersData] = $dataSet;
$expectedUserOrders = $this->getExpectedUserOrders($offsetInExpected, $lengthInExpected);

$graphQlType = 'orders';
$response = $this->getResponseContentForQuery($query);
$responseData = $this->getResponseDataForGraphQlType($response, $graphQlType);
$this->assertArrayHasKey('edges', $responseData);
$this->assertSameSize(
$expectedUserOrders,
$responseData['edges'],
);

$message = sprintf('Dataset index: %d', $datasetIndex);
foreach ($responseData['edges'] as $orderIndex => $edge) {
$orderMessage = sprintf(
'Hint: check data and sort of order with ID #%d',
self::EXPECTED_ORDER_IDS[$orderIndex + $offsetInExpected],
);

$this->assertArrayHasKey('edges', $responseData, $message);
$this->assertCount(count($expectedOrdersData), $responseData['edges'], $message);

foreach ($responseData['edges'] as $orderIndex => $edge) {
$orderMessage = $message . sprintf(' [Order index: %d]', $orderIndex);
$this->assertArrayHasKey('node', $edge, $orderMessage);
$this->assertArrayHasKey('node', $edge, $orderMessage);

$expectedOrderData = array_shift($expectedOrdersData);
$this->assertArrayHasKey('status', $edge['node'], $orderMessage);
$this->assertSame($expectedOrderData['status'], $edge['node']['status'], $orderMessage);
$expectedOrderData = array_shift($expectedUserOrders);
$this->assertArrayHasKey('status', $edge['node'], $orderMessage);
$this->assertSame($expectedOrderData['status'], $edge['node']['status'], $orderMessage);

$this->assertArrayHasKey('totalPrice', $edge['node'], $orderMessage);
$this->assertArrayHasKey('priceWithVat', $edge['node']['totalPrice'], $orderMessage);
$this->assertSame($expectedOrderData['priceWithVat'], $edge['node']['totalPrice']['priceWithVat'], $orderMessage);
}
$this->assertArrayHasKey('totalPrice', $edge['node'], $orderMessage);
$this->assertArrayHasKey('priceWithVat', $edge['node']['totalPrice'], $orderMessage);
$this->assertSame($expectedOrderData['priceWithVat'], $edge['node']['totalPrice']['priceWithVat'], $orderMessage);
}
}

/**
* @return iterable
* @phpstan-ignore-next-line Test is skipped
*/
private function getOrdersDataProvider(): iterable
public function getOrdersDataProvider(): iterable
{
return [
[
$this->getOrdersWithoutFilterQuery(),
$this->getExpectedUserOrders(),
],
[
$this->getFirstOrdersQuery(2),
array_slice($this->getExpectedUserOrders(), 0, 2),
],
[
$this->getFirstOrdersQuery(1),
array_slice($this->getExpectedUserOrders(), 0, 1),
],
[
$this->getLastOrdersQuery(1),
array_slice($this->getExpectedUserOrders(), 5, 1),
],
[
$this->getLastOrdersQuery(2),
array_slice($this->getExpectedUserOrders(), 4, 2),
],
];
}
// all orders
yield [[], null, null];

/**
* @return string
*/
private function getOrdersWithoutFilterQuery(): string
{
return '
{
orders {
edges {
node {
status
totalPrice {
priceWithVat
}
}
}
}
}
';
}
// first 2 orders
yield [['first' => 2], 0, 2];

/**
* @param int $numberOfOrders
* @return string
*/
private function getFirstOrdersQuery(int $numberOfOrders): string
{
return '
{
orders (first:' . $numberOfOrders . ') {
edges {
node {
status
totalPrice {
priceWithVat
}
}
}
}
}
';
}
// first 1 order
yield [['first' => 1], 0, 1];

/**
* @param int $numberOfOrders
* @return string
*/
private function getLastOrdersQuery(int $numberOfOrders): string
{
return '
{
orders (last:' . $numberOfOrders . ') {
edges {
node {
status
totalPrice {
priceWithVat
}
}
}
}
}
';
// last 1 order
yield [['last' => 1], 5, 1];

//last 2 orders
yield [['last' => 2], 4, 2];
}

/**
* @param int|null $offset
* @param int|null $length
* @return array
*/
private function getExpectedUserOrders(): array
private function getExpectedUserOrders(?int $offset, ?int $length): array
{
$firstDomainLocale = $this->getLocaleForFirstDomain();
$domainId = $this->domain->getId();
/** @var \Shopsys\FrameworkBundle\Model\Pricing\Vat\Vat $vatHigh */
$vatHigh = $this->getReferenceForDomain(VatDataFixture::VAT_HIGH, $domainId);
/** @var \Shopsys\FrameworkBundle\Model\Pricing\Vat\Vat $vatZero */
$vatZero = $this->getReferenceForDomain(VatDataFixture::VAT_ZERO, $domainId);

$expectedOrderItems1 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('2891.74', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('100', $vatZero)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('200', $vatHigh)],
];
$expectedOrder1 = [
'status' => t('In Progress', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems1,
)->getPriceWithVat()->getAmount(),
];

$expectedOrderItems2 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('8173.55', $vatHigh, 8)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('17842.98', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('2891.74', $vatHigh, 2)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('0', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('0', $vatZero)],
];
$expectedOrder2 = [
'status' => t('Done', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems2,
)->getPriceWithVat()->getAmount(),
];

$expectedOrderItems3 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('263.64', $vatHigh, 6)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('4.96', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('100', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('50', $vatZero)],
];
$expectedOrder3 = [
'status' => t('New [adjective]', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems3,
)->getPriceWithVat()->getAmount(),
];

$expectedOrderItems4 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('1314.05', $vatHigh, 2)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('818.18', $vatHigh, 3)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('0', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('0', $vatHigh)],
];
$expectedOrder4 = [
'status' => t('Done', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems4,
)->getPriceWithVat()->getAmount(),
];

$expectedOrderItems5 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('437.19', $vatHigh, 2)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('180.17', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('429.75', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('3.31', $vatHigh, 5)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('100', $vatZero)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('0', $vatHigh)],
];
$expectedOrder5 = [
'status' => t('New [adjective]', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems5,
)->getPriceWithVat()->getAmount(),
];

$expectedOrderItems6 = [
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('98.35', $vatHigh, 2)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('19743.80', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('3.31', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('90.08', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('164.46', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('437.19', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('200', $vatHigh)],
['totalPrice' => $this->getSerializedPriceConvertedToDomainDefaultCurrency('100', $vatZero)],
];
$expectedOrder6 = [
'status' => t('New [adjective]', [], Translator::DATA_FIXTURES_TRANSLATION_DOMAIN, $firstDomainLocale),
'priceWithVat' => self::getOrderTotalPriceByExpectedOrderItems(
$expectedOrderItems6,
)->getPriceWithVat()->getAmount(),
];

return [
$expectedOrder1,
$expectedOrder2,
$expectedOrder3,
$expectedOrder4,
$expectedOrder5,
$expectedOrder6,
];
$ordersArray = [];

foreach (self::EXPECTED_ORDER_IDS as $orderId) {
/** @var \App\Model\Order\Order $order */
$order = $this->getReference(OrderDataFixture::ORDER_PREFIX . $orderId);

$ordersArray[] = [
'status' => $order->getStatus()->getName(),
'priceWithVat' => $order->getTotalPriceWithVat()->getAmount(),
];
}

if ($offset !== null) {
return array_slice($ordersArray, $offset, $length);
}

return $ordersArray;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class GetOrdersAsUnauthenticatedCustomerUserTest extends GraphQlTestCase
{
public function testGetAllCustomerUserOrders(): void
{
$response = $this->getResponseContentForQuery($this->getOrdersWithoutFilterQuery());
$response = $this->getResponseContentForGql(__DIR__ . '/graphql/getOrders.graphql');
$this->assertResponseContainsArrayOfErrors($response);
$errors = $this->getErrorsFromResponse($response);

Expand All @@ -21,25 +21,4 @@ public function testGetAllCustomerUserOrders(): void
$errors[0]['message'],
);
}

/**
* @return string
*/
private function getOrdersWithoutFilterQuery(): string
{
return '
{
orders {
edges {
node {
status
totalPrice {
priceWithVat
}
}
}
}
}
';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
query getOrders ($first: Int, $last: Int) {
orders (first: $first, last: $last) {
edges {
node {
status
totalPrice {
priceWithVat
}
}
}
}
}

0 comments on commit 9de323e

Please sign in to comment.