/** * Run test toOptionIdArray method * * @return void * * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function testToOptionIdArray() { $itemsByPageId = array_fill(0, 4, 123); $data = ['item1' => ['test' => 'test'], 'item2' => ['test' => 'test'], 'item3' => ['test' => 'test'], 'item4' => ['test' => 'test']]; $objectMock = $this->getMock('Magento\\Framework\\Object', ['getData', 'getId', 'setData', 'getTitle', 'getIdentifier'], [], '', false); $criteriaMock = $this->getMockForAbstractClass('Magento\\Framework\\Api\\CriteriaInterface'); $connectionMock = $this->getMockForAbstractClass('Magento\\Framework\\DB\\Adapter\\AdapterInterface'); $resourceMock = $this->getMockForAbstractClass('Magento\\Framework\\Model\\Resource\\Db\\AbstractDb', [], '', false, true, true, ['getTable']); $selectMock = $this->getMock('Magento\\Framework\\DB\\Select', ['from', 'where'], [], '', false); $storeMock = $this->getMock('Magento\\Store\\Model\\Store', ['getCode'], [], '', false); $this->queryMock->expects($this->once())->method('fetchAll')->will($this->returnValue($data)); $this->searchResultProcessorMock->expects($this->once())->method('getColumnValues')->with('page_id')->will($this->returnValue($itemsByPageId)); $this->queryMock->expects($this->any())->method('getIdFieldName')->will($this->returnValue('id_field_name')); $objectMock->expects($this->any())->method('getData')->will($this->returnValueMap([['id_field_name', null, null], ['page_id', null, 123]])); $this->entityFactoryMock->expects($this->any())->method('create')->with('Magento\\Cms\\Model\\Page', ['data' => ['test' => 'test']])->will($this->returnValue($objectMock)); $this->queryMock->expects($this->once())->method('getCriteria')->will($this->returnValue($criteriaMock)); $criteriaMock->expects($this->once())->method('getPart')->with('first_store_flag')->will($this->returnValue(true)); $this->queryMock->expects($this->once())->method('getConnection')->will($this->returnValue($connectionMock)); $this->queryMock->expects($this->once())->method('getResource')->will($this->returnValue($resourceMock)); $connectionMock->expects($this->once())->method('select')->will($this->returnValue($selectMock)); $selectMock->expects($this->once())->method('from')->with(['cps' => 'query_table'])->will($this->returnSelf()); $resourceMock->expects($this->once())->method('getTable')->with('cms_page_store')->will($this->returnValue('query_table')); $selectMock->expects($this->once())->method('where')->with('cps.page_id IN (?)', array_fill(0, 4, 123))->will($this->returnSelf()); $connectionMock->expects($this->once())->method('fetchPairs')->with($selectMock)->will($this->returnValue([123 => 999])); $objectMock->expects($this->any())->method('getId')->will($this->returnValue(123)); $this->storeManagerMock->expects($this->any())->method('getStore')->with(999)->will($this->returnValue($storeMock)); $storeMock->expects($this->any())->method('getCode')->will($this->returnValue('store_code')); $objectMock->expects($this->any())->method('setData'); $objectMock->expects($this->any())->method('getTitle')->will($this->returnValue('item-value')); $objectMock->expects($this->any())->method('getIdentifier')->will($this->returnValue('identifier-value')); $expected = [['value' => 'identifier-value', 'label' => 'item-value'], ['value' => 'identifier-value|123', 'label' => 'item-value'], ['value' => 'identifier-value|123', 'label' => 'item-value'], ['value' => 'identifier-value|123', 'label' => 'item-value']]; $this->assertEquals($expected, $this->collection->toOptionIdArray()); }
/** * @test */ public function testGetList() { $field = 'name'; $value = 'magento'; $condition = 'eq'; $total = 10; $currentPage = 3; $pageSize = 2; $sortField = 'id'; $criteria = $this->getMockBuilder('Magento\\Framework\\Api\\SearchCriteriaInterface')->getMock(); $filterGroup = $this->getMockBuilder('Magento\\Framework\\Api\\Search\\FilterGroup')->getMock(); $filter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $storeFilter = $this->getMockBuilder('Magento\\Framework\\Api\\Filter')->getMock(); $sortOrder = $this->getMockBuilder('Magento\\Framework\\Api\\SortOrder')->getMock(); $criteria->expects($this->once())->method('getFilterGroups')->willReturn([$filterGroup]); $criteria->expects($this->once())->method('getSortOrders')->willReturn([$sortOrder]); $criteria->expects($this->once())->method('getCurrentPage')->willReturn($currentPage); $criteria->expects($this->once())->method('getPageSize')->willReturn($pageSize); $filterGroup->expects($this->once())->method('getFilters')->willReturn([$storeFilter, $filter]); $filter->expects($this->once())->method('getConditionType')->willReturn($condition); $filter->expects($this->any())->method('getField')->willReturn($field); $filter->expects($this->once())->method('getValue')->willReturn($value); $storeFilter->expects($this->any())->method('getField')->willReturn('store_id'); $storeFilter->expects($this->once())->method('getValue')->willReturn(1); $sortOrder->expects($this->once())->method('getField')->willReturn($sortField); $sortOrder->expects($this->once())->method('getDirection')->willReturn(SearchCriteriaInterface::SORT_DESC); /** @var \Magento\Framework\Api\SearchCriteriaInterface $criteria */ $this->collection->addItem($this->page); $this->pageSearchResult->expects($this->once())->method('setSearchCriteria')->with($criteria)->willReturnSelf(); $this->collection->expects($this->once())->method('addFieldToFilter')->with($field, [$condition => $value])->willReturnSelf(); $this->pageSearchResult->expects($this->once())->method('setTotalCount')->with($total)->willReturnSelf(); $this->collection->expects($this->once())->method('getSize')->willReturn($total); $this->collection->expects($this->once())->method('setCurPage')->with($currentPage)->willReturnSelf(); $this->collection->expects($this->once())->method('setPageSize')->with($pageSize)->willReturnSelf(); $this->collection->expects($this->once())->method('addOrder')->with($sortField, 'DESC')->willReturnSelf(); $this->page->expects($this->once())->method('getData')->willReturn(['data']); $this->pageSearchResult->expects($this->once())->method('setItems')->with(['someData'])->willReturnSelf(); $this->dataHelper->expects($this->once())->method('populateWithArray')->with($this->pageData, ['data'], 'Magento\\Cms\\Api\\Data\\PageInterface'); $this->dataObjectProcessor->expects($this->once())->method('buildOutputDataArray')->with($this->pageData, 'Magento\\Cms\\Api\\Data\\PageInterface')->willReturn('someData'); $this->assertEquals($this->pageSearchResult, $this->repository->getList($criteria)); }
/** * Retrieve count of loaded items * * @return int */ public function count() { return $this->collection->count(); }