1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Abstract test case to test positions of a module's total collectors as compared to other collectors
*/
namespace Magento\Sales\Model;
abstract class AbstractCollectorPositionsTest extends \PHPUnit\Framework\TestCase
{
/**
* @param string $collectorCode
* @param string $configType
* @param array $before
* @param array $after
*
* @dataProvider collectorPositionDataProvider
*/
public function testCollectorPosition($collectorCode, $configType, array $before, array $after)
{
$allCollectors = self::_getConfigCollectors($configType);
$collectorCodes = array_keys($allCollectors);
$collectorPos = array_search($collectorCode, $collectorCodes);
$this->assertNotSame(false, $collectorPos, "'{$collectorCode}' total collector is not found");
foreach ($before as $compareWithCode) {
$compareWithPos = array_search($compareWithCode, $collectorCodes);
if ($compareWithPos === false) {
continue;
}
$this->assertLessThan(
$compareWithPos,
$collectorPos,
"The '{$collectorCode}' collector must go before '{$compareWithCode}'"
);
}
foreach ($after as $compareWithCode) {
$compareWithPos = array_search($compareWithCode, $collectorCodes);
if ($compareWithPos === false) {
continue;
}
$this->assertGreaterThan(
$compareWithPos,
$collectorPos,
"The '{$collectorCode}' collector must go after '{$compareWithCode}'"
);
}
}
/**
* Return array of total collectors for the designated $configType
*
* @var string $configType
* @throws \InvalidArgumentException
* @return array
*/
protected static function _getConfigCollectors($configType)
{
switch ($configType) {
case 'quote':
$configClass = \Magento\Quote\Model\Quote\Address\Total\Collector::class;
$methodGetCollectors = 'getCollectors';
break;
case 'invoice':
$configClass = \Magento\Sales\Model\Order\Invoice\Config::class;
$methodGetCollectors = 'getTotalModels';
break;
case 'creditmemo':
$configClass = \Magento\Sales\Model\Order\Creditmemo\Config::class;
$methodGetCollectors = 'getTotalModels';
break;
default:
throw new \InvalidArgumentException('Unknown config type: ' . $configType);
}
$config = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($configClass);
return $config->{$methodGetCollectors}();
}
/**
* Data provider with the data to verify
*
* @return array
*/
abstract public function collectorPositionDataProvider();
}