RateRepositoryTest.php 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Model\Calculation;
  7. use Magento\Framework\Exception\InputException;
  8. use Magento\Framework\Exception\NoSuchEntityException;
  9. use Magento\Tax\Api\Data\TaxRateInterface;
  10. use Magento\Tax\Model\Calculation\Rate;
  11. use Magento\Tax\Model\TaxRuleFixtureFactory;
  12. use Magento\TestFramework\Helper\Bootstrap;
  13. /**
  14. * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
  15. */
  16. class RateRepositoryTest extends \PHPUnit\Framework\TestCase
  17. {
  18. /**
  19. * Object Manager
  20. *
  21. * @var \Magento\Framework\ObjectManagerInterface
  22. */
  23. private $objectManager;
  24. /**
  25. * TaxRate factory
  26. *
  27. * @var \Magento\Tax\Api\Data\TaxRateInterfaceFactory
  28. */
  29. private $taxRateFactory;
  30. /**
  31. * TaxRateService
  32. *
  33. * @var \Magento\Tax\Api\TaxRateRepositoryInterface
  34. */
  35. private $rateRepository;
  36. /**
  37. * Helps in creating required tax rules.
  38. *
  39. * @var TaxRuleFixtureFactory
  40. */
  41. private $taxRateFixtureFactory;
  42. /**
  43. * @var \Magento\Directory\Model\CountryFactory
  44. */
  45. private $countryFactory;
  46. /**
  47. * @var \Magento\Directory\Model\RegionFactory
  48. */
  49. private $regionFactory;
  50. /**
  51. * @var \Magento\Framework\Api\DataObjectHelper
  52. */
  53. private $dataObjectHelper;
  54. protected function setUp()
  55. {
  56. $this->objectManager = Bootstrap::getObjectManager();
  57. $this->rateRepository = $this->objectManager->get(\Magento\Tax\Api\TaxRateRepositoryInterface::class);
  58. $this->taxRateFactory = $this->objectManager->create(\Magento\Tax\Api\Data\TaxRateInterfaceFactory::class);
  59. $this->dataObjectHelper = $this->objectManager->create(\Magento\Framework\Api\DataObjectHelper::class);
  60. $this->taxRateFixtureFactory = new TaxRuleFixtureFactory();
  61. $this->countryFactory = $this->objectManager->create(\Magento\Directory\Model\CountryFactory::class);
  62. $this->regionFactory = $this->objectManager->create(\Magento\Directory\Model\RegionFactory::class);
  63. }
  64. /**
  65. * @magentoDbIsolation enabled
  66. */
  67. public function testSave()
  68. {
  69. $taxData = [
  70. 'tax_country_id' => 'US',
  71. 'tax_region_id' => '8',
  72. 'rate' => '8.25',
  73. 'code' => 'US-CA-*-Rate' . rand(),
  74. 'zip_is_range' => true,
  75. 'zip_from' => 78765,
  76. 'zip_to' => 78780,
  77. ];
  78. // Tax rate data object created
  79. $taxRate = $this->taxRateFactory->create();
  80. $this->dataObjectHelper->populateWithArray($taxRate, $taxData, \Magento\Tax\Api\Data\TaxRateInterface::class);
  81. //Tax rate service call
  82. $taxRateServiceData = $this->rateRepository->save($taxRate);
  83. //Assertions
  84. $this->assertInstanceOf(\Magento\Tax\Api\Data\TaxRateInterface::class, $taxRateServiceData);
  85. $this->assertEquals($taxData['tax_country_id'], $taxRateServiceData->getTaxCountryId());
  86. $this->assertEquals($taxData['tax_region_id'], $taxRateServiceData->getTaxRegionId());
  87. $this->assertEquals($taxData['rate'], $taxRateServiceData->getRate());
  88. $this->assertEquals($taxData['code'], $taxRateServiceData->getCode());
  89. $this->assertEquals($taxData['zip_from'], $taxRateServiceData->getZipFrom());
  90. $this->assertEquals($taxData['zip_to'], $taxRateServiceData->getZipTo());
  91. $this->assertEquals('78765-78780', $taxRateServiceData->getTaxPostcode());
  92. $this->assertNotNull($taxRateServiceData->getId());
  93. }
  94. /**
  95. * @magentoDbIsolation enabled
  96. */
  97. public function testSaveWithZeroValue()
  98. {
  99. $taxData = [
  100. 'tax_country_id' => 'US',
  101. 'tax_region_id' => '8',
  102. 'rate' => '0',
  103. 'code' => 'US-CA-*-Rate' . rand(),
  104. 'zip_is_range' => true,
  105. 'zip_from' => 78765,
  106. 'zip_to' => 78780,
  107. ];
  108. // Tax rate data object created
  109. $taxRate = $this->taxRateFactory->create();
  110. $this->dataObjectHelper->populateWithArray($taxRate, $taxData, \Magento\Tax\Api\Data\TaxRateInterface::class);
  111. //Tax rate service call
  112. $taxRateServiceData = $this->rateRepository->save($taxRate);
  113. //Assertions
  114. $this->assertInstanceOf(\Magento\Tax\Api\Data\TaxRateInterface::class, $taxRateServiceData);
  115. $this->assertEquals($taxData['tax_country_id'], $taxRateServiceData->getTaxCountryId());
  116. $this->assertEquals($taxData['tax_region_id'], $taxRateServiceData->getTaxRegionId());
  117. $this->assertEquals($taxData['rate'], $taxRateServiceData->getRate());
  118. $this->assertEquals($taxData['code'], $taxRateServiceData->getCode());
  119. $this->assertEquals($taxData['zip_from'], $taxRateServiceData->getZipFrom());
  120. $this->assertEquals($taxData['zip_to'], $taxRateServiceData->getZipTo());
  121. $this->assertEquals('78765-78780', $taxRateServiceData->getTaxPostcode());
  122. $this->assertNotNull($taxRateServiceData->getId());
  123. }
  124. /**
  125. * @magentoDbIsolation enabled
  126. * @magentoDataFixture Magento/Store/_files/store.php
  127. */
  128. public function testSaveWithTitles()
  129. {
  130. $store = $this->objectManager->get(\Magento\Store\Model\Store::class);
  131. $store->load('test', 'code');
  132. $taxData = [
  133. 'tax_country_id' => 'US',
  134. 'tax_region_id' => '8',
  135. 'rate' => '8.25',
  136. 'code' => 'US-CA-*-Rate' . rand(),
  137. 'zip_is_range' => true,
  138. 'zip_from' => 78765,
  139. 'zip_to' => 78780,
  140. 'titles' => [
  141. [
  142. 'store_id' => $store->getId(),
  143. 'value' => 'random store title',
  144. ],
  145. ],
  146. ];
  147. // Tax rate data object created
  148. $taxRate = $this->taxRateFactory->create();
  149. $this->dataObjectHelper->populateWithArray($taxRate, $taxData, \Magento\Tax\Api\Data\TaxRateInterface::class);
  150. //Tax rate service call
  151. $taxRateServiceData = $this->rateRepository->save($taxRate);
  152. //Assertions
  153. $this->assertInstanceOf(\Magento\Tax\Api\Data\TaxRateInterface::class, $taxRateServiceData);
  154. $this->assertEquals($taxData['tax_country_id'], $taxRateServiceData->getTaxCountryId());
  155. $this->assertEquals($taxData['tax_region_id'], $taxRateServiceData->getTaxRegionId());
  156. $this->assertEquals($taxData['rate'], $taxRateServiceData->getRate());
  157. $this->assertEquals($taxData['code'], $taxRateServiceData->getCode());
  158. $this->assertEquals($taxData['zip_from'], $taxRateServiceData->getZipFrom());
  159. $this->assertEquals($taxData['zip_to'], $taxRateServiceData->getZipTo());
  160. $this->assertEquals('78765-78780', $taxRateServiceData->getTaxPostcode());
  161. $this->assertNotNull($taxRateServiceData->getId());
  162. $titles = $taxRateServiceData->getTitles();
  163. $this->assertEquals(1, count($titles));
  164. $this->assertEquals($store->getId(), $titles[0]->getStoreId());
  165. $this->assertEquals($taxData['titles'][0]['value'], $titles[0]->getValue());
  166. $taxRateServiceData = $this->rateRepository->get($taxRateServiceData->getId());
  167. $titles = $taxRateServiceData->getTitles();
  168. $this->assertEquals(1, count($titles));
  169. $this->assertEquals($store->getId(), $titles[0]->getStoreId());
  170. $this->assertEquals($taxData['titles'][0]['value'], $titles[0]->getValue());
  171. }
  172. /**
  173. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  174. * @expectedExceptionMessage No such entity with taxRateId = 9999
  175. * @magentoDbIsolation enabled
  176. */
  177. public function testSaveThrowsExceptionIfTargetTaxRateDoesNotExist()
  178. {
  179. $invalidTaxData = [
  180. 'id' => 9999,
  181. 'tax_country_id' => 'US',
  182. 'tax_region_id' => '8',
  183. 'rate' => '8.25',
  184. 'code' => 'US-CA-*-Rate' . rand(),
  185. 'zip_is_range' => true,
  186. 'zip_from' => 78765,
  187. 'zip_to' => 78780,
  188. ];
  189. $taxRate = $this->taxRateFactory->create();
  190. $this->dataObjectHelper->populateWithArray(
  191. $taxRate,
  192. $invalidTaxData,
  193. \Magento\Tax\Api\Data\TaxRateInterface::class
  194. );
  195. $this->rateRepository->save($taxRate);
  196. }
  197. /**
  198. * @expectedException \Magento\Framework\Exception\AlreadyExistsException
  199. * @expectedExceptionMessage Code already exists.
  200. * @magentoDbIsolation enabled
  201. */
  202. public function testSaveThrowsExceptionIfTaxRateWithCorrespondingCodeAlreadyExists()
  203. {
  204. $invalidTaxData = [
  205. 'tax_country_id' => 'US',
  206. 'tax_region_id' => '8',
  207. 'rate' => '8.25',
  208. 'code' => 'US-CA-*-Rate' . rand(),
  209. 'zip_is_range' => true,
  210. 'zip_from' => 78765,
  211. 'zip_to' => 78780,
  212. ];
  213. $taxRate1 = $this->taxRateFactory->create();
  214. $this->dataObjectHelper->populateWithArray(
  215. $taxRate1,
  216. $invalidTaxData,
  217. \Magento\Tax\Api\Data\TaxRateInterface::class
  218. );
  219. $taxRate2 = $this->taxRateFactory->create();
  220. $this->dataObjectHelper->populateWithArray(
  221. $taxRate2,
  222. $invalidTaxData,
  223. \Magento\Tax\Api\Data\TaxRateInterface::class
  224. );
  225. //Service call initiated twice to add the same code
  226. $this->rateRepository->save($taxRate1);
  227. $this->rateRepository->save($taxRate2);
  228. }
  229. /**
  230. * @param array $dataArray
  231. * @param string $errorMessages
  232. * @throws \Magento\Framework\Exception\InputException
  233. *
  234. * @dataProvider createDataProvider
  235. * @expectedException \Magento\Framework\Exception\InputException
  236. * @magentoDbIsolation enabled
  237. */
  238. public function testSaveThrowsExceptionIfGivenDataIsInvalid($dataArray, $errorMessages)
  239. {
  240. $taxRate = $this->taxRateFactory->create();
  241. $this->dataObjectHelper->populateWithArray($taxRate, $dataArray, \Magento\Tax\Api\Data\TaxRateInterface::class);
  242. try {
  243. $this->rateRepository->save($taxRate);
  244. } catch (InputException $exception) {
  245. $errors = $exception->getErrors();
  246. foreach ($errors as $key => $error) {
  247. $this->assertEquals($errorMessages[$key], $error->getMessage());
  248. }
  249. throw $exception;
  250. }
  251. }
  252. /**
  253. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  254. */
  255. public function createDataProvider()
  256. {
  257. return [
  258. 'invalidZipRange' => [
  259. [
  260. 'zip_is_range' => true,
  261. 'zip_from' => 'from',
  262. 'zip_to' => 'to',
  263. ],
  264. 'error' => [
  265. '"country_id" is required. Enter and try again.',
  266. '"percentage_rate" is required. Enter and try again.',
  267. '"code" is required. Enter and try again.',
  268. 'Invalid value of "from" provided for the zip_from field.',
  269. 'Invalid value of "to" provided for the zip_to field.',
  270. ],
  271. ],
  272. 'emptyZipRange' => [
  273. [
  274. 'zip_is_range' => true,
  275. 'zip_from' => '',
  276. 'zip_to' => '',
  277. ],
  278. 'error' => [
  279. '"country_id" is required. Enter and try again.',
  280. '"percentage_rate" is required. Enter and try again.',
  281. '"code" is required. Enter and try again.',
  282. 'Invalid value of "" provided for the zip_from field.',
  283. 'Invalid value of "" provided for the zip_to field.',
  284. ],
  285. ],
  286. 'empty' => [
  287. [],
  288. 'error' => [
  289. '"country_id" is required. Enter and try again.',
  290. '"percentage_rate" is required. Enter and try again.',
  291. '"code" is required. Enter and try again.',
  292. '"postcode" is required. Enter and try again.',
  293. ],
  294. ],
  295. 'zipRangeAndPostcode' => [
  296. [
  297. 'postcode' => 78727,
  298. 'zip_is_range' => true,
  299. 'zip_from' => 78765,
  300. 'zip_to' => 78780,
  301. ],
  302. 'error' => [
  303. '"country_id" is required. Enter and try again.',
  304. '"percentage_rate" is required. Enter and try again.',
  305. '"code" is required. Enter and try again.',
  306. ],
  307. ],
  308. 'higherRange' => [
  309. [
  310. 'zip_is_range' => true,
  311. 'zip_from' => 78765,
  312. 'zip_to' => 78780,
  313. ],
  314. 'error' => [
  315. '"country_id" is required. Enter and try again.',
  316. '"percentage_rate" is required. Enter and try again.',
  317. '"code" is required. Enter and try again.',
  318. 'Range To should be equal or greater than Range From.',
  319. ],
  320. ],
  321. 'invalidCountry' => [
  322. ['tax_country_id' => 'XX'],
  323. 'error' => [
  324. 'Invalid value of "XX" provided for the country_id field.',
  325. '"percentage_rate" is required. Enter and try again.',
  326. '"code" is required. Enter and try again.',
  327. '"postcode" is required. Enter and try again.',
  328. ],
  329. ],
  330. 'invalidCountry2' => [
  331. ['tax_country_id' => ' '],
  332. 'error' => [
  333. '"country_id" is required. Enter and try again.',
  334. '"percentage_rate" is required. Enter and try again.',
  335. '"code" is required. Enter and try again.',
  336. '"postcode" is required. Enter and try again.',
  337. ],
  338. ],
  339. 'invalidRegion1' => [
  340. ['tax_region_id' => '-'],
  341. 'error' => [
  342. '"country_id" is required. Enter and try again.',
  343. 'Invalid value of "-" provided for the region_id field.',
  344. '"percentage_rate" is required. Enter and try again.',
  345. '"code" is required. Enter and try again.',
  346. '"postcode" is required. Enter and try again.',
  347. ],
  348. ],
  349. 'spaceRegion' => [
  350. ['tax_region_id' => ' '],
  351. 'error' => [
  352. '"country_id" is required. Enter and try again.',
  353. '"percentage_rate" is required. Enter and try again.',
  354. '"code" is required. Enter and try again.',
  355. '"postcode" is required. Enter and try again.',
  356. ],
  357. ],
  358. 'emptyPercentageRate' => [
  359. [
  360. 'tax_country_id' => 'US',
  361. 'tax_region_id' => '8',
  362. 'rate' => '',
  363. 'code' => 'US-CA-*-Rate' . rand(),
  364. 'zip_is_range' => true,
  365. 'zip_from' => 78765,
  366. 'zip_to' => 78780,
  367. ],
  368. 'error' => [
  369. '"percentage_rate" is required. Enter and try again.',
  370. ],
  371. ]
  372. ];
  373. }
  374. /**
  375. * @magentoDbIsolation enabled
  376. */
  377. public function testGet()
  378. {
  379. $data = [
  380. 'tax_country_id' => 'US',
  381. 'tax_region_id' => '12',
  382. 'tax_postcode' => '*',
  383. 'code' => 'US_12_Code',
  384. 'rate' => '7.5',
  385. ];
  386. $rate = $this->objectManager->create(\Magento\Tax\Model\Calculation\Rate::class)
  387. ->setData($data)
  388. ->save();
  389. $taxRate = $this->rateRepository->get($rate->getId());
  390. $this->assertEquals('US', $taxRate->getTaxCountryId());
  391. $this->assertEquals(12, $taxRate->getTaxRegionId());
  392. $this->assertEquals('*', $taxRate->getTaxPostcode());
  393. $this->assertEquals('US_12_Code', $taxRate->getCode());
  394. $this->assertEquals(7.5, $taxRate->getRate());
  395. $this->assertNull($taxRate->getZipIsRange());
  396. $this->assertNull($taxRate->getZipTo());
  397. $this->assertNull($taxRate->getZipFrom());
  398. }
  399. /**
  400. * @expectedException \Magento\Framework\Exception\NoSuchEntityException
  401. * @expectedExceptionMessage No such entity with taxRateId = 9999
  402. */
  403. public function testGetThrowsExceptionIfTargetTaxRateDoesNotExist()
  404. {
  405. $this->rateRepository->get(9999);
  406. }
  407. /**
  408. * @magentoDbIsolation enabled
  409. */
  410. public function testSaveUpdatesTaxRate()
  411. {
  412. $taxRate = $this->taxRateFactory->create();
  413. $taxRate->setTaxCountryId('US')
  414. ->setTaxRegionId(42)
  415. ->setRate(8.25)
  416. ->setCode('UpdateTaxRates')
  417. ->setTaxPostcode('78780');
  418. $taxRate = $this->rateRepository->save($taxRate);
  419. $updatedTaxRate = $this->taxRateFactory->create();
  420. $updatedTaxRate->setId($taxRate->getId())
  421. ->setCode('UpdateTaxRates')
  422. ->setTaxCountryId('US')
  423. ->setTaxRegionId(42)
  424. ->setRate(8.25)
  425. ->setZipIsRange(true)
  426. ->setZipFrom(78700)
  427. ->setZipTo(78780);
  428. $updatedTaxRate = $this->rateRepository->save($updatedTaxRate);
  429. $retrievedRate = $this->rateRepository->get($taxRate->getId());
  430. // Expect the service to have filled in the new postcode for us
  431. $this->assertEquals($updatedTaxRate->getTaxPostcode(), $retrievedRate->getTaxPostcode());
  432. $this->assertNotEquals($taxRate->getTaxPostcode(), $retrievedRate->getTaxPostcode());
  433. }
  434. /**
  435. * @magentoDbIsolation enabled
  436. * @expectedException \Magento\Framework\Exception\InputException
  437. * @expectedExceptionMessage postcode
  438. */
  439. public function testSaveThrowsExceptionIfTargetTaxRateExistsButProvidedDataIsInvalid()
  440. {
  441. $taxRate = $this->taxRateFactory->create();
  442. $taxRate->setTaxCountryId('US')
  443. ->setTaxRegionId(42)
  444. ->setRate(8.25)
  445. ->setCode('UpdateTaxRates')
  446. ->setTaxPostcode('78780');
  447. $taxRate = $this->rateRepository->save($taxRate);
  448. $updatedTaxRate = $this->taxRateFactory->create();
  449. $updatedTaxRate->setId($taxRate->getId())
  450. ->setTaxCountryId('US')
  451. ->setTaxRegionId(42)
  452. ->setRate(8.25)
  453. ->setCode('UpdateTaxRates')
  454. ->setTaxPostcode(null);
  455. $this->rateRepository->save($updatedTaxRate);
  456. }
  457. /**
  458. * @magentoDbIsolation enabled
  459. */
  460. public function testDeleteById()
  461. {
  462. // Create a new tax rate
  463. $taxRateData = $this->taxRateFactory->create();
  464. $taxRateData->setCode('TX')
  465. ->setTaxCountryId('US')
  466. ->setRate(5)
  467. ->setTaxPostcode(77000)
  468. ->setTaxRegionId(1);
  469. $taxRateId = $this->rateRepository->save($taxRateData)->getId();
  470. // Delete the new tax rate
  471. $this->assertTrue($this->rateRepository->deleteById($taxRateId));
  472. // Get the new tax rate, this should fail
  473. try {
  474. $this->rateRepository->get($taxRateId);
  475. $this->fail('NoSuchEntityException expected but not thrown');
  476. } catch (NoSuchEntityException $e) {
  477. $expectedParams = [
  478. 'fieldName' => 'taxRateId',
  479. 'fieldValue' => $taxRateId,
  480. ];
  481. $this->assertEquals($expectedParams, $e->getParameters());
  482. } catch (\Exception $e) {
  483. $this->fail('Caught unexpected exception');
  484. }
  485. }
  486. /**
  487. * @magentoDbIsolation enabled
  488. */
  489. public function testDeleteThrowsExceptionIfTargetTaxRateDoesNotExist()
  490. {
  491. // Create a new tax rate
  492. $taxRateData = $this->taxRateFactory->create();
  493. $taxRateData->setCode('TX')
  494. ->setTaxCountryId('US')
  495. ->setRate(6)
  496. ->setTaxPostcode(77001)
  497. ->setTaxRegionId(1);
  498. $taxRateId = $this->rateRepository->save($taxRateData)->getId();
  499. // Delete the new tax rate
  500. $this->assertTrue($this->rateRepository->deleteById($taxRateId));
  501. // Delete the new tax rate again, this should fail
  502. try {
  503. $this->rateRepository->deleteById($taxRateId);
  504. $this->fail('NoSuchEntityException expected but not thrown');
  505. } catch (NoSuchEntityException $e) {
  506. $expectedParams = [
  507. 'fieldName' => 'taxRateId',
  508. 'fieldValue' => $taxRateId,
  509. ];
  510. $this->assertEquals($expectedParams, $e->getParameters());
  511. } catch (\Exception $e) {
  512. $this->fail('Caught unexpected exception');
  513. }
  514. }
  515. /**
  516. * @param \Magento\Framework\Api\Filter[] $filters
  517. * @param \Magento\Framework\Api\Filter[] $filterGroup
  518. * @param $expectedRateCodes
  519. *
  520. * @magentoDbIsolation enabled
  521. * @dataProvider searchTaxRatesDataProvider
  522. * @SuppressWarnings(PHPMD.UnusedLocalVariable)
  523. */
  524. public function testGetList($filters, $filterGroup, $expectedRateCodes)
  525. {
  526. $taxRates = $this->taxRateFixtureFactory->createTaxRates(
  527. [
  528. ['percentage' => 7.5, 'country' => 'US', 'region' => '42'],
  529. ['percentage' => 7.5, 'country' => 'US', 'region' => '12'],
  530. ['percentage' => 22.0, 'country' => 'US', 'region' => '42'],
  531. ['percentage' => 10.0, 'country' => 'US', 'region' => '12'],
  532. ]
  533. );
  534. /** @var \Magento\Framework\Api\SearchCriteriaBuilder $searchBuilder */
  535. $searchBuilder = Bootstrap::getObjectManager()
  536. ->create(\Magento\Framework\Api\SearchCriteriaBuilder::class);
  537. foreach ($filters as $filter) {
  538. $searchBuilder->addFilters([$filter]);
  539. }
  540. if ($filterGroup !== null) {
  541. $searchBuilder->addFilters($filterGroup);
  542. }
  543. $searchCriteria = $searchBuilder->create();
  544. $searchResults = $this->rateRepository->getList($searchCriteria);
  545. $this->assertEquals($searchCriteria, $searchResults->getSearchCriteria());
  546. $this->assertEquals(count($expectedRateCodes), $searchResults->getTotalCount());
  547. foreach ($searchResults->getItems() as $rate) {
  548. $this->assertContains($rate->getCode(), $expectedRateCodes);
  549. }
  550. }
  551. public function searchTaxRatesDataProvider()
  552. {
  553. $filterBuilder = Bootstrap::getObjectManager()->create(\Magento\Framework\Api\FilterBuilder::class);
  554. return [
  555. 'eq' => [
  556. [$filterBuilder->setField(Rate::KEY_REGION_ID)->setValue(42)->create()],
  557. null,
  558. ['US - 42 - 7.5', 'US - 42 - 22'],
  559. ],
  560. 'and' => [
  561. [
  562. $filterBuilder->setField(Rate::KEY_REGION_ID)->setValue(42)->create(),
  563. $filterBuilder->setField(Rate::KEY_PERCENTAGE_RATE)->setValue(22.0)->create(),
  564. ],
  565. [],
  566. ['US - 42 - 22'],
  567. ],
  568. 'or' => [
  569. [],
  570. [
  571. $filterBuilder->setField(Rate::KEY_PERCENTAGE_RATE)->setValue(22.0)->create(),
  572. $filterBuilder->setField(Rate::KEY_PERCENTAGE_RATE)->setValue(10.0)->create(),
  573. ],
  574. ['US - 42 - 22', 'US - 12 - 10'],
  575. ],
  576. 'like' => [
  577. [
  578. $filterBuilder->setField(Rate::KEY_CODE)->setValue('%7.5')->setConditionType('like')->create(),
  579. ],
  580. [],
  581. ['US - 42 - 7.5', 'US - 12 - 7.5'],
  582. ],
  583. 'like_region_name' => [
  584. [
  585. $filterBuilder->setField(Rate::KEY_REGION_NAME)
  586. ->setValue('%NM%')
  587. ->setConditionType('like')
  588. ->create(),
  589. ],
  590. null,
  591. ['US - 42 - 7.5', 'US - 42 - 22'],
  592. ],
  593. ];
  594. }
  595. }