Skip to content

Commit 02dfb4f

Browse files
committed
Merge remote-tracking branch 'origin/BUG#AC-6920' into Spartans_246_quality_graphql_mainlinepr
2 parents 9e4b4b2 + fa8be05 commit 02dfb4f

File tree

2 files changed

+182
-3
lines changed

2 files changed

+182
-3
lines changed

app/code/Magento/QuoteGraphQl/Model/Resolver/MaskedCartId.php

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,17 @@
77

88
namespace Magento\QuoteGraphQl\Model\Resolver;
99

10+
use Magento\Framework\Exception\AlreadyExistsException;
1011
use Magento\Framework\Exception\LocalizedException;
1112
use Magento\Framework\Exception\NoSuchEntityException;
1213
use Magento\Framework\GraphQl\Config\Element\Field;
1314
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
1415
use Magento\Framework\GraphQl\Query\ResolverInterface;
1516
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
1617
use Magento\Quote\Model\Quote;
18+
use Magento\Quote\Model\QuoteIdMaskFactory;
1719
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
20+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
1821

1922
/**
2023
* Get cart id from the cart
@@ -24,15 +27,31 @@ class MaskedCartId implements ResolverInterface
2427
/**
2528
* @var QuoteIdToMaskedQuoteIdInterface
2629
*/
27-
private $quoteIdToMaskedQuoteId;
30+
private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId;
31+
32+
/**
33+
* @var QuoteIdMaskFactory
34+
*/
35+
private QuoteIdMaskFactory $quoteIdMaskFactory;
36+
37+
/**
38+
* @var QuoteIdMaskResourceModel
39+
*/
40+
private QuoteIdMaskResourceModel $quoteIdMaskResourceModel;
2841

2942
/**
3043
* @param QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
44+
* @param QuoteIdMaskFactory $quoteIdMaskFactory
45+
* @param QuoteIdMaskResourceModel $quoteIdMaskResourceModel
3146
*/
3247
public function __construct(
33-
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId
48+
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId,
49+
QuoteIdMaskFactory $quoteIdMaskFactory,
50+
QuoteIdMaskResourceModel $quoteIdMaskResourceModel
3451
) {
3552
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId;
53+
$this->quoteIdMaskFactory = $quoteIdMaskFactory;
54+
$this->quoteIdMaskResourceModel = $quoteIdMaskResourceModel;
3655
}
3756

