88namespace Magento \Sales \Test \Unit \Ui \Component \Listing \Column ;
99
1010use 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 ;
1115use Magento \Framework \TestFramework \Unit \Helper \ObjectManager ;
1216use Magento \Framework \View \Element \UiComponent \ContextInterface ;
1317use Magento \Framework \View \Element \UiComponent \Processor ;
1721use PHPUnit \Framework \MockObject \MockObject ;
1822use PHPUnit \Framework \TestCase ;
1923
24+ /**
25+ * Test for \Magento\Sales\Ui\Component\Listing\Column\PurchasedPrice
26+ */
2027class PurchasedPriceTest extends TestCase
2128{
2229 /**
@@ -34,6 +41,21 @@ class PurchasedPriceTest extends TestCase
3441 */
3542 private $ orderRepository ;
3643
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+
3759 protected function setUp (): void
3860 {
3961 $ objectManager = new ObjectManager ($ this );
@@ -47,56 +69,61 @@ protected function setUp(): void
4769 ->setMethods (['load ' , 'format ' ])
4870 ->disableOriginalConstructor ()
4971 ->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+
5485 $ this ->model = $ objectManager ->getObject (
5586 PurchasedPrice::class,
5687 [
5788 'currency ' => $ this ->currencyMock ,
5889 '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 ,
6095 ]
6196 );
6297 }
6398
6499 /**
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
69102 * @dataProvider prepareDataSourceDataProvider
70103 */
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 ' ]) ;
88121
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 ' ];
100127
101128 $ this ->currencyMock ->expects ($ this ->once ())
102129 ->method ('load ' )
@@ -108,9 +135,9 @@ public function testPrepareDataSource(
108135 ->with ($ oldItemValue , [], false )
109136 ->willReturn ($ newItemValue );
110137
111- $ this ->model ->setData ('name ' , $ itemName );
138+ $ this ->model ->setData ('name ' , ' item_name ' );
112139 $ dataSource = $ this ->model ->prepareDataSource ($ dataSource );
113- $ this ->assertEquals ($ newItemValue , $ dataSource ['data ' ]['items ' ][0 ][$ itemName ]);
140+ $ this ->assertEquals ($ newItemValue , $ dataSource ['data ' ]['items ' ][0 ][' item_name ' ]);
114141 }
115142
116143 /**
@@ -120,17 +147,38 @@ public function prepareDataSourceDataProvider(): array
120147 {
121148 return [
122149 [
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+ ]
127165 ],
128166 [
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+ ]
134182 ];
135183 }
136184}
0 commit comments