Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
AlgorithmBaseTest.php
Go to the documentation of this file.
1 <?php
7 
9 
17 class AlgorithmBaseTest extends \PHPUnit\Framework\TestCase
18 {
24  protected $_layer;
25 
31  protected $_filter;
32 
36  protected $priceResource;
37 
45  public function testPricesSegmentation($categoryId, array $entityIds, array $intervalItems)
46  {
48  $layer = $objectManager->create(\Magento\Catalog\Model\Layer\Category::class);
50  $termBucket = $objectManager->create(
51  \Magento\Framework\Search\Request\Aggregation\TermBucket::class,
52  ['name' => 'name', 'field' => 'price', 'metrics' => []]
53  );
54 
55  $dimensions = [
56  'scope' => $objectManager->create(
57  \Magento\Framework\Search\Request\Dimension::class,
58  ['name' => 'someName', 'value' => 'default']
59  ),
60  ];
61 
63  $entityMetadata = $objectManager->create(\Magento\Framework\Search\EntityMetadata::class, ['entityId' => 'id']);
64  $idKey = $entityMetadata->getEntityId();
65 
67  $documentFactory = $objectManager->create(
68  \Magento\Framework\Search\Adapter\Mysql\DocumentFactory::class,
69  ['entityMetadata' => $entityMetadata]
70  );
71 
73  $documents = [];
74  foreach ($entityIds as $entityId) {
75  $rawDocument = [
76  $idKey => $entityId,
77  'score' => 1,
78  ];
79  $documents[] = $documentFactory->create($rawDocument);
80  }
81 
83  $temporaryStorage = $objectManager->create(\Magento\Framework\Search\Adapter\Mysql\TemporaryStorage::class);
84  $table = $temporaryStorage->storeDocuments($documents);
85 
87  $dataProvider = $objectManager->create(
88  \Magento\CatalogSearch\Model\Adapter\Mysql\Aggregation\DataProvider::class
89  );
90  $select = $dataProvider->getDataSet($termBucket, $dimensions, $table);
91 
93  $intervalFactory = $objectManager->create(
94  \Magento\Framework\Search\Adapter\Mysql\Aggregation\IntervalFactory::class
95  );
96  $interval = $intervalFactory->create(['select' => $select]);
97 
99  $model = $objectManager->create(\Magento\Framework\Search\Dynamic\Algorithm::class);
100 
101  $layer->setCurrentCategory($categoryId);
102  $collection = $layer->getProductCollection();
103 
104  $memoryUsedBefore = memory_get_usage();
105  $model->setStatistics(
106  $collection->getMinPrice(),
107  $collection->getMaxPrice(),
108  $collection->getPriceStandardDeviation(),
109  $collection->getPricesCount()
110  );
111 
112  $items = $model->calculateSeparators($interval);
113  $this->assertEquals(array_keys($intervalItems), array_keys($items));
114 
115  for ($i = 0; $i < count($intervalItems); ++$i) {
116  $this->assertInternalType('array', $items[$i]);
117  $this->assertEquals($intervalItems[$i]['from'], $items[$i]['from']);
118  $this->assertEquals($intervalItems[$i]['to'], $items[$i]['to']);
119  $this->assertEquals($intervalItems[$i]['count'], $items[$i]['count']);
120  }
121 
122  // Algorithm should use less than 10M
123  $this->assertLessThan(10 * 1024 * 1024, memory_get_usage() - $memoryUsedBefore);
124  }
125 
130  {
131  $testCases = include __DIR__ . '/_files/_algorithm_base_data.php';
132  $result = [];
133  foreach ($testCases as $index => $testCase) {
134  $result[] = [
135  $index + 4, //category id
136  $testCase[1],
137  $testCase[2],
138  ];
139  }
140 
141  return $result;
142  }
143 }
$objectManager
Definition: bootstrap.php:17
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$table
Definition: trigger.php:14
$i
Definition: gallery.phtml:31
$index
Definition: list.phtml:44
$items