3857
/**
@@ -60,10 +79,33 @@ public function resolve(Field $field, $context, ResolveInfo $info, array $value
6079
private function getQuoteMaskId(int $quoteId): string
6180
{
6281
try {
63-
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
82+
$maskedId =$this->ensureQuoteMaskExist($quoteId);
6483
} catch (NoSuchEntityException $exception) {
6584
throw new GraphQlNoSuchEntityException(__('Current user does not have an active cart.'));
6685
}
6786
return $maskedId;
6887
}
88+
89+
/**
90+
* Create masked id for quote if it's not exists
91+
*
92+
* @param int $quoteId
93+
* @return string
94+
* @throws AlreadyExistsException
95+
*/
96+
private function ensureQuoteMaskExist(int $quoteId): string
97+
{
98+
try {
99+
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
100+
} catch (NoSuchEntityException $e) {
101+
$maskedId = '';
102+
}
103+
if ($maskedId === '') {
104+
$quoteIdMask = $this->quoteIdMaskFactory->create();
105+
$quoteIdMask->setQuoteId($quoteId);
106+
$this->quoteIdMaskResourceModel->save($quoteIdMask);
107+
$maskedId = $this->quoteIdToMaskedQuoteId->execute($quoteId);
108+
}
109+
return $maskedId;
110+
}
69111
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
declare(strict_types=1);
7+
8+
namespace Magento\QuoteGraphQl\Test\Unit\Model\Resolver;
9+
10+
use Magento\Framework\Exception\LocalizedException;
11+
use Magento\Framework\GraphQl\Config\Element\Field;
12+
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
13+
use Magento\GraphQl\Model\Query\Context;
14+
use Magento\Quote\Model\Quote;
15+
use Magento\Quote\Model\QuoteIdMask;
16+
use Magento\Quote\Model\QuoteIdMaskFactory;
17+
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
18+
use Magento\Quote\Model\ResourceModel\Quote\QuoteIdMask as QuoteIdMaskResourceModel;
19+
use Magento\QuoteGraphQl\Model\Resolver\Cart;
20+
use Magento\QuoteGraphQl\Model\Resolver\MaskedCartId;
21+
use PHPUnit\Framework\MockObject\MockObject;
22+
use PHPUnit\Framework\TestCase;
23+
24+
class MaskedCartIdTest extends TestCase
25+
{
26+
/**
27+
* @var MaskedCartId
28+
*/
29+
private MaskedCartId $maskedCartId;
30+
31+
/**
32+
* @var QuoteIdToMaskedQuoteIdInterface|MockObject
33+
*/
34+
private QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId;
35+
36+
/**
37+
* @var \Magento\QuoteGraphQl\Test\Unit\Model\Resolver\QuoteIdMaskFactory|MockObject
38+
*/
39+
private QuoteIdMaskFactory $quoteIdMaskFactory;
40+
41+
/**
42+
* @var QuoteIdMaskResourceModel|MockObject
43+
*/
44+
private QuoteIdMaskResourceModel $quoteIdMaskResourceModelMock;
45+
46+
/**
47+
* @var Field|MockObject
48+
*/
49+
private Field $fieldMock;
50+
51+
/**
52+
* @var ResolveInfo|MockObject
53+
*/
54+
private ResolveInfo $resolveInfoMock;
55+
56+
/**
57+
* @var Context|MockObject
58+
*/
59+
private Context $contextMock;
60+
61+
/**
62+
* @var Quote|MockObject
63+
*/
64+
private Quote $quoteMock;
65+
66+
/**
67+
* @var QuoteIdMask|MockObject
68+
*/
69+
private QuoteIdMask $quoteIdMask;
70+
71+
/**
72+
* @var array
73+
*/
74+
private array $valueMock = [];
75+
76+
protected function setUp(): void
77+
{
78+
$this->fieldMock = $this->createMock(Field::class);
79+
$this->resolveInfoMock = $this->createMock(ResolveInfo::class);
80+
$this->contextMock = $this->createMock(Context::class);
81+
$this->quoteIdToMaskedQuoteId = $this->createPartialMock(
82+
QuoteIdToMaskedQuoteIdInterface::class,
83+
['execute']
84+
);
85+
$this->quoteIdMaskFactory = $this->createPartialMock(
86+
QuoteIdMaskFactory::class,
87+
['create']
88+
);
89+
$this->quoteIdMaskResourceModelMock = $this->getMockBuilder(QuoteIdMaskResourceModel::class)
90+
->disableOriginalConstructor()
91+
->addMethods(
92+
[
93+
'setQuoteId',
94+
]
95+
)
96+
->onlyMethods(['save'])
97+
->getMock();
98+
$this->maskedCartId = new MaskedCartId(
99+
$this->quoteIdToMaskedQuoteId,
100+
$this->quoteIdMaskFactory,
101+
$this->quoteIdMaskResourceModelMock
102+
);
103+
$this->quoteMock = $this->getMockBuilder(Quote::class)
104+
->disableOriginalConstructor()
105+
->getMock();
106+
$this->quoteIdMask = $this->getMockBuilder(QuoteIdMask::class)
107+
->disableOriginalConstructor()
108+
->getMock();
109+
}
110+
111+
public function testResolveWithoutModelInValueParameter(): void
112+
{
113+
$this->expectException(LocalizedException::class);
114+
$this->expectExceptionMessage('"model" value should be specified');
115+
$this->maskedCartId->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock);
116+
}
117+
118+
public function testResolve(): void
119+
{
120+
$this->valueMock = ['model' => $this->quoteMock];
121+
$cartId = 1;
122+
$this->quoteMock
123+
->expects($this->once())
124+
->method('getId')
125+
->willReturn($cartId);
126+
$this->quoteIdMaskFactory
127+
->expects($this->once())
128+
->method('create')
129+
->willReturn($this->quoteIdMask);
130+
$this->quoteIdMask->setQuoteId($cartId);
131+
$this->quoteIdMaskResourceModelMock
132+
->expects($this->once())
133+
->method('save')
134+
->with($this->quoteIdMask);
135+
$this->maskedCartId->resolve($this->fieldMock, $this->contextMock, $this->resolveInfoMock, $this->valueMock);
136+
}
137+
}

0 commit comments

Comments
 (0)