7
7
8
8
namespace Magento \GraphQl \Quote \Customer ;
9
9
10
- use Magento \Framework \App \ResourceConnection ;
11
10
use Magento \Integration \Api \CustomerTokenServiceInterface ;
11
+ use Magento \Quote \Model \QuoteFactory ;
12
+ use Magento \Quote \Model \MaskedQuoteIdToQuoteIdInterface ;
13
+ use Magento \Quote \Model \QuoteIdMaskFactory ;
14
+ use Magento \Quote \Model \ResourceModel \Quote as QuoteResource ;
12
15
use Magento \TestFramework \Helper \Bootstrap ;
13
16
use Magento \TestFramework \TestCase \GraphQlAbstract ;
14
17
use Magento \Quote \Api \GuestCartRepositoryInterface ;
@@ -29,44 +32,58 @@ class CreateEmptyCartTest extends GraphQlAbstract
29
32
private $ customerTokenService ;
30
33
31
34
/**
32
- * @var ResourceConnection
35
+ * @var QuoteResource
33
36
*/
34
- private $ resourceConnection ;
37
+ private $ quoteResource ;
38
+
39
+ /**
40
+ * @var QuoteFactory
41
+ */
42
+ private $ quoteFactory ;
43
+
44
+ /**
45
+ * @var MaskedQuoteIdToQuoteIdInterface
46
+ */
47
+ private $ maskedQuoteIdToQuoteId ;
48
+
49
+ /**
50
+ * @var QuoteIdMaskFactory
51
+ */
52
+ private $ quoteIdMaskFactory ;
53
+
54
+ /**
55
+ * @var string
56
+ */
57
+ private $ maskedQuoteId ;
35
58
36
59
protected function setUp ()
37
60
{
38
61
$ objectManager = Bootstrap::getObjectManager ();
39
62
$ this ->guestCartRepository = $ objectManager ->get (GuestCartRepositoryInterface::class);
40
63
$ this ->customerTokenService = $ objectManager ->get (CustomerTokenServiceInterface::class);
41
- $ this ->resourceConnection = $ objectManager ->get (ResourceConnection::class);
64
+ $ this ->quoteResource = $ objectManager ->get (QuoteResource::class);
65
+ $ this ->quoteFactory = $ objectManager ->get (QuoteFactory::class);
66
+ $ this ->maskedQuoteIdToQuoteId = $ objectManager ->get (MaskedQuoteIdToQuoteIdInterface::class);
67
+ $ this ->quoteIdMaskFactory = $ objectManager ->get (QuoteIdMaskFactory::class);
42
68
}
43
69
44
70
/**
45
71
* @magentoApiDataFixture Magento/Customer/_files/customer.php
46
72
*/
47
73
public function testCreateEmptyCart ()
48
74
{
49
- $ query = <<<QUERY
50
- mutation {
51
- createEmptyCart
52
- }
53
- QUERY ;
54
-
55
- $ customerToken =
$ this ->
customerTokenService ->
createCustomerAccessToken (
'[email protected] ' ,
'password ' );
56
- $ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
57
-
58
- $ response = $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
75
+ $ query = $ this ->getQuery ();
76
+ $ response = $ this ->graphQlQuery ($ query , [], '' , $ this ->getHeaderMapWithCustomerToken ());
59
77
60
78
self ::assertArrayHasKey ('createEmptyCart ' , $ response );
79
+ self ::assertNotEmpty ($ response ['createEmptyCart ' ]);
61
80
62
- $ maskedCartId = $ response ['createEmptyCart ' ];
63
- $ guestCart = $ this -> guestCartRepository -> get ( $ maskedCartId ) ;
81
+ $ guestCart = $ this -> guestCartRepository -> get ( $ response ['createEmptyCart ' ]) ;
82
+ $ this -> maskedQuoteId = $ response [ ' createEmptyCart ' ] ;
64
83
65
84
self ::assertNotNull ($ guestCart ->getId ());
66
85
self ::assertEquals (1 , $ guestCart ->getCustomer ()->getId ());
67
- self ::assertSame ('default ' , $ guestCart ->getStore ()->getCode ());
68
-
69
- $ this ->deleteCreatedQuote ($ guestCart ->getId ());
86
+ self ::assertEquals ('default ' , $ guestCart ->getStore ()->getCode ());
70
87
}
71
88
72
89
/**
@@ -75,40 +92,63 @@ public function testCreateEmptyCart()
75
92
*/
76
93
public function testCreateEmptyCartWithNotDefaultStore ()
77
94
{
78
- $ query = <<<QUERY
79
- mutation {
80
- createEmptyCart
81
- }
82
- QUERY ;
83
-
84
- $ customerToken =
$ this ->
customerTokenService ->
createCustomerAccessToken (
'[email protected] ' ,
'password ' );
85
- $ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken , 'Store ' => 'fixture_second_store ' ];
95
+ $ query = $ this ->getQuery ();
86
96
97
+ $ headerMap = $ this ->getHeaderMapWithCustomerToken ();
98
+ $ headerMap ['Store ' ] = 'fixture_second_store ' ;
87
99
$ response = $ this ->graphQlQuery ($ query , [], '' , $ headerMap );
88
100
89
101
self ::assertArrayHasKey ('createEmptyCart ' , $ response );
102
+ self ::assertNotEmpty ($ response ['createEmptyCart ' ]);
90
103
91
- $ maskedCartId = $ response ['createEmptyCart ' ];
92
104
/* guestCartRepository is used for registered customer to get the cart hash */
93
- $ guestCart = $ this ->guestCartRepository ->get ($ maskedCartId );
105
+ $ guestCart = $ this ->guestCartRepository ->get ($ response ['createEmptyCart ' ]);
106
+ $ this ->maskedQuoteId = $ response ['createEmptyCart ' ];
94
107
95
108
self ::assertNotNull ($ guestCart ->getId ());
96
109
self ::assertEquals (1 , $ guestCart ->getCustomer ()->getId ());
97
- self ::assertSame ('fixture_second_store ' , $ guestCart ->getStore ()->getCode ());
110
+ self ::assertEquals ('fixture_second_store ' , $ guestCart ->getStore ()->getCode ());
111
+ }
98
112
99
- $ this ->deleteCreatedQuote ($ guestCart ->getId ());
113
+ /**
114
+ * @return string
115
+ */
116
+ private function getQuery (): string
117
+ {
118
+ return <<<QUERY
119
+ mutation {
120
+ createEmptyCart
121
+ }
122
+ QUERY ;
100
123
}
101
124
102
125
/**
103
- * Delete active quote for customer by customer id.
104
- * This is needed to have ability to create new quote for another store and not return the active one.
105
- * @see QuoteManagement::createCustomerCart
106
- *
107
- * @param $quoteId
126
+ * @param string $username
127
+ * @param string $password
128
+ * @return array
108
129
*/
109
- private function deleteCreatedQuote ($ quoteId )
130
+ private function getHeaderMapWithCustomerToken (
131
+ string $ username =
'[email protected] ' ,
132
+ string $ password = 'password '
133
+ ): array {
134
+ $ customerToken = $ this ->customerTokenService ->createCustomerAccessToken ($ username , $ password );
135
+ $ headerMap = ['Authorization ' => 'Bearer ' . $ customerToken ];
136
+ return $ headerMap ;
137
+ }
138
+
139
+ public function tearDown ()
110
140
{
111
- $ connection = $ this ->resourceConnection ->getConnection ();
112
- $ connection ->query ('DELETE FROM quote WHERE entity_id = ' . $ quoteId );
141
+ if (null !== $ this ->maskedQuoteId ) {
142
+ $ quoteId = $ this ->maskedQuoteIdToQuoteId ->execute ($ this ->maskedQuoteId );
143
+
144
+ $ quote = $ this ->quoteFactory ->create ();
145
+ $ this ->quoteResource ->load ($ quote , $ quoteId );
146
+ $ this ->quoteResource ->delete ($ quote );
147
+
148
+ $ quoteIdMask = $ this ->quoteIdMaskFactory ->create ();
149
+ $ quoteIdMask ->setQuoteId ($ quoteId )
150
+ ->delete ();
151
+ }
152
+ parent ::tearDown ();
113
153
}
114
154
}
0 commit comments