CollectionTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Pricing\Test\Unit\Adjustment;
  7. use \Magento\Framework\Pricing\Adjustment\Collection;
  8. class CollectionTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Framework\Pricing\Adjustment\Pool
  12. */
  13. protected $adjustmentPool;
  14. /**
  15. * @var [][]
  16. */
  17. protected $adjustmentsData;
  18. protected function setUp()
  19. {
  20. $adj1 = $this->createMock(\Magento\Framework\Pricing\Adjustment\AdjustmentInterface::class);
  21. $adj1->expects($this->any())
  22. ->method('getSortOrder')
  23. ->will($this->returnValue(10));
  24. $adj2 = $this->createMock(\Magento\Framework\Pricing\Adjustment\AdjustmentInterface::class);
  25. $adj2->expects($this->any())
  26. ->method('getSortOrder')
  27. ->will($this->returnValue(20));
  28. $adj3 = $this->createMock(\Magento\Framework\Pricing\Adjustment\AdjustmentInterface::class);
  29. $adj3->expects($this->any())
  30. ->method('getSortOrder')
  31. ->will($this->returnValue(5));
  32. $adj4 = $this->createMock(\Magento\Framework\Pricing\Adjustment\AdjustmentInterface::class);
  33. $adj4->expects($this->any())
  34. ->method('getSortOrder')
  35. ->will($this->returnValue(\Magento\Framework\Pricing\Adjustment\Pool::DEFAULT_SORT_ORDER));
  36. $adjustmentsData = [
  37. 'adj1' => $adj1,
  38. 'adj2' => $adj2,
  39. 'adj3' => $adj3,
  40. 'adj4' => $adj4,
  41. ];
  42. /** @var \Magento\Framework\Pricing\Adjustment\Pool|\PHPUnit_Framework_MockObject_MockObject $adjustmentPool */
  43. $adjustmentPool = $this->getMockBuilder(\Magento\Framework\Pricing\Adjustment\Pool::class)
  44. ->disableOriginalConstructor()
  45. ->setMethods(['getAdjustmentByCode'])
  46. ->getMock();
  47. $adjustmentPool->expects($this->any())->method('getAdjustmentByCode')->will(
  48. $this->returnCallback(
  49. function ($code) use ($adjustmentsData) {
  50. if (!isset($adjustmentsData[$code])) {
  51. $this->fail(sprintf('Adjustment "%s" not found', $code));
  52. }
  53. return $adjustmentsData[$code];
  54. }
  55. )
  56. );
  57. $this->adjustmentPool = $adjustmentPool;
  58. $this->adjustmentsData = $adjustmentsData;
  59. }
  60. /**
  61. * @param string[] $adjustments
  62. * @param string[] $expectedResult
  63. * @dataProvider getItemsDataProvider
  64. */
  65. public function testGetItems($adjustments, $expectedResult)
  66. {
  67. $collection = new Collection($this->adjustmentPool, $adjustments);
  68. $result = $collection->getItems();
  69. $this->assertEmpty(array_diff($expectedResult, array_keys($result)));
  70. }
  71. /**
  72. * @return array
  73. */
  74. public function getItemsDataProvider()
  75. {
  76. return [
  77. [['adj1'], ['adj1']],
  78. [['adj4'], ['adj4']],
  79. [['adj1', 'adj4'], ['adj1', 'adj4']],
  80. [['adj1', 'adj2', 'adj3', 'adj4'], ['adj3', 'adj1', 'adj2', 'adj4']]
  81. ];
  82. }
  83. /**
  84. * @param string[] $adjustments
  85. * @param string $code
  86. * @param $expectedResult
  87. * @dataProvider getItemByCodeDataProvider
  88. */
  89. public function testGetItemByCode($adjustments, $code, $expectedResult)
  90. {
  91. $collection = new Collection($this->adjustmentPool, $adjustments);
  92. $item = $collection->getItemByCode($code);
  93. $this->assertEquals($expectedResult, $item->getAdjustmentCode());
  94. }
  95. /**
  96. * @return array
  97. */
  98. public function getItemByCodeDataProvider()
  99. {
  100. return [
  101. [['adj1'], 'adj1', $this->adjustmentsData['adj1']],
  102. [['adj1', 'adj2', 'adj3', 'adj4'], 'adj1', $this->adjustmentsData['adj1']],
  103. [['adj1', 'adj2', 'adj3', 'adj4'], 'adj2', $this->adjustmentsData['adj2']],
  104. [['adj1', 'adj2', 'adj3', 'adj4'], 'adj3', $this->adjustmentsData['adj3']],
  105. [['adj1', 'adj2', 'adj3', 'adj4'], 'adj4', $this->adjustmentsData['adj4']],
  106. ];
  107. }
  108. /**
  109. * @expectedException \InvalidArgumentException
  110. */
  111. public function testGetItemByNotExistingCode()
  112. {
  113. $adjustments = ['adj1'];
  114. $collection = new Collection($this->adjustmentPool, $adjustments);
  115. $collection->getItemByCode('not_existing_code');
  116. }
  117. }