AbstractCollectorPositionsTest.php 3.0 KB

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