/** * Initiate tax classes. * * @return $this */ protected function initTaxClasses() { if (empty($this->taxClasses)) { $collection = $this->collectionFactory->create(); $collection->addFieldToFilter('class_type', ClassModel::TAX_CLASS_TYPE_PRODUCT); /* @var $collection \Magento\Tax\Model\Resource\TaxClass\Collection */ foreach ($collection as $taxClass) { $this->taxClasses[$taxClass->getClassName()] = $taxClass->getId(); } } return $this; }
/** * @param string $expected * @param string[] $taxClassKeyMockValeMap * @param bool $found * @dataProvider getTaxClassIdDataProvider */ public function testGetTaxClassId($expected, $taxClassKeyMockValeMap, $found = false) { /** @var \Magento\Tax\Service\V1\Data\TaxClassKey|\PHPUnit_Framework_MockObject_MockObject $taxClassKeyMock */ $taxClassKeyMock = $this->getMockBuilder('Magento\\Tax\\Service\\V1\\Data\\TaxClassKey')->disableOriginalConstructor()->getMock(); $this->mockReturnValue($taxClassKeyMock, $taxClassKeyMockValeMap); if ($taxClassKeyMockValeMap['getType'] == TaxClassKey::TYPE_NAME) { $this->searchCriteriaBuilderMock->expects($this->exactly(2))->method('addFilter')->will($this->returnValue($this->searchCriteriaBuilderMock)); /** @var \Magento\Framework\Service\V1\Data\SearchCriteria $searchCriteria */ $searchCriteria = $this->createSearchCriteria(); $this->searchCriteriaBuilderMock->expects($this->once())->method('create')->will($this->returnValue($searchCriteria)); $this->searchResultBuilder->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); /** @var \PHPUnit_Framework_MockObject_MockObject $taxClassModelMock */ $taxClassModelMock = $this->getMockBuilder('Magento\\Tax\\Model\\ClassModel')->disableOriginalConstructor()->getMock(); $collectionMock = $this->mockTaxClassCollection($taxClassModelMock); $this->taxClassCollectionFactory->expects($this->once())->method('create')->will($this->returnValue($collectionMock)); /** @var \PHPUnit_Framework_MockObject_MockObject $taxMock */ $taxClassMock = $this->getMockBuilder('Magento\\Tax\\Service\\V1\\Data\\TaxClass')->disableOriginalConstructor()->getMock(); $this->converterMock->expects($this->once())->method('createTaxClassData')->with($taxClassModelMock)->will($this->returnValue($taxClassMock)); $this->searchResultBuilder->expects($this->once())->method('setItems')->will($this->returnValue([$taxClassMock])); $searchResultsMock = $this->getMockBuilder('Magento\\Tax\\Service\\V1\\Data\\TaxClassSearchResults')->disableOriginalConstructor()->getMock(); $searchResultsMock->expects($this->once())->method('getItems')->will($this->returnValue($found ? [$taxClassMock] : [])); $taxClassMock->expects($this->any())->method('getClassId')->will($this->returnValue(self::TAX_CLASS_ID)); $this->searchResultBuilder->expects($this->once())->method('create')->will($this->returnValue($searchResultsMock)); } $this->assertEquals($expected, $this->taxClassService->getTaxClassId($taxClassKeyMock)); }
/** * Retrieve tax classes which match a specific criteria. * * @param \Magento\Framework\Service\V1\Data\SearchCriteria $searchCriteria * @return \Magento\Tax\Service\V1\Data\TaxClassSearchResults containing Data\TaxClass * @throws \Magento\Framework\Exception\InputException */ public function searchTaxClass(\Magento\Framework\Service\V1\Data\SearchCriteria $searchCriteria) { $this->searchResultsBuilder->setSearchCriteria($searchCriteria); /** @var TaxClassCollection $collection */ $collection = $this->taxClassCollectionFactory->create(); /** TODO: This method duplicates functionality of search methods in other services and should be refactored. */ foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $this->searchResultsBuilder->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $field => $direction) { $collection->addOrder($field, $direction == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $taxClasses = []; /** @var \Magento\Tax\Model\ClassModel $taxClassModel */ foreach ($collection->getItems() as $taxClassModel) { $taxClasses[] = $this->converter->createTaxClassData($taxClassModel); } $this->searchResultsBuilder->setItems($taxClasses); return $this->searchResultsBuilder->create(); }
/** * {@inheritdoc} */ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria) { $searchResults = $this->searchResultsFactory->create(); $searchResults->setSearchCriteria($searchCriteria); /** @var TaxClassCollection $collection */ $collection = $this->taxClassCollectionFactory->create(); foreach ($searchCriteria->getFilterGroups() as $group) { $this->addFilterGroupToCollection($group, $collection); } $searchResults->setTotalCount($collection->getSize()); $sortOrders = $searchCriteria->getSortOrders(); /** @var SortOrder $sortOrder */ if ($sortOrders) { foreach ($searchCriteria->getSortOrders() as $sortOrder) { $collection->addOrder($sortOrder->getField(), $sortOrder->getDirection() == SearchCriteria::SORT_ASC ? 'ASC' : 'DESC'); } } $collection->setCurPage($searchCriteria->getCurrentPage()); $collection->setPageSize($searchCriteria->getPageSize()); $searchResults->setItems($collection->getItems()); return $searchResults; }
public function testSearch() { $collectionSize = 3; $currentPage = 1; $pageSize = 10; $searchCriteria = $this->createSearchCriteria(); $this->searchResultBuilder->expects($this->once())->method('setSearchCriteria')->with($searchCriteria); /** @var \PHPUnit_Framework_MockObject_MockObject $collectionMock */ $collectionMock = $this->getMockBuilder('Magento\\Tax\\Model\\Resource\\TaxClass\\Collection')->disableOriginalConstructor()->setMethods(['addFieldToFilter', 'getSize', 'setCurPage', 'setPageSize', 'getItems', 'addOrder'])->getMock(); $this->taxClassCollectionFactory->expects($this->once())->method('create')->will($this->returnValue($collectionMock)); $collectionMock->expects($this->exactly(2))->method('addFieldToFilter'); $collectionMock->expects($this->any())->method('getSize')->will($this->returnValue($collectionSize)); $collectionMock->expects($this->once())->method('setCurPage')->with($currentPage); $collectionMock->expects($this->once())->method('setPageSize')->with($pageSize); $collectionMock->expects($this->once())->method('addOrder')->with('class_name', 'ASC'); /** @var \PHPUnit_Framework_MockObject_MockObject $taxClassModelMock */ $taxClassModelMock = $this->getMockBuilder('Magento\\Tax\\Model\\ClassModel')->disableOriginalConstructor()->getMock(); $collectionMock->expects($this->once())->method('getItems')->will($this->returnValue([$taxClassModelMock])); /** @var \PHPUnit_Framework_MockObject_MockObject $taxMock */ $taxClassMock = $this->getMockBuilder('Magento\\Tax\\Service\\V1\\Data\\TaxClass')->disableOriginalConstructor()->getMock(); $this->converterMock->expects($this->once())->method('createTaxClassData')->with($taxClassModelMock)->will($this->returnValue($taxClassMock)); $this->searchResultBuilder->expects($this->once())->method('setItems')->will($this->returnValue([$taxClassMock])); $this->taxClassService->searchTaxClass($searchCriteria); }
/** * Gets the tax rates by type * * @param \Magento\Framework\Object $request * @param string|array $fieldName * @param string|array $type * @return array */ protected function _getRates($request, $fieldName, $type) { $result = array(); /** @var $classes \Magento\Tax\Model\Resource\TaxClass\Collection */ $classes = $this->_classesFactory->create(); $classes->addFieldToFilter('class_type', $type)->load(); foreach ($classes as $class) { $request->setData($fieldName, $class->getId()); $result[$class->getId()] = $this->getRate($request); } return $result; }