|
7 | 7 |
|
8 | 8 | namespace Magento\Search\Test\Unit\Model;
|
9 | 9 |
|
| 10 | +use Magento\Framework\Data\Collection\AbstractDb; |
10 | 11 | use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
|
11 | 12 | use Magento\Search\Model\QueryResult;
|
| 13 | +use Magento\Search\Model\ResourceModel\Query\Collection; |
| 14 | +use Magento\Search\Model\ResourceModel\Query\CollectionFactory as QueryCollectionFactory; |
| 15 | +use Magento\Store\Api\Data\StoreInterface; |
| 16 | +use Magento\Store\Model\StoreManagerInterface; |
12 | 17 | use PHPUnit\Framework\TestCase;
|
13 | 18 |
|
14 | 19 | class QueryResultTest extends TestCase
|
15 | 20 | {
|
| 21 | + /** |
| 22 | + * @var QueryResult |
| 23 | + */ |
| 24 | + private QueryResult $model; |
| 25 | + |
16 | 26 | /**
|
17 | 27 | * @var ObjectManager
|
18 | 28 | */
|
19 |
| - private $objectManager; |
| 29 | + private ObjectManager $objectManager; |
| 30 | + |
| 31 | + /** |
| 32 | + * @var QueryCollectionFactory |
| 33 | + */ |
| 34 | + private QueryCollectionFactory $queryCollectionFactoryMock; |
| 35 | + |
| 36 | + /** |
| 37 | + * @var StoreManagerInterface |
| 38 | + */ |
| 39 | + private StoreManagerInterface $storeManagerMock; |
| 40 | + |
| 41 | + /** |
| 42 | + * @var StoreInterface |
| 43 | + */ |
| 44 | + private StoreInterface $storeInterfaceMock; |
| 45 | + |
| 46 | + /** |
| 47 | + * @var Collection |
| 48 | + */ |
| 49 | + private Collection $queryCollectionMock; |
| 50 | + |
| 51 | + /** |
| 52 | + * @var QueryResult |
| 53 | + */ |
| 54 | + private QueryResult $queryResultMock; |
| 55 | + |
| 56 | + /** |
| 57 | + * @var AbstractDb |
| 58 | + */ |
| 59 | + private AbstractDb $abstractDbMock; |
20 | 60 |
|
21 | 61 | protected function setUp(): void
|
22 | 62 | {
|
23 | 63 | $this->objectManager = new ObjectManager($this);
|
| 64 | + $this->queryCollectionFactoryMock = $this->getMockBuilder(QueryCollectionFactory::class) |
| 65 | + ->disableOriginalConstructor() |
| 66 | + ->onlyMethods(['create']) |
| 67 | + ->getMock(); |
| 68 | + $this->storeManagerMock = $this->getMockBuilder(StoreManagerInterface::class) |
| 69 | + ->disableOriginalConstructor() |
| 70 | + ->getMock(); |
| 71 | + $this->storeInterfaceMock = $this->getMockBuilder(StoreInterface::class) |
| 72 | + ->disableOriginalConstructor() |
| 73 | + ->onlyMethods(['getId']) |
| 74 | + ->getMockForAbstractClass(); |
| 75 | + $this->queryCollectionMock = $this->getMockBuilder(Collection::class) |
| 76 | + ->disableOriginalConstructor() |
| 77 | + ->getMock(); |
| 78 | + $this->queryResultMock = $this->getMockBuilder(QueryResult::class) |
| 79 | + ->disableOriginalConstructor() |
| 80 | + ->getMock(); |
| 81 | + $this->abstractDbMock = $this->getMockBuilder(AbstractDb::class) |
| 82 | + ->disableOriginalConstructor() |
| 83 | + ->onlyMethods(['getData']) |
| 84 | + ->getMockForAbstractClass(); |
| 85 | + $this->model = new QueryResult( |
| 86 | + 'mint', |
| 87 | + 'resultsCount', |
| 88 | + $this->queryCollectionFactoryMock, |
| 89 | + $this->storeManagerMock |
| 90 | + ); |
24 | 91 | }
|
25 | 92 |
|
26 | 93 | /**
|
27 | 94 | * @dataProvider getPropertiesDataProvider
|
28 | 95 | */
|
29 |
| - public function testGetProperties($queryText, $resultsCount) |
| 96 | + public function testGetProperties($queryText) |
30 | 97 | {
|
31 | 98 | /** @var QueryResult $queryResult */
|
32 | 99 | $queryResult = $this->objectManager->getObject(
|
33 | 100 | QueryResult::class,
|
34 | 101 | [
|
35 |
| - 'queryText' => $queryText, |
36 |
| - 'resultsCount' => $resultsCount, |
| 102 | + 'queryText' => $queryText |
37 | 103 | ]
|
38 | 104 | );
|
39 | 105 | $this->assertEquals($queryText, $queryResult->getQueryText());
|
40 |
| - $this->assertEquals($resultsCount, $queryResult->getResultsCount()); |
41 | 106 | }
|
42 | 107 |
|
43 | 108 | /**
|
44 | 109 | * Data provider for testGetProperties
|
45 | 110 | * @return array
|
46 | 111 | */
|
47 |
| - public function getPropertiesDataProvider() |
| 112 | + public function getPropertiesDataProvider(): array |
48 | 113 | {
|
49 | 114 | return [
|
50 | 115 | [
|
51 |
| - 'queryText' => 'Some kind of query text', |
52 |
| - 'resultsCount' => 0, |
| 116 | + 'queryText' => 'Some kind of query text' |
53 | 117 | ],
|
54 | 118 | [
|
55 |
| - 'queryText' => 'Another query', |
56 |
| - 'resultsCount' => 322312312, |
| 119 | + 'queryText' => 'Another query' |
57 | 120 | ],
|
58 | 121 | [
|
59 |
| - 'queryText' => 'It\' a query too', |
60 |
| - 'resultsCount' => -100, |
| 122 | + 'queryText' => 'It\' a query too' |
61 | 123 | ],
|
62 | 124 | [
|
63 |
| - 'queryText' => '', |
64 |
| - 'resultsCount' => null, |
| 125 | + 'queryText' => '' |
65 | 126 | ],
|
66 | 127 | [
|
67 |
| - 'queryText' => 42, |
68 |
| - 'resultsCount' => false, |
| 128 | + 'queryText' => 42 |
69 | 129 | ],
|
70 | 130 | ];
|
71 | 131 | }
|
| 132 | + |
| 133 | + /** |
| 134 | + * @return void |
| 135 | + * @throws \Magento\Framework\Exception\NoSuchEntityException |
| 136 | + */ |
| 137 | + public function testAfterGetResultsCount() |
| 138 | + { |
| 139 | + $expectedArr = ['num_results' => 5]; |
| 140 | + $this->queryCollectionFactoryMock->expects($this->once()) |
| 141 | + ->method('create') |
| 142 | + ->willReturn($this->queryCollectionMock); |
| 143 | + $this->storeManagerMock->expects($this->once()) |
| 144 | + ->method('getStore') |
| 145 | + ->willReturn($this->storeInterfaceMock); |
| 146 | + $this->storeInterfaceMock->expects($this->once()) |
| 147 | + ->method('getId') |
| 148 | + ->willReturn(1); |
| 149 | + $this->queryCollectionMock->expects($this->once()) |
| 150 | + ->method('setStoreId') |
| 151 | + ->willReturnSelf(); |
| 152 | + $this->queryCollectionMock->expects($this->once()) |
| 153 | + ->method('setQueryFilter') |
| 154 | + ->willReturn($this->queryCollectionMock); |
| 155 | + $this->abstractDbMock->expects($this->once()) |
| 156 | + ->method('getData') |
| 157 | + ->willReturn(['num_results' => 5]); |
| 158 | + $this->queryCollectionMock->expects($this->once()) |
| 159 | + ->method('getIterator') |
| 160 | + ->willReturn(new \ArrayIterator([$this->abstractDbMock])); |
| 161 | + $this->assertEquals($expectedArr, $this->model->getResultsCount()); |
| 162 | + } |
72 | 163 | }
|
0 commit comments