Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
TierPriceValidatorTest.php
Go to the documentation of this file.
1 <?php
8 
14 class TierPriceValidatorTest extends \PHPUnit\Framework\TestCase
15 {
19  private $tierPriceValidator;
20 
24  private $productIdLocator;
25 
29  private $searchCriteriaBuilder;
30 
34  private $filterBuilder;
35 
39  private $customerGroupRepository;
40 
44  private $websiteRepository;
45 
49  private $validationResult;
50 
55  private $invalidSkuProcessor;
56 
60  private $tierPrice;
61 
65  protected function setUp()
66  {
67  $this->productIdLocator = $this->getMockBuilder(\Magento\Catalog\Model\ProductIdLocatorInterface::class)
68  ->disableOriginalConstructor()->getMockForAbstractClass();
69  $this->searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class)
70  ->disableOriginalConstructor()->getMock();
71  $this->filterBuilder = $this->getMockBuilder(\Magento\Framework\Api\FilterBuilder::class)
72  ->disableOriginalConstructor()->getMock();
73  $this->customerGroupRepository = $this->getMockBuilder(\Magento\Customer\Api\GroupRepositoryInterface::class)
74  ->disableOriginalConstructor()->getMockForAbstractClass();
75  $this->websiteRepository = $this->getMockBuilder(\Magento\Store\Api\WebsiteRepositoryInterface::class)
76  ->disableOriginalConstructor()->getMockForAbstractClass();
77  $this->validationResult = $this->getMockBuilder(\Magento\Catalog\Model\Product\Price\Validation\Result::class)
78  ->disableOriginalConstructor()->getMock();
79  $this->invalidSkuProcessor = $this
80  ->getMockBuilder(\Magento\Catalog\Model\Product\Price\Validation\InvalidSkuProcessor::class)
81  ->disableOriginalConstructor()->getMock();
82  $this->tierPrice = $this->getMockBuilder(\Magento\Catalog\Api\Data\TierPriceInterface::class)
83  ->disableOriginalConstructor()->getMockForAbstractClass();
84 
85  $objectManagerHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
86  $this->tierPriceValidator = $objectManagerHelper->getObject(
87  \Magento\Catalog\Model\Product\Price\Validation\TierPriceValidator::class,
88  [
89  'productIdLocator' => $this->productIdLocator,
90  'searchCriteriaBuilder' => $this->searchCriteriaBuilder,
91  'filterBuilder' => $this->filterBuilder,
92  'customerGroupRepository' => $this->customerGroupRepository,
93  'websiteRepository' => $this->websiteRepository,
94  'validationResult' => $this->validationResult,
95  'invalidSkuProcessor' => $this->invalidSkuProcessor
96  ]
97  );
98  }
99 
106  private function prepareCustomerGroupRepositoryMock(array $returned)
107  {
108  $searchCriteria = $this
109  ->getMockBuilder(\Magento\Framework\Api\Search\SearchCriteriaInterface::class)
110  ->disableOriginalConstructor()->getMock();
111  $filter = $this->getMockBuilder(\Magento\Framework\Api\AbstractSimpleObject::class)
112  ->disableOriginalConstructor()->getMockForAbstractClass();
113  $this->filterBuilder->expects($this->atLeastOnce())->method('setField')->willReturnSelf();
114  $this->filterBuilder->expects($this->atLeastOnce())->method('setValue')->willReturnSelf();
115  $this->filterBuilder->expects($this->atLeastOnce())->method('create')->willReturn($filter);
116  $this->searchCriteriaBuilder->expects($this->atLeastOnce())->method('addFilters')->willReturnSelf();
117  $this->searchCriteriaBuilder->expects($this->atLeastOnce())->method('create')->willReturn($searchCriteria);
118  $customerGroupSearchResults = $this
119  ->getMockBuilder(\Magento\Customer\Api\Data\GroupSearchResultsInterface::class)
120  ->disableOriginalConstructor()->getMock();
121  $customerGroupSearchResults->expects($this->once())->method('getItems')
122  ->willReturn($returned['customerGroupSearchResults_getItems']);
123  $this->customerGroupRepository->expects($this->atLeastOnce())->method('getList')
124  ->willReturn($customerGroupSearchResults);
125  }
126 
134  private function prepareRetrieveValidationResultMethod($sku, array $returned)
135  {
136  $this->tierPrice->expects($this->atLeastOnce())->method('getSku')->willReturn($sku);
137  $tierPriceValue = 104;
138  $this->tierPrice->expects($this->atLeastOnce())->method('getPrice')->willReturn($tierPriceValue);
139  $this->tierPrice->expects($this->atLeastOnce())->method('getPriceType')
140  ->willReturn($returned['tierPrice_getPriceType']);
141  $qty = 0;
142  $this->tierPrice->expects($this->atLeastOnce())->method('getQuantity')->willReturn($qty);
143  $websiteId = 0;
144  $invalidWebsiteId = 4;
145  $this->tierPrice->expects($this->atLeastOnce())->method('getWebsiteId')
146  ->willReturnOnConsecutiveCalls($websiteId, $websiteId, $websiteId, $invalidWebsiteId, $websiteId);
147  $this->tierPrice->expects($this->atLeastOnce())->method('getCustomerGroup')
148  ->willReturn($returned['tierPrice_getCustomerGroup']);
149  $skuDiff = [$sku];
150  $this->invalidSkuProcessor->expects($this->atLeastOnce())->method('retrieveInvalidSkuList')
151  ->willReturn($skuDiff);
152  $productId = 3346346;
153  $productType = \Magento\Catalog\Model\Product\Type::TYPE_BUNDLE;
154  $idsBySku = [
155  $sku => [$productId => $productType]
156  ];
157  $this->productIdLocator->expects($this->atLeastOnce())->method('retrieveProductIdsBySkus')
158  ->willReturn($idsBySku);
159  }
160 
166  public function testValidateSkus()
167  {
168  $skus = ['SDFS234234'];
169  $this->invalidSkuProcessor->expects($this->atLeastOnce())
170  ->method('filterSkuList')
171  ->with($skus, [])
172  ->willReturn($skus);
173 
174  $this->assertEquals($skus, $this->tierPriceValidator->validateSkus($skus));
175  }
176 
184  public function testRetrieveValidationResult(array $returned)
185  {
186  $sku = 'ASDF234234';
187  $prices = [$this->tierPrice];
188  $existingPrices = [$this->tierPrice];
189  $this->prepareRetrieveValidationResultMethod($sku, $returned);
190  $website = $this->getMockBuilder(\Magento\Store\Api\Data\WebsiteInterface::class)
191  ->disableOriginalConstructor()->getMockForAbstractClass();
192  $this->websiteRepository->expects($this->atLeastOnce())->method('getById')->willReturn($website);
193  $this->prepareCustomerGroupRepositoryMock($returned);
194 
195  $this->assertEquals(
196  $this->validationResult,
197  $this->tierPriceValidator->retrieveValidationResult($prices, $existingPrices)
198  );
199  }
200 
207  {
208  $customerGroupName = 'test_Group';
209  $customerGroup = $this->getMockBuilder(\Magento\Customer\Api\Data\GroupInterface::class)
210  ->setMethods(['getCode', 'getId'])
211  ->disableOriginalConstructor()->getMockForAbstractClass();
212  $customerGroup->expects($this->atLeastOnce())->method('getCode')->willReturn($customerGroupName);
213  $customerGroupId = 23;
214  $customerGroup->expects($this->atLeastOnce())->method('getId')->willReturn($customerGroupId);
215 
216  return [
217  [
218  [
219  'tierPrice_getCustomerGroup' => $customerGroupName,
221  'customerGroupSearchResults_getItems' => [$customerGroup]
222  ]
223  ],
224  [
225  [
226  'tierPrice_getCustomerGroup' => $customerGroupName,
228  'customerGroupSearchResults_getItems' => []
229  ]
230  ]
231  ];
232  }
233 
240  {
241  $sku = 'ASDF234234';
242  $customerGroupName = 'test_Group';
243  $prices = [$this->tierPrice];
244  $existingPrices = [$this->tierPrice];
245  $returned = [
247  'customerGroupSearchResults_getItems' => [],
248  'tierPrice_getCustomerGroup' => $customerGroupName,
249  ];
250  $this->prepareRetrieveValidationResultMethod($sku, $returned);
251  $exception = new \Magento\Framework\Exception\NoSuchEntityException();
252  $this->websiteRepository->expects($this->atLeastOnce())->method('getById')->willThrowException($exception);
253  $this->prepareCustomerGroupRepositoryMock($returned);
254 
255  $this->assertEquals(
256  $this->validationResult,
257  $this->tierPriceValidator->retrieveValidationResult($prices, $existingPrices)
258  );
259  }
260 }
foreach($websiteCodes as $websiteCode) $skus
$searchCriteria