Skip to content

Commit 94f223b

Browse files
committed
MC-19235: Quote improvement in TestFramework
1 parent 3a7a953 commit 94f223b

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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\TestFramework\Quote\Model;
9+
10+
use Magento\Framework\Api\SearchCriteriaBuilder;
11+
use Magento\Quote\Api\CartRepositoryInterface;
12+
use Magento\Quote\Api\Data\CartInterface;
13+
14+
/**
15+
* Search and return quote by reserved order id.
16+
*/
17+
class GetQuoteByReservedOrderId
18+
{
19+
/** @var SearchCriteriaBuilder */
20+
private $searchCriteriaBuilder;
21+
22+
/** @var CartRepositoryInterface */
23+
private $cartRepository;
24+
25+
/**
26+
* @param SearchCriteriaBuilder $searchCriteriaBuilder
27+
* @param CartRepositoryInterface $cartRepository
28+
*/
29+
public function __construct(SearchCriteriaBuilder $searchCriteriaBuilder, CartRepositoryInterface $cartRepository)
30+
{
31+
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
32+
$this->cartRepository = $cartRepository;
33+
}
34+
35+
/**
36+
* Return quote by reserved order id.
37+
*
38+
* @param string $reservedOrderId
39+
* @return CartInterface|null
40+
*/
41+
public function execute(string $reservedOrderId): ?CartInterface
42+
{
43+
$searchCriteria = $this->searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)->create();
44+
$quote = $this->cartRepository->getList($searchCriteria)->getItems();
45+
46+
return array_shift($quote);
47+
}
48+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
use Magento\Quote\Api\CartRepositoryInterface;
9+
use Magento\Quote\Model\Quote;
10+
use Magento\Quote\Model\Quote\Address;
11+
use Magento\Store\Api\StoreRepositoryInterface;
12+
use Magento\TestFramework\Helper\Bootstrap;
13+
use Magento\TestFramework\ObjectManager;
14+
use Magento\Customer\Api\CustomerRepositoryInterface;
15+
16+
/** @var ObjectManager $objectManager */
17+
$objectManager = Bootstrap::getObjectManager();
18+
19+
$customer = $objectManager->get(CustomerRepositoryInterface::class)->get('[email protected]');
20+
$store = $objectManager->get(StoreRepositoryInterface::class)->get('secondstore');
21+
22+
$addressData = include __DIR__ . '/../../Customer/Fixtures/address_data.php';
23+
/** @var Address $shippingAddress */
24+
$shippingAddress = $objectManager->create(Address::class, ['data' => $addressData[0]]);
25+
$shippingAddress->setAddressType('shipping');
26+
27+
$billingAddress = clone $shippingAddress;
28+
$billingAddress->setId(null)
29+
->setAddressType('billing');
30+
31+
/** @var Quote $quote */
32+
$quote = $objectManager->create(
33+
Quote::class,
34+
[
35+
'data' => [
36+
'customer_id' => $customer->getId(),
37+
'store_id' => $store->getId(),
38+
'reserved_order_id' => 'tsg-123456789',
39+
'is_active' => true,
40+
'is_multishipping' => false
41+
],
42+
]
43+
);
44+
$quote->setShippingAddress($shippingAddress)
45+
->setBillingAddress($billingAddress);
46+
47+
/** @var CartRepositoryInterface $repository */
48+
$repository = $objectManager->get(CartRepositoryInterface::class);
49+
$repository->save($quote);
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
use Magento\Framework\Registry;
9+
use Magento\Quote\Api\CartRepositoryInterface;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
12+
13+
/** @var ObjectManager $objectManager */
14+
$objectManager = Bootstrap::getObjectManager();
15+
16+
/** @var Registry $registry */
17+
$registry = $objectManager->get(Registry::class);
18+
$registry->unregister('isSecureArea');
19+
$registry->register('isSecureArea', true);
20+
21+
$quote = $objectManager->get(GetQuoteByReservedOrderId::class)->execute('tsg-123456789');
22+
if ($quote !== null) {
23+
/** @var CartRepositoryInterface $cartRepository */
24+
$cartRepository = $objectManager->get(CartRepositoryInterface::class);
25+
$cartRepository->delete($quote);
26+
}
27+
28+
$registry->unregister('isSecureArea');
29+
$registry->register('isSecureArea', false);

0 commit comments

Comments
 (0)