123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264 |
- <?php
- /**
- * Copyright © Magento, Inc. All rights reserved.
- * See COPYING.txt for license details.
- */
- namespace Magento\Tax\Test\Unit\Model\TaxClass\Source;
- use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
- class CustomerTest extends \PHPUnit\Framework\TestCase
- {
- /**
- * @var \Magento\Tax\Api\TaxClassRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $taxClassRepositoryMock;
- /**
- * @var \Magento\Framework\Api\SearchCriteriaBuilder|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $searchCriteriaBuilderMock;
- /**
- * @var \Magento\Framework\Api\FilterBuilder|\PHPUnit_Framework_MockObject_MockObject
- */
- protected $filterBuilderMock;
- /**
- * @var \Magento\Tax\Model\TaxClass\Source\Customer
- */
- protected $customer;
- /**
- * @var ObjectManager
- */
- protected $objectManager;
- /**
- * Set up
- *
- * @return void
- */
- protected function setUp()
- {
- $this->objectManager = new ObjectManager($this);
- $this->taxClassRepositoryMock = $this->getMockForAbstractClass(
- \Magento\Tax\Api\TaxClassRepositoryInterface::class,
- ['getList'],
- '',
- false,
- true,
- true,
- []
- );
- $this->searchCriteriaBuilderMock = $this->createPartialMock(
- \Magento\Framework\Api\SearchCriteriaBuilder::class,
- ['addFilters', 'create']
- );
- $this->filterBuilderMock = $this->createPartialMock(
- \Magento\Framework\Api\FilterBuilder::class,
- ['setField', 'setValue', 'create']
- );
- $this->customer = $this->objectManager->getObject(
- \Magento\Tax\Model\TaxClass\Source\Customer::class,
- [
- 'taxClassRepository' => $this->taxClassRepositoryMock,
- 'searchCriteriaBuilder' => $this->searchCriteriaBuilderMock,
- 'filterBuilder' => $this->filterBuilderMock
- ]
- );
- }
- /**
- * Run test getAllOptions method
- *
- * @param bool $isEmpty
- * @param array $expected
- * @dataProvider dataProviderGetAllOptions
- */
- public function testGetAllOptions($isEmpty, array $expected)
- {
- $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $searchResultsMock = $this->getMockForAbstractClass(
- \Magento\Tax\Api\Data\TaxClassSearchResultsInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getItems']
- );
- $taxClassMock = $this->getMockForAbstractClass(
- \Magento\Tax\Api\Data\TaxClassInterface::class,
- ['getClassId', 'getClassName'],
- '',
- false,
- true,
- true
- );
- $this->filterBuilderMock->expects($this->once())
- ->method('setField')
- ->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('setValue')
- ->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($filterMock);
- $this->searchCriteriaBuilderMock->expects($this->once())
- ->method('addFilters')
- ->with([$filterMock])
- ->willReturnSelf();
- $this->searchCriteriaBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($searchCriteriaMock);
- $this->taxClassRepositoryMock->expects($this->once())
- ->method('getList')
- ->with($searchCriteriaMock)
- ->willReturn($searchResultsMock);
- if (!$isEmpty) {
- $taxClassMock->expects($this->once())
- ->method('getClassId')
- ->willReturn(10);
- $taxClassMock->expects($this->once())
- ->method('getClassName')
- ->willReturn('class-name');
- $items = [$taxClassMock];
- $searchResultsMock->expects($this->once())
- ->method('getItems')
- ->willReturn($items);
- // checking of a lack of re-initialization
- for ($i = 10; --$i;) {
- $result = $this->customer->getAllOptions();
- $this->assertEquals($expected, $result);
- }
- } else {
- $items = [];
- $searchResultsMock->expects($this->once())
- ->method('getItems')
- ->willReturn($items);
- // checking exception
- $this->assertEmpty($this->customer->getAllOptions());
- }
- }
- /**
- * Data provider for testGetAllOptions
- *
- * @return array
- */
- public function dataProviderGetAllOptions()
- {
- return [
- ['isEmpty' => false, 'expected' => [['value' => 10, 'label' => 'class-name']]],
- ['isEmpty' => true, 'expected' => []]
- ];
- }
- /**
- * Run test getAllOptions method for names integrity
- *
- * @param array $value
- * @dataProvider dataProviderGetAllOptionsNameIntegrity
- */
- public function testGetAllOptionsNameIntegrity(array $value)
- {
- $filterMock = $this->createMock(\Magento\Framework\Api\Filter::class);
- $searchCriteriaMock = $this->createMock(\Magento\Framework\Api\SearchCriteria::class);
- $searchResultsMock = $this->getMockForAbstractClass(
- \Magento\Tax\Api\Data\TaxClassSearchResultsInterface::class,
- [],
- '',
- false,
- true,
- true,
- ['getItems']
- );
- $taxClassMock = $this->getMockForAbstractClass(
- \Magento\Tax\Api\Data\TaxClassInterface::class,
- ['getClassId', 'getClassName'],
- '',
- false,
- true,
- true
- );
- $this->filterBuilderMock->expects($this->once())
- ->method('setField')
- ->with(\Magento\Tax\Model\ClassModel::KEY_TYPE)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('setValue')
- ->with(\Magento\Tax\Api\TaxClassManagementInterface::TYPE_CUSTOMER)
- ->willReturnSelf();
- $this->filterBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($filterMock);
- $this->searchCriteriaBuilderMock->expects($this->once())
- ->method('addFilters')
- ->with([$filterMock])
- ->willReturnSelf();
- $this->searchCriteriaBuilderMock->expects($this->once())
- ->method('create')
- ->willReturn($searchCriteriaMock);
- $this->taxClassRepositoryMock->expects($this->once())
- ->method('getList')
- ->with($searchCriteriaMock)
- ->willReturn($searchResultsMock);
- $taxClassMock->expects($this->once())
- ->method('getClassId')
- ->willReturn($value['value']);
- $taxClassMock->expects($this->once())
- ->method('getClassName')
- ->willReturn($value['label']);
- $items = [$taxClassMock];
- $searchResultsMock->expects($this->once())
- ->method('getItems')
- ->willReturn($items);
- $result=($this->customer->getAllOptions());
- $expected=$value;
- $this->assertEquals([$expected], $result);
- }
- /**
- * Data provider for testGetAllOptionsNameIntegrity
- *
- * @return array
- */
- public function dataProviderGetAllOptionsNameIntegrity()
- {
- return [
- [
- 'value' => ['value' => 1, 'label' => 'unescaped name'],
- ],
- [
- 'value' => ['value' => 2, 'label' => 'tax < 50%'],
- ],
- [
- 'value' => ['value' => 3, 'label' => 'rule for 1 & 2'],
- ],
- [
- 'value' => ['value' => 4, 'label' => 'html <tag>'],
- ],
- [
- 'value' => ['value' => 5, 'label' => 'comment <!-- comment -->'],
- ],
- [
- 'value' => ['value' => 6, 'label' => 'php tag <?= "2"; ?>'],
- ],
- ];
- }
- }
|