77
88namespace Magento \GraphQl \Quote ;
99
10+ use Magento \Catalog \Api \ProductRepositoryInterface ;
11+ use Magento \Framework \ObjectManagerInterface ;
1012use Magento \Integration \Api \CustomerTokenServiceInterface ;
13+ use Magento \Quote \Api \Data \CartItemInterface ;
1114use Magento \Quote \Model \Quote ;
1215use Magento \Quote \Model \QuoteIdToMaskedQuoteIdInterface ;
1316use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
1922 */
2023class PlaceOrderTest extends GraphQlAbstract
2124{
25+ /**
26+ * @var ObjectManagerInterface
27+ */
28+ private $ objectManager ;
29+
2230 /**
2331 * @var CustomerTokenServiceInterface
2432 */
@@ -41,11 +49,16 @@ class PlaceOrderTest extends GraphQlAbstract
4149
4250 protected function setUp ()
4351 {
44- $ objectManager = Bootstrap::getObjectManager ();
45- $ this ->quoteResource = $ objectManager ->create (QuoteResource::class);
46- $ this ->quote = $ objectManager ->create (Quote::class);
47- $ this ->quoteIdToMaskedId = $ objectManager ->create (QuoteIdToMaskedQuoteIdInterface::class);
48- $ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
52+ $ this ->objectManager = Bootstrap::getObjectManager ();
53+ $ this ->quoteResource = $ this ->objectManager ->create (QuoteResource::class);
54+ $ this ->quote = $ this ->objectManager ->create (Quote::class);
55+ $ this ->quoteIdToMaskedId = $ this ->objectManager ->create (QuoteIdToMaskedQuoteIdInterface::class);
56+ $ this ->customerTokenService = $ this ->objectManager ->get (CustomerTokenServiceInterface::class);
57+ $ this ->quoteResource ->load (
58+ $ this ->quote ,
59+ 'test_order_1 ' ,
60+ 'reserved_order_id '
61+ );
4962 }
5063
5164 /**
@@ -54,14 +67,138 @@ protected function setUp()
5467 public function testPlaceOrder ()
5568 {
5669 $ reservedOrderId = 'test_order_1 ' ;
57- $ this ->quoteResource ->load (
58- $ this ->quote ,
59- $ reservedOrderId ,
60- 'reserved_order_id '
70+
71+ $ query = $ this ->preparePlaceOrderQuery ();
72+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
73+
74+ self ::assertArrayHasKey ('order_id ' , $ response ['placeOrder ' ]['order ' ]);
75+ self ::assertEquals ($ reservedOrderId , $ response ['placeOrder ' ]['order ' ]['order_id ' ]);
76+ }
77+
78+ /**
79+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
80+ */
81+ public function testPlaceOrderOfAnotherCustomerCart ()
82+ {
83+ $ query = $ this ->preparePlaceOrderQuery ();
84+
85+ self ::expectExceptionMessageRegExp ('/The current user cannot perform operations on cart*/ ' );
86+ $ this ->graphQlQuery ($ query );
87+ }
88+
89+ /**
90+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
91+ */
92+ public function testPlaceOrderWithOutOfStockProduct ()
93+ {
94+ $ productRepository = $ this ->objectManager ->get (ProductRepositoryInterface::class);
95+ $ product = $ productRepository ->get ('simple ' );
96+ $ extensionAttributes = $ product ->getExtensionAttributes ();
97+ $ stockItem = $ extensionAttributes ->getStockItem ();
98+ $ stockItem ->setIsInStock (false );
99+ $ productRepository ->save ($ product );
100+
101+ $ query = $ this ->preparePlaceOrderQuery ();
102+
103+ self ::expectExceptionMessage ('Unable to place order: Some of the products are out of stock ' );
104+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
105+ }
106+
107+ /**
108+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
109+ */
110+ public function testPlaceOrderWithNoItemsInCart ()
111+ {
112+ $ quoteItems = $ this ->quote ->getAllItems ();
113+
114+ /** @var CartItemInterface $quoteItem */
115+ foreach ($ quoteItems as $ quoteItem ) {
116+ $ this ->quote ->removeItem ($ quoteItem ->getItemId ());
117+ }
118+ $ this ->quoteResource ->save ($ this ->quote );
119+
120+ $ query = $ this ->preparePlaceOrderQuery ();
121+
122+ self ::expectExceptionMessage (
123+ 'Unable to place order: A server error stopped your order from being placed. ' .
124+ 'Please try to place your order again '
125+ );
126+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
127+ }
128+
129+ /**
130+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
131+ */
132+ public function testPlaceOrderWithNoShippingMethod ()
133+ {
134+ $ this ->quote ->getShippingAddress ()->setShippingMethod ('' );
135+ $ this ->quoteResource ->save ($ this ->quote );
136+
137+ $ query = $ this ->preparePlaceOrderQuery ();
138+
139+ self ::expectExceptionMessage (
140+ 'Unable to place order: The shipping method is missing. Select the shipping method and try again '
141+ );
142+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
143+ }
144+
145+ /**
146+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
147+ */
148+ public function testPlaceOrderWithNoShippingAddress ()
149+ {
150+ $ this ->quote ->removeAddress ($ this ->quote ->getShippingAddress ()->getId ());
151+ $ this ->quoteResource ->save ($ this ->quote );
152+
153+ $ query = $ this ->preparePlaceOrderQuery ();
154+
155+ self ::expectExceptionMessage (
156+ 'Unable to place order: Some addresses can \'t be used due to the configurations for specific countries '
157+ );
158+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
159+ }
160+
161+ /**
162+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
163+ */
164+ public function testPlaceOrderWithNoPaymentMethod ()
165+ {
166+ $ this ->quote ->getPayment ()->setMethod ('' );
167+ $ this ->quoteResource ->save ($ this ->quote );
168+
169+ $ query = $ this ->preparePlaceOrderQuery ();
170+
171+ self ::expectExceptionMessage ('Unable to place order: Enter a valid payment method and try again ' );
172+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
173+ }
174+
175+ /**
176+ * @magentoApiDataFixture Magento/Checkout/_files/quote_with_check_payment.php
177+ */
178+ public function testPlaceOrderWithNoBillingAddress ()
179+ {
180+ $ this ->quote ->removeAddress ($ this ->quote ->getBillingAddress ()->getId ());
181+ $ this ->quoteResource ->save ($ this ->quote );
182+
183+ $ query = $ this ->preparePlaceOrderQuery ();
184+
185+ self ::expectExceptionMessageRegExp (
186+ '/Unable to place order: Please check the billing address information*/ '
61187 );
188+ $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
189+ }
190+
191+ /**
192+ * Prepares GraphQl query for placing an order
193+ *
194+ * @return string
195+ * @throws \Magento\Framework\Exception\NoSuchEntityException
196+ */
197+ private function preparePlaceOrderQuery (): string
198+ {
62199 $ maskedQuoteId = $ this ->quoteIdToMaskedId ->execute ((int )$ this ->quote ->getId ());
63200
64- $ query = <<<QUERY
201+ return <<<QUERY
65202mutation {
66203 placeOrder(input: {cart_id: " $ maskedQuoteId"}) {
67204 order {
@@ -70,17 +207,13 @@ public function testPlaceOrder()
70207 }
71208}
72209QUERY ;
73-
74- $ response = $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMap ());
75-
76- self ::assertArrayHasKey ('order_id ' , $ response ['placeOrder ' ]['order ' ]);
77- self ::assertEquals ($ reservedOrderId , $ response ['placeOrder ' ]['order ' ]['order_id ' ]);
78210 }
79211
80212 /**
81213 * @param string $username
82214 * @param string $password
83215 * @return array
216+ * @throws \Magento\Framework\Exception\AuthenticationException
84217 */
85218 private function getHeaderMap (string $ username = 'customer@example.com ' , string $ password = 'password ' ): array
86219 {
0 commit comments