CalculationTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Tax\Test\Unit\Model\ResourceModel;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class CalculationTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * Tests the building of the search templates for the postal code
  12. *
  13. * @param string $postalCode
  14. * @param string|null $exactPostalcode
  15. * @dataProvider dataProviderCreateSearchPostCodeTemplates
  16. */
  17. public function testCreateSearchPostCodeTemplates($postalCode, $exactPostalcode)
  18. {
  19. // create the mocks
  20. $resource = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  21. $storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
  22. $taxData = $this->createPartialMock(\Magento\Tax\Helper\Data::class, ['getPostCodeSubStringLength']);
  23. $taxData
  24. ->expects($this->any())
  25. ->method('getPostCodeSubStringLength')
  26. ->will($this->returnValue(10));
  27. $objectManager = new ObjectManager($this);
  28. $calcMock = $objectManager->getObject(
  29. \Magento\Tax\Model\ResourceModel\Calculation::class,
  30. [
  31. 'resource' => $resource,
  32. 'taxData' => $taxData,
  33. 'storeManager' => $storeManager
  34. ]
  35. );
  36. // get access to the method
  37. $method = new \ReflectionMethod(
  38. \Magento\Tax\Model\ResourceModel\Calculation::class,
  39. '_createSearchPostCodeTemplates'
  40. );
  41. $method->setAccessible(true);
  42. // test & verify
  43. $resultsArr = $method->invokeArgs($calcMock, [$postalCode, $exactPostalcode]);
  44. $this->verifyResults($resultsArr, $postalCode, $exactPostalcode);
  45. }
  46. /**
  47. * Verify the results array, based on certain properties of the codes
  48. *
  49. * @param array $resultsArr
  50. * @param string $code1
  51. * @param string|null $code2
  52. */
  53. private function verifyResults($resultsArr, $code1, $code2 = null)
  54. {
  55. // determine expected size of the results array
  56. $expectedSize = strlen($code1) + 1; // array will also include the vanilla 'code1' value
  57. if ($code2) {
  58. $expectedSize = strlen($code2) + 2; // array will include both 'code1' and 'code2'
  59. }
  60. $actualSize = count($resultsArr);
  61. $this->assertEquals(
  62. $expectedSize,
  63. $actualSize,
  64. 'Expected size of the result array was ' . $expectedSize . ' but actual was ' . $actualSize
  65. );
  66. // verify code(s) are present within the array
  67. $this->assertTrue(in_array($code1, $resultsArr, 'Expected to find code "' . $code1 . '"'));
  68. if ($code2) {
  69. $this->assertTrue(in_array($code2, $resultsArr, 'Expected to find code "' . $code2 . '"'));
  70. }
  71. }
  72. /**
  73. * @return array
  74. */
  75. public function dataProviderCreateSearchPostCodeTemplates()
  76. {
  77. return [
  78. 'USA basic' => ['78729', null],
  79. 'USA zip+4' => ['54321', '12345-6789'],
  80. 'Poland' => ['05-509', null]
  81. ];
  82. }
  83. }