CustomerTest.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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\TaxClass\Source;
  7. use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
  8. class CustomerTest extends \PHPUnit\Framework\TestCase
  9. {
  10. /**
  11. * @var \Magento\Tax\Api\TaxClassRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
  12. */
  13. protected $taxClassRepositoryMock;
  14. /**
  15. * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
  16. */
  17. protected $searchCriteriaBuilderMock;
  18. /**
  19. * @var \Magento\Framework\Api\FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
  20. */
  21. protected $filterBuilderMock;
  22. /**
  23. * @var \Magento\Tax\Model\TaxClass\Source\Customer
  24. */
  25. protected $customer;
  26. /**
  27. * @var ObjectManager
  28. */
  29. protected $objectManager;
  30. /**
  31. * Set up
  32. *
  33. * @return void
  34. */
  35. protected function setUp()
  36. {
  37. $this->objectManager = new ObjectManager($this);
  38. $this->taxClassRepositoryMock = $this->getMockForAbstractClass(
  39. \Magento\Tax\Api\TaxClassRepositoryInterface::class,
  40. ['getList'],
  41. '',
  42. false,
  43. true,
  44. true,
  45. []
  46. );
  47. $this->searchCriteriaBuilderMock = $this->createPartialMock(
  48. \Magento\Framework\Api\SearchCriteriaBuilder::class,
  49. ['addFilters', 'create']
  50. );
  51. $this->filterBuilderMock = $this->createPartialMock(
  52. \Magento\Framework\Api\FilterBuilder::class,
  53. ['setField', 'setValue', 'create']
  54. );
  55. $this->customer = $this->objectManager->getObject(
  56. \Magento\Tax\Model\TaxClass\Source\Customer::class,
  57. [
  58. 'taxClassRepository' => $this->taxClassRepositoryMock,
  59. 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
  60. 'filterBuilder' => $this->filterBuilderMock
  61. ]
  62. );
  63. }
  64. /**
  65. * Run test getAllOptions method
  66. *
  67. * @param bool $isEmpty
  68. * @param array $expected
  69. * @dataProvider dataProviderGetAllOptions
  70. */
  71. public function testGetAllOptions($isEmpty, array $expected)
  72. {
  73. $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  74. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  75. $searchResultsMock = $this->getMockForAbstractClass(
  76. \Magento\Tax\Api\Data\TaxClassSearchResultsInterface::class,
  77. [],
  78. '',
  79. false,
  80. true,
  81. true,
  82. ['getItems']
  83. );
  84. $taxClassMock = $this->getMockForAbstractClass(
  85. \Magento\Tax\Api\Data\TaxClassInterface::class,
  86. ['getClassId', 'getClassName'],
  87. '',
  88. false,
  89. true,
  90. true
  91. );
  92. $this->filterBuilderMock->expects($this->once())
  93. ->method('setField')
  94. ->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)
  95. ->willReturnSelf();
  96. $this->filterBuilderMock->expects($this->once())
  97. ->method('setValue')
  98. ->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)
  99. ->willReturnSelf();
  100. $this->filterBuilderMock->expects($this->once())
  101. ->method('create')
  102. ->willReturn($filterMock);
  103. $this->searchCriteriaBuilderMock->expects($this->once())
  104. ->method('addFilters')
  105. ->with([$filterMock])
  106. ->willReturnSelf();
  107. $this->searchCriteriaBuilderMock->expects($this->once())
  108. ->method('create')
  109. ->willReturn($searchCriteriaMock);
  110. $this->taxClassRepositoryMock->expects($this->once())
  111. ->method('getList')
  112. ->with($searchCriteriaMock)
  113. ->willReturn($searchResultsMock);
  114. if (!$isEmpty) {
  115. $taxClassMock->expects($this->once())
  116. ->method('getClassId')
  117. ->willReturn(10);
  118. $taxClassMock->expects($this->once())
  119. ->method('getClassName')
  120. ->willReturn('class-name');
  121. $items = [$taxClassMock];
  122. $searchResultsMock->expects($this->once())
  123. ->method('getItems')
  124. ->willReturn($items);
  125. // checking of a lack of re-initialization
  126. for ($i = 10; --$i;) {
  127. $result = $this->customer->getAllOptions();
  128. $this->assertEquals($expected, $result);
  129. }
  130. } else {
  131. $items = [];
  132. $searchResultsMock->expects($this->once())
  133. ->method('getItems')
  134. ->willReturn($items);
  135. // checking exception
  136. $this->assertEmpty($this->customer->getAllOptions());
  137. }
  138. }
  139. /**
  140. * Data provider for testGetAllOptions
  141. *
  142. * @return array
  143. */
  144. public function dataProviderGetAllOptions()
  145. {
  146. return [
  147. ['isEmpty' => false, 'expected' => [['value' => 10, 'label' => 'class-name']]],
  148. ['isEmpty' => true, 'expected' => []]
  149. ];
  150. }
  151. /**
  152. * Run test getAllOptions method for names integrity
  153. *
  154. * @param array $value
  155. * @dataProvider dataProviderGetAllOptionsNameIntegrity
  156. */
  157. public function testGetAllOptionsNameIntegrity(array $value)
  158. {
  159. $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
  160. $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
  161. $searchResultsMock = $this->getMockForAbstractClass(
  162. \Magento\Tax\Api\Data\TaxClassSearchResultsInterface::class,
  163. [],
  164. '',
  165. false,
  166. true,
  167. true,
  168. ['getItems']
  169. );
  170. $taxClassMock = $this->getMockForAbstractClass(
  171. \Magento\Tax\Api\Data\TaxClassInterface::class,
  172. ['getClassId', 'getClassName'],
  173. '',
  174. false,
  175. true,
  176. true
  177. );
  178. $this->filterBuilderMock->expects($this->once())
  179. ->method('setField')
  180. ->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)
  181. ->willReturnSelf();
  182. $this->filterBuilderMock->expects($this->once())
  183. ->method('setValue')
  184. ->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)
  185. ->willReturnSelf();
  186. $this->filterBuilderMock->expects($this->once())
  187. ->method('create')
  188. ->willReturn($filterMock);
  189. $this->searchCriteriaBuilderMock->expects($this->once())
  190. ->method('addFilters')
  191. ->with([$filterMock])
  192. ->willReturnSelf();
  193. $this->searchCriteriaBuilderMock->expects($this->once())
  194. ->method('create')
  195. ->willReturn($searchCriteriaMock);
  196. $this->taxClassRepositoryMock->expects($this->once())
  197. ->method('getList')
  198. ->with($searchCriteriaMock)
  199. ->willReturn($searchResultsMock);
  200. $taxClassMock->expects($this->once())
  201. ->method('getClassId')
  202. ->willReturn($value['value']);
  203. $taxClassMock->expects($this->once())
  204. ->method('getClassName')
  205. ->willReturn($value['label']);
  206. $items = [$taxClassMock];
  207. $searchResultsMock->expects($this->once())
  208. ->method('getItems')
  209. ->willReturn($items);
  210. $result=($this->customer->getAllOptions());
  211. $expected=$value;
  212. $this->assertEquals([$expected], $result);
  213. }
  214. /**
  215. * Data provider for testGetAllOptionsNameIntegrity
  216. *
  217. * @return array
  218. */
  219. public function dataProviderGetAllOptionsNameIntegrity()
  220. {
  221. return [
  222. [
  223. 'value' => ['value' => 1, 'label' => 'unescaped name'],
  224. ],
  225. [
  226. 'value' => ['value' => 2, 'label' => 'tax < 50%'],
  227. ],
  228. [
  229. 'value' => ['value' => 3, 'label' => 'rule for 1 & 2'],
  230. ],
  231. [
  232. 'value' => ['value' => 4, 'label' => 'html <tag>'],
  233. ],
  234. [
  235. 'value' => ['value' => 5, 'label' => 'comment <!-- comment -->'],
  236. ],
  237. [
  238. 'value' => ['value' => 6, 'label' => 'php tag <?= "2"; ?>'],
  239. ],
  240. ];
  241. }
  242. }