Example #1
0
 /**
  * @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();
 }
 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);
 }