RateTest.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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\Calculation;
  7. class RateTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\TestFramework\Unit\Helper\ObjectManager
  11. */
  12. protected $objectHelper;
  13. /**
  14. * @var \PHPUnit_Framework_MockObject_MockObject
  15. */
  16. protected $resourceMock;
  17. /**
  18. * Init data
  19. */
  20. protected function setUp()
  21. {
  22. $this->objectHelper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
  23. $this->resourceMock = $this->createPartialMock(
  24. \Magento\Framework\Model\ResourceModel\AbstractResource::class,
  25. [
  26. '_construct',
  27. 'getConnection',
  28. 'getIdFieldName',
  29. 'beginTransaction',
  30. 'rollBack'
  31. ]
  32. );
  33. $this->resourceMock->expects($this->any())->method('beginTransaction')->will($this->returnSelf());
  34. }
  35. /**
  36. * Check if validation throws exceptions in case of incorrect input data
  37. *
  38. * @param string $exceptionMessage
  39. * @param array $data
  40. *
  41. * @dataProvider exceptionOfValidationDataProvider
  42. */
  43. public function testExceptionOfValidation($exceptionMessage, $data)
  44. {
  45. $this->expectException(\Magento\Framework\Exception\LocalizedException::class);
  46. $this->expectExceptionMessage($exceptionMessage);
  47. $rate = $this->objectHelper->getObject(
  48. \Magento\Tax\Model\Calculation\Rate::class,
  49. ['resource' => $this->resourceMock]
  50. );
  51. foreach ($data as $key => $value) {
  52. $rate->setData($key, $value);
  53. }
  54. $rate->beforeSave();
  55. }
  56. /**
  57. * Data provider
  58. *
  59. * @return array
  60. */
  61. public function exceptionOfValidationDataProvider()
  62. {
  63. return [
  64. 'fill all required fields 1' => [
  65. 'exceptionMessage' => 'The required information is invalid. Verify the information and try again.',
  66. 'data' => [
  67. 'zip_is_range' => true,
  68. 'zip_from' => '0111',
  69. 'zip_to' => '',
  70. 'code' => '',
  71. 'tax_country_id' => '',
  72. 'rate' => '',
  73. 'tax_postcode' => '',
  74. ],
  75. ],
  76. 'fill all required fields 2' => [
  77. 'exceptionMessage' => 'The required information is invalid. Verify the information and try again.',
  78. 'data' => [
  79. 'zip_is_range' => '',
  80. 'zip_from' => '',
  81. 'zip_to' => '',
  82. 'code' => '',
  83. 'tax_country_id' => '',
  84. 'rate' => '0.2',
  85. 'tax_postcode' => '1234',
  86. ],
  87. ],
  88. 'positive number' => [
  89. 'exceptionMessage' => 'The Rate Percent is invalid. Enter a positive number and try again.',
  90. 'data' => [
  91. 'zip_is_range' => '',
  92. 'zip_from' => '',
  93. 'zip_to' => '',
  94. 'code' => 'code',
  95. 'tax_country_id' => 'US',
  96. 'rate' => '-1',
  97. 'tax_postcode' => '1234',
  98. ],
  99. ],
  100. 'zip code length' => [
  101. 'exceptionMessage' => 'The ZIP Code length is invalid. '
  102. . 'Verify that the length is nine characters or fewer and try again.',
  103. 'data' => [
  104. 'zip_is_range' => true,
  105. 'zip_from' => '1234567890',
  106. 'zip_to' => '1234',
  107. 'code' => 'code',
  108. 'tax_country_id' => 'US',
  109. 'rate' => '1.1',
  110. 'tax_postcode' => '1234',
  111. ],
  112. ],
  113. 'contain characters' => [
  114. 'exceptionMessage' => 'The ZIP Code is invalid. Use numbers only.',
  115. 'data' => [
  116. 'zip_is_range' => true,
  117. 'zip_from' => 'foo',
  118. 'zip_to' => '1234',
  119. 'code' => 'code',
  120. 'tax_country_id' => 'US',
  121. 'rate' => '1.1',
  122. 'tax_postcode' => '1234',
  123. ],
  124. ],
  125. 'equal or greater' => [
  126. 'exceptionMessage' => 'Range To should be equal or greater than Range From.',
  127. 'data' => [
  128. 'zip_is_range' => true,
  129. 'zip_from' => '321',
  130. 'zip_to' => '123',
  131. 'code' => 'code',
  132. 'tax_country_id' => 'US',
  133. 'rate' => '1.1',
  134. 'tax_postcode' => '1234',
  135. ],
  136. ],
  137. ];
  138. }
  139. }