Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DynamicTest.php
Go to the documentation of this file.
1 <?php
7 
10 
11 class DynamicTest extends \PHPUnit\Framework\TestCase
12 {
16  private $model;
17 
22 
27 
32 
37 
42 
46  protected $bucket;
47 
51  protected $entityStorage;
52 
58  protected function setUp()
59  {
60  $this->requestBuckedInterface = $this->getMockBuilder(\Magento\Framework\Search\Request\BucketInterface::class)
61  ->disableOriginalConstructor()
62  ->getMock();
63 
64  $this->dataProviderContainer = $this
65  ->getMockBuilder(\Magento\Elasticsearch\SearchAdapter\Dynamic\DataProvider::class)
66  ->disableOriginalConstructor()
67  ->getMock();
68 
69  $this->algorithmRepository = $this->getMockBuilder(
70  \Magento\Framework\Search\Dynamic\Algorithm\Repository::class
71  )
72  ->disableOriginalConstructor()
73  ->getMock();
74 
75  $this->entityStorageFactory = $this->getMockBuilder(
76  \Magento\Framework\Search\Dynamic\EntityStorageFactory::class
77  )
78  ->disableOriginalConstructor()
79  ->getMock();
80 
81  $this->algorithmInterface = $this
82  ->getMockBuilder(\Magento\Framework\Search\Dynamic\Algorithm\AlgorithmInterface::class)
83  ->disableOriginalConstructor()
84  ->getMock();
85 
86  $this->bucket = $this->getMockBuilder(\Magento\Framework\Search\Request\Aggregation\DynamicBucket::class)
87  ->setMethods(['getMethod'])
88  ->disableOriginalConstructor()
89  ->getMock();
90 
91  $this->entityStorage = $this->getMockBuilder(\Magento\Framework\Search\Dynamic\EntityStorage::class)
92  ->disableOriginalConstructor()
93  ->getMock();
94 
95  $this->entityStorageFactory->expects($this->any())
96  ->method('create')
97  ->willReturn($this->entityStorage);
98 
99  $objectManagerHelper = new ObjectManagerHelper($this);
100 
101  $this->model = $objectManagerHelper->getObject(
102  \Magento\Elasticsearch\SearchAdapter\Aggregation\Builder\Dynamic::class,
103  [
104  'algorithmRepository' => $this->algorithmRepository,
105  'entityStorageFactory' => $this->entityStorageFactory,
106  ]
107  );
108  }
109 
113  public function testBuild()
114  {
115  $dimensions = [
116  'scope' => [
117  'name' => 'scope',
118  'value' => 1,
119  ],
120  ];
121 
122  $queryResult = [
123  'took' => 1,
124  'timed_out' => false,
125  '_shards' => [],
126  'hits' => [
127  'total' => 1,
128  'max_score' => 1,
129  'hits' => [
130  [
131  '_id' => 1,
132  ]
133  ],
134  ],
135  'aggregations' => [],
136  ];
137 
138  $this->bucket->expects($this->once())
139  ->method('getMethod')
140  ->willReturn('auto');
141 
142  $this->algorithmRepository->expects($this->any())
143  ->method('get')
144  ->with('auto', ['dataProvider' => $this->dataProviderContainer])
145  ->willReturn($this->algorithmInterface);
146 
147  $this->algorithmInterface->expects($this->once())
148  ->method('getItems')
149  ->with(
150  $this->bucket,
151  $dimensions,
152  $this->entityStorage
153  )
154  ->willReturn([
155  0 => [
156  'from' => '',
157  'to' => 22,
158  'count' => 2,
159  ],
160  1 => [
161  'from' => 22,
162  'to' => 24,
163  'count' => 4,
164  ],
165  2 => [
166  'from' => 24,
167  'to' => '',
168  'count' => 6,
169  ],
170  ]);
171 
172  $this->model->build(
173  $this->bucket,
174  $dimensions,
175  $queryResult,
176  $this->dataProviderContainer
177  );
178  }
179 }