Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
UpdateQuoteStoreTest.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
19 use Magento\TestFramework\Helper\Bootstrap as BootstrapHelper;
20 
25 class UpdateQuoteStoreTest extends \PHPUnit\Framework\TestCase
26 {
30  private $objectManager;
31 
35  private $quoteRepository;
36 
37  protected function setUp()
38  {
39  $this->objectManager = BootstrapHelper::getObjectManager();
40  $this->quoteRepository = $this->objectManager->create(CartRepositoryInterface::class);
41  }
42 
51  public function testUpdateQuoteStoreAfterChangeStoreCookie()
52  {
53  $secondStoreCode = 'fixture_second_store';
54  $reservedOrderId = 'reserved_order_id';
55 
57  $storeManager = $this->objectManager->get(StoreManagerInterface::class);
58  $currentStore = $storeManager->getStore();
59 
60  $quote = $this->getQuote($reservedOrderId);
61  $this->assertEquals(
62  $currentStore->getId(),
63  $quote->getStoreId(),
64  'Current store id and quote store id are not match'
65  );
66 
68  $checkoutSession = $this->objectManager->get(Session::class);
69  $checkoutSession->setQuoteId($quote->getId());
70 
71  $storeRepository = $this->objectManager->create(StoreRepository::class);
72  $secondStore = $storeRepository->get($secondStoreCode);
73 
74  $storeCookieManager = $this->getStoreCookieManager($currentStore);
75  $storeCookieManager->setStoreCookie($secondStore);
76 
77  $updatedQuote = $this->getQuote($reservedOrderId);
78  $this->assertEquals(
79  $secondStore->getId(),
80  $updatedQuote->getStoreId(),
81  'Active quote store id should be equal second store id'
82  );
83  }
84 
91  private function getQuote(string $reservedOrderId): Quote
92  {
94  $searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
95  $searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
96  ->create();
97 
98  $items = $this->quoteRepository->getList($searchCriteria)->getItems();
99 
100  return array_pop($items);
101  }
102 
113  private function getStoreCookieManager(StoreInterface $currentStore): StoreCookieManagerInterface
114  {
116  $storeCookieManager = $this->objectManager->get(StoreCookieManagerInterface::class);
117  $cookieManagerMock = $this->getMockBuilder(\Magento\Framework\Stdlib\Cookie\PhpCookieManager::class)
118  ->disableOriginalConstructor()
119  ->getMock();
120  $cookieManagerMock->method('getCookie')
121  ->willReturn($currentStore->getCode());
122 
123  $reflection = new \ReflectionClass($storeCookieManager);
124  $cookieManager = $reflection->getProperty('cookieManager');
125  $cookieManager->setAccessible(true);
126  $cookieManager->setValue($storeCookieManager, $cookieManagerMock);
127 
128  return $storeCookieManager;
129  }
130 }
$quote
$storeManager
$searchCriteria
$secondStore
$searchCriteriaBuilder
$currentStore
Definition: queue.php:17
$items