5
5
*/
6
6
namespace Magento \Catalog \Api ;
7
7
8
+ use Magento \Catalog \Api \Data \ProductInterface ;
9
+ use Magento \Framework \Exception \CouldNotSaveException ;
10
+ use Magento \Framework \Exception \InputException ;
11
+ use Magento \Framework \Exception \NoSuchEntityException ;
12
+ use Magento \Framework \Exception \StateException ;
13
+ use Magento \Framework \Webapi \Rest \Request ;
14
+ use Magento \TestFramework \Helper \Bootstrap ;
15
+ use Magento \TestFramework \ObjectManager ;
8
16
use Magento \TestFramework \TestCase \WebapiAbstract ;
9
- use Magento \Framework \Webapi \Exception as HTTPExceptionCodes ;
10
17
11
18
/**
12
- * SpecialPriceStorage test.
19
+ * SpecialPriceStorage API operations test
13
20
*/
14
21
class SpecialPriceStorageTest extends WebapiAbstract
15
22
{
16
23
const SERVICE_NAME = 'catalogSpecialPriceStorageV1 ' ;
17
24
const SERVICE_VERSION = 'V1 ' ;
18
25
const SIMPLE_PRODUCT_SKU = 'simple ' ;
26
+ const VIRTUAL_PRODUCT_SKU = 'virtual-product ' ;
19
27
20
28
/**
21
- * @var \Magento\TestFramework\ ObjectManager
29
+ * @var ObjectManager
22
30
*/
23
31
private $ objectManager ;
24
32
@@ -27,7 +35,7 @@ class SpecialPriceStorageTest extends WebapiAbstract
27
35
*/
28
36
protected function setUp ()
29
37
{
30
- $ this ->objectManager = \ Magento \ TestFramework \ Helper \ Bootstrap::getObjectManager ();
38
+ $ this ->objectManager = Bootstrap::getObjectManager ();
31
39
}
32
40
33
41
/**
@@ -38,14 +46,14 @@ protected function setUp()
38
46
public function testGet ()
39
47
{
40
48
$ specialPrice = 3057 ;
41
- $ productRepository = $ this ->objectManager ->create (\ Magento \ Catalog \ Api \ ProductRepositoryInterface::class);
49
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
42
50
$ product = $ productRepository ->get (self ::SIMPLE_PRODUCT_SKU , true );
43
51
$ product ->setData ('special_price ' , $ specialPrice );
44
52
$ productRepository ->save ($ product );
45
53
$ serviceInfo = [
46
54
'rest ' => [
47
55
'resourcePath ' => '/V1/products/special-price-information ' ,
48
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST
56
+ 'httpMethod ' => Request::HTTP_METHOD_POST
49
57
],
50
58
'soap ' => [
51
59
'service ' => self ::SERVICE_NAME ,
@@ -54,7 +62,7 @@ public function testGet()
54
62
],
55
63
];
56
64
$ response = $ this ->_webApiCall ($ serviceInfo , ['skus ' => [self ::SIMPLE_PRODUCT_SKU ]]);
57
- /** @var \Magento\Catalog\Api\Data\ ProductInterface $product */
65
+ /** @var ProductInterface $product */
58
66
$ product = $ productRepository ->get (self ::SIMPLE_PRODUCT_SKU );
59
67
$ this ->assertNotEmpty ($ response );
60
68
$ this ->assertEquals ($ product ->getSpecialPrice (), $ response [0 ]['price ' ]);
@@ -64,34 +72,27 @@ public function testGet()
64
72
* Test update method.
65
73
*
66
74
* @magentoApiDataFixture Magento/Catalog/_files/product_virtual.php
75
+ * @dataProvider updateData
76
+ * @param array $data
67
77
*/
68
- public function testUpdate ()
78
+ public function testUpdate (array $ data )
69
79
{
70
- $ sku = 'virtual-product ' ;
71
80
$ serviceInfo = [
72
81
'rest ' => [
73
82
'resourcePath ' => '/V1/products/special-price ' ,
74
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST
83
+ 'httpMethod ' => Request::HTTP_METHOD_POST
75
84
],
76
85
'soap ' => [
77
86
'service ' => self ::SERVICE_NAME ,
78
87
'serviceVersion ' => self ::SERVICE_VERSION ,
79
88
'operation ' => self ::SERVICE_NAME . 'Update ' ,
80
89
],
81
90
];
82
- $ storeId = 0 ;
83
- $ newPrice = 31337 ;
84
91
$ response = $ this ->_webApiCall (
85
92
$ serviceInfo ,
86
93
[
87
94
'prices ' => [
88
- [
89
- 'price ' => $ newPrice ,
90
- 'store_id ' => $ storeId ,
91
- 'sku ' => $ sku ,
92
- 'price_from ' => '2037-01-19 03:14:07 ' ,
93
- 'price_to ' => '2038-01-19 03:14:07 ' ,
94
- ]
95
+ $ data
95
96
]
96
97
]
97
98
);
@@ -102,23 +103,28 @@ public function testUpdate()
102
103
* Test delete method.
103
104
*
104
105
* @magentoApiDataFixture Magento/Catalog/_files/product_simple.php
106
+ * @dataProvider deleteData
107
+ * @param array $data
108
+ * @throws CouldNotSaveException
109
+ * @throws InputException
110
+ * @throws NoSuchEntityException
111
+ * @throws StateException
105
112
*/
106
- public function testDelete ()
113
+ public function testDelete (array $ data )
107
114
{
108
- $ specialPrice = 3057 ;
109
- /** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */
110
- $ productRepository = $ this ->objectManager ->create (\Magento \Catalog \Api \ProductRepositoryInterface::class);
111
- $ fromDate = '1970-01-01 00:00:01 ' ;
112
- $ toDate = '2038-01-19 03:14:07 ' ;
113
- $ product = $ productRepository ->get (self ::SIMPLE_PRODUCT_SKU , true );
114
- $ product ->setData ('special_price ' , $ specialPrice )
115
- ->setData ('special_from_date ' , $ fromDate )
116
- ->setData ('special_to_date ' , $ toDate );
115
+ /** @var ProductRepositoryInterface $productRepository */
116
+ $ productRepository = $ this ->objectManager ->create (ProductRepositoryInterface::class);
117
+ $ product = $ productRepository ->get ($ data ['sku ' ], true );
118
+ $ product ->setData ('special_price ' , $ data ['price ' ]);
119
+ $ product ->setData ('special_from_date ' , $ data ['price_from ' ]);
120
+ if ($ data ['price_to ' ]) {
121
+ $ product ->setData ('special_to_date ' , $ data ['price_to ' ]);
122
+ }
117
123
$ productRepository ->save ($ product );
118
124
$ serviceInfo = [
119
125
'rest ' => [
120
126
'resourcePath ' => '/V1/products/special-price-delete ' ,
121
- 'httpMethod ' => \ Magento \ Framework \ Webapi \ Rest \ Request::HTTP_METHOD_POST
127
+ 'httpMethod ' => Request::HTTP_METHOD_POST
122
128
],
123
129
'soap ' => [
124
130
'service ' => self ::SERVICE_NAME ,
@@ -130,19 +136,80 @@ public function testDelete()
130
136
$ serviceInfo ,
131
137
[
132
138
'prices ' => [
133
- [
134
- 'price ' => $ specialPrice ,
135
- 'store_id ' => 0 ,
136
- 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
137
- 'price_from ' => $ fromDate ,
138
- 'price_to ' => $ toDate ,
139
- ]
139
+ $ data
140
140
]
141
141
]
142
142
);
143
- /** @var \Magento\Catalog\Api\Data\ProductInterface $product */
144
- $ product = $ productRepository ->get (self ::SIMPLE_PRODUCT_SKU , false , null , true );
143
+ $ product = $ productRepository ->get ($ data ['sku ' ], false , null , true );
145
144
$ this ->assertEmpty ($ response );
146
145
$ this ->assertNull ($ product ->getSpecialPrice ());
147
146
}
147
+
148
+ /**
149
+ * Data provider for testUpdate
150
+ *
151
+ * @return array
152
+ */
153
+ public function updateData (): array
154
+ {
155
+ $ fromDate = '2037-01-19 03:14:07 ' ;
156
+ $ toDate = '2038-01-19 03:14:07 ' ;
157
+
158
+ return [
159
+ [
160
+ // data set with 'price_to' specified
161
+ [
162
+ 'price ' => 31337 ,
163
+ 'store_id ' => 0 ,
164
+ 'sku ' => self ::VIRTUAL_PRODUCT_SKU ,
165
+ 'price_from ' => $ fromDate ,
166
+ 'price_to ' => $ toDate
167
+ ]
168
+ ],
169
+ [
170
+ // data set without 'price_to' specified
171
+ [
172
+ 'price ' => 31337 ,
173
+ 'store_id ' => 0 ,
174
+ 'sku ' => self ::VIRTUAL_PRODUCT_SKU ,
175
+ 'price_from ' => $ fromDate ,
176
+ 'price_to ' => false
177
+ ]
178
+ ],
179
+ ];
180
+ }
181
+
182
+ /**
183
+ * Data provider for testDelete
184
+ *
185
+ * @return array
186
+ */
187
+ public function deleteData (): array
188
+ {
189
+ $ fromDate = '1970-01-01 00:00:01 ' ;
190
+ $ toDate = '2038-01-19 03:14:07 ' ;
191
+
192
+ return [
193
+ [
194
+ // data set with 'price_to' specified
195
+ [
196
+ 'price ' => 3057 ,
197
+ 'store_id ' => 0 ,
198
+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
199
+ 'price_from ' => $ fromDate ,
200
+ 'price_to ' => $ toDate
201
+ ]
202
+ ],
203
+ [
204
+ // data set without 'price_to' specified
205
+ [
206
+ 'price ' => 3057 ,
207
+ 'store_id ' => 0 ,
208
+ 'sku ' => self ::SIMPLE_PRODUCT_SKU ,
209
+ 'price_from ' => $ fromDate ,
210
+ 'price_to ' => false
211
+ ]
212
+ ],
213
+ ];
214
+ }
148
215
}
0 commit comments