8
8
namespace Magento \Sales \Test \Unit \Ui \Component \Listing \Column ;
9
9
10
10
use Magento \Directory \Model \Currency ;
11
+ use Magento \Sales \Api \Data \OrderInterface ;
12
+ use Magento \Sales \Api \Data \OrderSearchResultInterface ;
13
+ use Magento \Framework \Api \SearchCriteria ;
14
+ use Magento \Framework \Api \SearchCriteriaBuilder ;
11
15
use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
12
16
use Magento \Framework \View \Element \UiComponent \ContextInterface ;
13
17
use Magento \Framework \View \Element \UiComponent \Processor ;
17
21
use PHPUnit \Framework \MockObject \MockObject ;
18
22
use PHPUnit \Framework \TestCase ;
19
23
24
+ /**
25
+ * Test for \Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice
26
+ */
20
27
class PurchasedPriceTest extends TestCase
21
28
{
22
29
/**
@@ -34,6 +41,21 @@ class PurchasedPriceTest extends TestCase
34
41
*/
35
42
private $ orderRepository ;
36
43
44
+ /**
45
+ * @var SearchCriteria|MockObject
46
+ */
47
+ private $ searchCriteriaMock ;
48
+
49
+ /**
50
+ * @var OrderSearchResultInterface|MockObject
51
+ */
52
+ private $ orderSearchResultMock ;
53
+
54
+ /**
55
+ * @var OrderInterface|MockObject
56
+ */
57
+ private $ order ;
58
+
37
59
protected function setUp (): void
38
60
{
39
61
$ objectManager = new ObjectManager ($ this );
@@ -47,56 +69,61 @@ protected function setUp(): void
47
69
->setMethods (['load ' , 'format ' ])
48
70
->disableOriginalConstructor ()
49
71
->getMock ();
50
- $ this ->orderRepository = $ this ->getMockBuilder (OrderRepositoryInterface::class)
51
- ->setMethods (['getList ' ,'get ' ,'delete ' ,'save ' ,'getOrderCurrencyCode ' ])
52
- ->disableOriginalConstructor ()
53
- ->getMock ();
72
+ $ this ->orderRepository = $ this ->getMockForAbstractClass (OrderRepositoryInterface::class);
73
+ $ this ->order = $ this ->getMockForAbstractClass (OrderInterface::class);
74
+ $ this ->orderSearchResultMock = $ this ->getMockForAbstractClass (OrderSearchResultInterface::class);
75
+ $ this ->searchCriteriaMock = $ this ->createMock (SearchCriteria::class);
76
+ $ searchCriteriaBuilderMock = $ this ->createMock (SearchCriteriaBuilder::class);
77
+ $ searchCriteriaBuilderMock ->expects ($ this ->once ())
78
+ ->method ('addFilter ' )
79
+ ->willReturnSelf ();
80
+
81
+ $ searchCriteriaBuilderMock ->expects ($ this ->once ())
82
+ ->method ('create ' )
83
+ ->willReturn ($ this ->searchCriteriaMock );
84
+
54
85
$ this ->model = $ objectManager ->getObject (
55
86
PurchasedPrice::class,
56
87
[
57
88
'currency ' => $ this ->currencyMock ,
58
89
'context ' => $ contextMock ,
59
- 'order ' => $ this ->orderRepository ,
90
+ 'orderRepository ' => $ this ->orderRepository ,
91
+ 'order ' => $ this ->order ,
92
+ 'searchCriteriaBuilder ' => $ searchCriteriaBuilderMock ,
93
+ 'searchCriteria ' => $ this ->searchCriteriaMock ,
94
+ 'orderSearchResult ' => $ this ->orderSearchResultMock ,
60
95
]
61
96
);
62
97
}
63
98
64
99
/**
65
- * @param string $itemName
66
- * @param string $oldItemValue
67
- * @param string $newItemValue
68
- * @param string|null $orderCurrencyCode
100
+ * @param array $orderData
101
+ * @param array $dataSource
69
102
* @dataProvider prepareDataSourceDataProvider
70
103
*/
71
- public function testPrepareDataSource (
72
- $ itemName ,
73
- $ oldItemValue,
74
- $ newItemValue,
75
- $ orderCurrencyCode
76
- ): void {
77
- $ dataSource = [
78
- ' data ' => [
79
- ' items ' => [
80
- [
81
- $ itemName => $ oldItemValue ,
82
- ' order_currency_code ' => $ orderCurrencyCode ,
83
- ' order_id ' => 1 ,
84
- ]
85
- ]
86
- ]
87
- ] ;
104
+ public function testPrepareDataSource (array $ orderData , array $ dataSource ): void
105
+ {
106
+ $ oldItemValue = ' oldItemValue ' ;
107
+ $ newItemValue = ' newItemValue ' ;
108
+
109
+ $ this -> orderRepository -> expects ( $ this -> once ())
110
+ -> method ( ' getList ' )
111
+ -> with ( $ this -> searchCriteriaMock )
112
+ -> willReturn ( $ this -> orderSearchResultMock );
113
+
114
+ $ this -> orderSearchResultMock -> expects ( $ this -> once ())
115
+ -> method ( ' getItems ' )
116
+ -> willReturn ([ $ this -> order ]);
117
+
118
+ $ this -> order -> expects ( $ this -> once ())
119
+ -> method ( ' getEntityId ' )
120
+ -> willReturn ( $ orderData [ ' entity_id ' ]) ;
88
121
89
- if (isset ($ dataSource ['data ' ]['items ' ][0 ]['order_currency_code ' ])) {
90
- $ currencyCode = $ dataSource ['data ' ]['items ' ][0 ]['order_currency_code ' ];
91
- } else {
92
- $ currencyCode = 'FR ' ;
93
- $ this ->orderRepository ->expects ($ this ->once ())
94
- ->method ('get ' )
95
- ->willReturnSelf ();
96
- $ this ->orderRepository ->expects ($ this ->once ())
97
- ->method ('getOrderCurrencyCode ' )
98
- ->willReturn ($ currencyCode );
99
- }
122
+ $ this ->order ->expects ($ this ->once ())
123
+ ->method ('getOrderCurrencyCode ' )
124
+ ->willReturn ($ orderData ['order_currency_code ' ]);
125
+
126
+ $ currencyCode = $ dataSource ['data ' ]['items ' ][0 ]['order_currency_code ' ] ?? $ orderData ['order_currency_code ' ];
100
127
101
128
$ this ->currencyMock ->expects ($ this ->once ())
102
129
->method ('load ' )
@@ -108,9 +135,9 @@ public function testPrepareDataSource(
108
135
->with ($ oldItemValue , [], false )
109
136
->willReturn ($ newItemValue );
110
137
111
- $ this ->model ->setData ('name ' , $ itemName );
138
+ $ this ->model ->setData ('name ' , ' item_name ' );
112
139
$ dataSource = $ this ->model ->prepareDataSource ($ dataSource );
113
- $ this ->assertEquals ($ newItemValue , $ dataSource ['data ' ]['items ' ][0 ][$ itemName ]);
140
+ $ this ->assertEquals ($ newItemValue , $ dataSource ['data ' ]['items ' ][0 ][' item_name ' ]);
114
141
}
115
142
116
143
/**
@@ -120,17 +147,38 @@ public function prepareDataSourceDataProvider(): array
120
147
{
121
148
return [
122
149
[
123
- 'item_name ' => 'itemName ' ,
124
- 'old_item_value ' => 'oldItemValue ' ,
125
- 'new_item_value ' => 'newItemValue ' ,
126
- 'order_currency_code ' => 'US ' ,
150
+ 'orderData ' => [
151
+ 'entity_id ' => 1 ,
152
+ 'order_currency_code ' => 'US ' ,
153
+ ],
154
+ 'dataSource ' => [
155
+ 'data ' => [
156
+ 'items ' => [
157
+ [
158
+ 'item_name ' => 'oldItemValue ' ,
159
+ 'order_currency_code ' => 'US ' ,
160
+ 'order_id ' => 1 ,
161
+ ]
162
+ ]
163
+ ]
164
+ ]
127
165
],
128
166
[
129
- 'item_name ' => 'itemName ' ,
130
- 'old_item_value ' => 'oldItemValue ' ,
131
- 'new_item_value ' => 'newItemValue ' ,
132
- 'order_currency_code ' => null ,
133
- ],
167
+ 'orderData ' => [
168
+ 'entity_id ' => 1 ,
169
+ 'order_currency_code ' => 'FR ' ,
170
+ ],
171
+ 'dataSource ' => [
172
+ 'data ' => [
173
+ 'items ' => [
174
+ [
175
+ 'item_name ' => 'oldItemValue ' ,
176
+ 'order_id ' => 1 ,
177
+ ]
178
+ ]
179
+ ]
180
+ ]
181
+ ]
134
182
];
135
183
}
136
184
}
0 commit comments