Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ConfigTest.php
Go to the documentation of this file.
1 <?php
7 
10 
14 class ConfigTest extends \PHPUnit\Framework\TestCase
15 {
19  protected $salesConfig;
20 
25 
29  protected $statusFactoryMock;
30 
34  protected $orderStatusModel;
35 
39  protected $storeManagerMock;
40 
44  protected function setUp()
45  {
46  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
47 
48  $this->storeManagerMock = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
49  $this->orderStatusModel = $objectManager->getObject(\Magento\Sales\Model\Order\Status::class, [
50  'storeManager' => $this->storeManagerMock,
51  ]);
52  $this->statusFactoryMock = $this->getMockBuilder(\Magento\Sales\Model\Order\StatusFactory::class)
53  ->disableOriginalConstructor()
54  ->setMethods(['load', 'create'])
55  ->getMock();
56  $this->orderStatusCollectionFactoryMock = $this->createPartialMock(
57  \Magento\Sales\Model\ResourceModel\Order\Status\CollectionFactory::class,
58  ['create']
59  );
60  $this->salesConfig = $objectManager
61  ->getObject(
62  \Magento\Sales\Model\Order\Config::class,
63  [
64  'orderStatusFactory' => $this->statusFactoryMock,
65  'orderStatusCollectionFactory' => $this->orderStatusCollectionFactoryMock
66  ]
67  );
68  }
69 
74  {
75  $statuses = [
76  new DataObject(
77  [
78  'status' => 'canceled',
79  'is_default' => 1,
80  'visible_on_front' => 1,
81  ]
82  ),
83  new DataObject(
84  [
85  'status' => 'complete',
86  'is_default' => 1,
87  'visible_on_front' => 0,
88  ]
89  ),
90  new DataObject(
91  [
92  'status' => 'processing',
93  'is_default' => 1,
94  'visible_on_front' => 1,
95  ]
96  ),
97  new DataObject(
98  [
99  'status' => 'pending_payment',
100  'is_default' => 1,
101  'visible_on_front' => 0,
102  ]
103  ),
104  ];
105  $expectedResult = ['complete', 'pending_payment'];
106 
107  $collectionMock = $this->createPartialMock(Collection::class, ['create', 'joinStates']);
108  $this->orderStatusCollectionFactoryMock->expects($this->once())
109  ->method('create')
110  ->will($this->returnValue($collectionMock));
111  $collectionMock->expects($this->once())
112  ->method('joinStates')
113  ->will($this->returnValue($statuses));
114 
115  $result = $this->salesConfig->getInvisibleOnFrontStatuses();
116  $this->assertSame($expectedResult, $result);
117  }
118 
123  {
124  $statuses = [
125  new DataObject(
126  [
127  'status' => 'fraud',
128  'state' => 'processing',
129  'label' => 'Suspected Fraud',
130  ]
131  ),
132  new DataObject(
133  [
134  'status' => 'processing',
135  'state' => 'processing',
136  'label' => 'Processing',
137  ]
138  )
139  ];
140  $collectionMock = $this->createPartialMock(Collection::class, ['create', 'joinStates']);
141  $this->orderStatusCollectionFactoryMock->expects($this->once())
142  ->method('create')
143  ->will($this->returnValue($collectionMock));
144  $collectionMock->expects($this->once())
145  ->method('joinStates')
146  ->will($this->returnValue($statuses));
147  $result = $this->salesConfig->getStateLabelByStateAndStatus('processing', 'fraud');
148  $this->assertSame('Suspected Fraud', $result->getText());
149  }
150 
161  public function testGetStatuses($state, $joinLabels, $collectionData, $expectedResult)
162  {
163  $collectionMock = $this->createPartialMock(
164  Collection::class,
165  ['create', 'joinStates', 'addStateFilter', 'orderByLabel']
166  );
167  $this->orderStatusCollectionFactoryMock->expects($this->any())
168  ->method('create')
169  ->will($this->returnValue($collectionMock));
170 
171  $collectionMock->expects($this->once())
172  ->method('addStateFilter')
173  ->will($this->returnSelf());
174 
175  $collectionMock->expects($this->once())
176  ->method('orderByLabel')
177  ->will($this->returnValue($collectionData));
178 
179  $collectionMock->expects($this->once())
180  ->method('joinStates')
181  ->will($this->returnValue($collectionData));
182 
183  $this->statusFactoryMock->method('create')
184  ->willReturnSelf();
185 
186  $this->statusFactoryMock->method('load')
187  ->willReturn($this->orderStatusModel);
188 
189  $storeMock = $this->createMock(\Magento\Store\Api\Data\StoreInterface::class);
190  $storeMock->method('getId')
191  ->willReturn(1);
192 
193  $this->storeManagerMock->method('getStore')
194  ->with($this->anything())
195  ->willReturn($storeMock);
196 
197  $this->orderStatusModel->setData('store_labels', [1 => 'Pending label']);
198 
199  $result = $this->salesConfig->getStateStatuses($state, $joinLabels);
200  $this->assertSame($expectedResult, $result);
201 
202  // checking data cached in private property
203  $this->assertSame($result, $this->salesConfig->getStateStatuses($state, $joinLabels));
204  }
205 
211  public function getStatusesDataProvider()
212  {
213  return [
214  'processing state' => [
215  'state' => 'processing',
216  'joinLabels' => false,
217  'collectionData' => [
218  new DataObject(
219  [
220  'status' => 'fraud',
221  'state' => 'processing',
222  'store_label' => 'Suspected Fraud',
223  ]
224  ),
225  new DataObject(
226  [
227  'status' => 'processing',
228  'state' => 'processing',
229  'store_label' => 'Processing',
230  ]
231  ),
232  ],
233  'expectedResult' => [
234  0 => 'fraud',
235  1 => 'processing'
236  ],
237  ],
238  'pending state' => [
239  'state' => 'pending',
240  'joinLabels' => true,
241  'collectionData' => [
242  new DataObject(
243  [
244  'status' => 'pending_status',
245  'state' => 'pending',
246  'store_label' => 'Pending label',
247  ]
248  ),
249  ],
250  'expectedResult' => [
251  'pending_status' => 'Pending label'
252  ],
253  ],
254  ];
255  }
256 }
$objectManager
Definition: bootstrap.php:17
testGetStatuses($state, $joinLabels, $collectionData, $expectedResult)
Definition: ConfigTest.php:161