Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AbstractSearchResultTest.php
Go to the documentation of this file.
1 <?php
7 
8 use \Magento\Framework\Data\AbstractSearchResult;
9 
13 class AbstractSearchResultTest extends \PHPUnit\Framework\TestCase
14 {
18  protected $searchResult;
19 
23  protected $query;
24 
28  protected $entityFactory;
29 
33  protected $criteria;
34 
38  protected $eventManagerMock;
39 
44 
45  protected function setUp()
46  {
47  $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);
48 
49  $this->criteria = $this->getMockForAbstractClass(\Magento\Framework\Api\CriteriaInterface::class);
50  $this->query = $this->getMockForAbstractClass(\Magento\Framework\DB\QueryInterface::class);
51  $this->query->expects($this->any())
52  ->method('getCriteria')
53  ->willReturn($this->criteria);
54  $this->entityFactory = $this->getMockForAbstractClass(
55  \Magento\Framework\Data\Collection\EntityFactoryInterface::class
56  );
57  $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class)
58  ->disableOriginalConstructor()
59  ->getMock();
60  $this->searchResultIteratorMock = $this->getMockBuilder(
61  \Magento\Framework\Data\SearchResultIteratorFactory::class
62  )->disableOriginalConstructor()->getMock();
63  $this->searchResult = $objectManager->getObject(
64  \Magento\Framework\Data\Test\Unit\Stub\SearchResult::class,
65  [
66  'query' => $this->query,
67  'entityFactory' => $this->entityFactory,
68  'eventManager' => $this->eventManagerMock,
69  'resultIteratorFactory' => $this->searchResultIteratorMock
70  ]
71  );
72  }
73 
74  public function testGetItems()
75  {
76  $itemData = ['id' => 1];
77 
78  $testItem = new \Magento\Framework\DataObject($itemData);
79 
80  $this->query->expects($this->once())
81  ->method('fetchAll')
82  ->willReturn([$itemData]);
83  $this->entityFactory->expects($this->once())
84  ->method('create')
85  ->with(\Magento\Framework\DataObject::class, ['data' => $itemData])
86  ->willReturn($testItem);
87 
88  $items = $this->searchResult->getItems();
89 
90  $this->assertCount(1, $items);
91  $this->assertEquals($testItem, end($items));
92  }
93 
94  public function testGetTotalCount()
95  {
96  $totalCount = 42;
97 
98  $this->query->expects($this->once())
99  ->method('getSize')
100  ->willReturn($totalCount);
101 
102  $this->assertEquals($totalCount, $this->searchResult->getTotalCount());
103  }
104 
105  public function testGetSearchCriteria()
106  {
107  $this->assertEquals($this->criteria, $this->searchResult->getSearchCriteria());
108  }
109 
110  public function testGetSize()
111  {
112  $size = 42;
113  $this->query->expects($this->once())
114  ->method('getSize')
115  ->willReturn($size);
116  $this->assertEquals($size, $this->searchResult->getSize());
117  }
118 }
$objectManager
Definition: bootstrap.php:17
$items