1+ <?php
2+ /**
3+ * Copyright © Magento, Inc. All rights reserved.
4+ * See COPYING.txt for license details.
5+ */
6+
7+ namespace Magento \Quote \Model ;
8+
9+ use Magento \Quote \Api \Data \AddressInterface ;
10+ use Magento \Quote \Model \Quote \Address \Rate ;
11+ use Magento \TestFramework \Helper \Bootstrap ;
12+
13+ /**
14+ * Class QuoteValidatorTest
15+ *
16+ * @magentoDbIsolation enabled
17+ */
18+ class QuoteValidatorTest extends \PHPUnit \Framework \TestCase
19+ {
20+ /**
21+ * @var QuoteValidator
22+ */
23+ private $ quoteValidator ;
24+
25+ /**
26+ * @inheritdoc
27+ */
28+ public function setUp ()
29+ {
30+ $ this ->quoteValidator = Bootstrap::getObjectManager ()->create (QuoteValidator::class);
31+ }
32+
33+ /**
34+ * @expectedException \Magento\Framework\Exception\LocalizedException
35+ * @expectedExceptionMessage Please check the shipping address information.
36+ */
37+ public function testValidateBeforeSubmitShippingAddressInvalid ()
38+ {
39+ $ quote = $ this ->getQuote ();
40+ $ quote ->getShippingAddress ()->setPostcode ('' );
41+
42+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
43+ }
44+
45+ /**
46+ * @expectedException \Magento\Framework\Exception\LocalizedException
47+ * @expectedExceptionMessage Some addresses can't be used due to the configurations for specific countries.
48+ */
49+ public function testValidateBeforeSubmitCountryIsNotAllowed ()
50+ {
51+ /** @magentoConfigFixture does not allow to change the value for the website scope */
52+ Bootstrap::getObjectManager ()->get (
53+ \Magento \Framework \App \Config \MutableScopeConfigInterface::class
54+ )->setValue (
55+ 'general/country/allow ' ,
56+ 'US ' ,
57+ \Magento \Store \Model \ScopeInterface::SCOPE_WEBSITES
58+ );
59+ $ quote = $ this ->getQuote ();
60+ $ quote ->getShippingAddress ()->setCountryId ('AF ' );
61+
62+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
63+ }
64+
65+ /**
66+ * @expectedException \Magento\Framework\Exception\LocalizedException
67+ * @expectedExceptionMessage The shipping method is missing. Select the shipping method and try again.
68+ */
69+ public function testValidateBeforeSubmitShippingMethodInvalid ()
70+ {
71+ $ quote = $ this ->getQuote ();
72+ $ quote ->getShippingAddress ()->setShippingMethod ('NONE ' );
73+
74+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
75+ }
76+
77+ /**
78+ * @expectedException \Magento\Framework\Exception\LocalizedException
79+ * @expectedExceptionMessage Please check the billing address information.
80+ */
81+ public function testValidateBeforeSubmitBillingAddressInvalid ()
82+ {
83+ $ quote = $ this ->getQuote ();
84+ $ quote ->getBillingAddress ()->setTelephone ('' );
85+
86+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
87+ }
88+
89+ /**
90+ * @expectedException \Magento\Framework\Exception\LocalizedException
91+ * @expectedExceptionMessage Enter a valid payment method and try again.
92+ */
93+ public function testValidateBeforeSubmitPaymentMethodInvalid ()
94+ {
95+ $ quote = $ this ->getQuote ();
96+ $ quote ->getPayment ()->setMethod ('' );
97+
98+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
99+ }
100+
101+ /**
102+ * @expectedException \Magento\Framework\Exception\LocalizedException
103+ * @magentoConfigFixture current_store sales/minimum_order/active 1
104+ * @magentoConfigFixture current_store sales/minimum_order/amount 100
105+ */
106+ public function testValidateBeforeSubmitMinimumAmountInvalid ()
107+ {
108+ $ quote = $ this ->getQuote ();
109+
110+ $ this ->quoteValidator ->validateBeforeSubmit ($ quote );
111+ }
112+
113+ /**
114+ * @return void
115+ */
116+ public function testValidateBeforeSubmitWithoutMinimumOrderAmount ()
117+ {
118+ $ this ->quoteValidator ->validateBeforeSubmit ($ this ->getQuote ());
119+ }
120+
121+ /**
122+ * @return Quote
123+ */
124+ private function getQuote (): Quote
125+ {
126+ /** @var Quote $quote */
127+ $ quote = Bootstrap::getObjectManager ()->create (Quote::class);
128+
129+ /** @var AddressInterface $billingAddress */
130+ $ billingAddress = Bootstrap::getObjectManager ()->create (AddressInterface::class);
131+ $ billingAddress ->setFirstname ('Joe ' )
132+ ->setLastname ('Doe ' )
133+ ->setCountryId ('US ' )
134+ ->setRegion ('TX ' )
135+ ->setCity ('Austin ' )
136+ ->setStreet ('1000 West Parmer Line ' )
137+ ->setPostcode ('11501 ' )
138+ ->setTelephone ('123456789 ' );
139+ $ quote ->setBillingAddress ($ billingAddress );
140+
141+ /** @var AddressInterface $shippingAddress */
142+ $ shippingAddress = Bootstrap::getObjectManager ()->create (AddressInterface::class);
143+ $ shippingAddress ->setFirstname ('Joe ' )
144+ ->setLastname ('Doe ' )
145+ ->setCountryId ('US ' )
146+ ->setRegion ('TX ' )
147+ ->setCity ('Austin ' )
148+ ->setStreet ('1000 West Parmer Line ' )
149+ ->setPostcode ('11501 ' )
150+ ->setTelephone ('123456789 ' );
151+ $ quote ->setShippingAddress ($ shippingAddress );
152+
153+ $ quote ->getShippingAddress ()
154+ ->setShippingMethod ('flatrate_flatrate ' )
155+ ->setCollectShippingRates (true );
156+ /** @var Rate $shippingRate */
157+ $ shippingRate = Bootstrap::getObjectManager ()->create (Rate::class);
158+ $ shippingRate ->setMethod ('flatrate ' )
159+ ->setCarrier ('flatrate ' )
160+ ->setPrice ('5 ' )
161+ ->setCarrierTitle ('Flat Rate ' )
162+ ->setCode ('flatrate_flatrate ' );
163+ $ quote ->getShippingAddress ()
164+ ->addShippingRate ($ shippingRate );
165+
166+ $ quote ->getPayment ()->setMethod ('CC ' );
167+
168+ /** @var QuoteRepository $quoteRepository */
169+ $ quoteRepository = Bootstrap::getObjectManager ()->create (QuoteRepository::class);
170+ $ quoteRepository ->save ($ quote );
171+
172+ return $ quote ;
173+ }
174+ }
0 commit comments