1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Magento\Extractors\Catalog\LayeredNavigation; |
4
|
|
|
|
5
|
|
|
use Magium\Magento\AbstractMagentoTestCase; |
6
|
|
|
use Magium\Magento\Themes\AbstractThemeConfiguration; |
7
|
|
|
use Magium\WebDriver\WebDriver; |
8
|
|
|
use Magium\Extractors\AbstractExtractor; |
9
|
|
|
use Magium\Magento\Extractors\Catalog\LayeredNavigation\FilterTypes\AbstractFilterType; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* This is not part of the category namespace because it can be reused for search |
13
|
|
|
* |
14
|
|
|
* Class LayeredNavigation |
15
|
|
|
* @package Magium\Magento\Extractors\Catalog |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
class LayeredNavigation extends AbstractExtractor |
19
|
|
|
{ |
20
|
|
|
const EXTRACTOR = 'Catalog\LayeredNavigation\LayeredNavigation'; |
21
|
|
|
|
22
|
|
|
const FILTER_TYPE_DEFAULT = 'DefaultFilter'; |
23
|
|
|
const FILTER_TYPE_PRICE = 'PriceFilter'; |
24
|
|
|
const FILTER_TYPE_SWATCH = 'SwatchFilter'; |
25
|
|
|
|
26
|
|
|
protected $filterTypes = []; |
27
|
|
|
|
28
|
|
|
protected $baseNamespace = 'Magium\Magento\Extractors\Catalog\LayeredNavigation\FilterTypes'; |
29
|
|
|
|
30
|
|
|
protected $filterNames = []; |
31
|
|
|
protected $filterValues = []; |
32
|
|
|
|
33
|
|
|
|
34
|
|
|
public function __construct( |
35
|
|
|
WebDriver $webDriver, |
36
|
|
|
AbstractMagentoTestCase $testCase, |
37
|
|
|
AbstractThemeConfiguration $theme |
38
|
|
|
) { |
39
|
|
|
parent::__construct($webDriver, $testCase, $theme); |
40
|
|
|
$this->addFilterType(self::FILTER_TYPE_DEFAULT); |
41
|
|
|
$this->addFilterType(self::FILTER_TYPE_PRICE); |
42
|
|
|
$this->addFilterType(self::FILTER_TYPE_SWATCH); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* The purpose of this method is to allow shorthand calls to FilterTypes/* classes. |
48
|
|
|
* |
49
|
|
|
* @param $type |
50
|
|
|
* @return string |
51
|
|
|
* @throws InvalidFilterException |
52
|
|
|
*/ |
53
|
|
|
|
54
|
|
|
protected function filterTypeName($type) |
55
|
|
|
{ |
56
|
|
|
$returnType = $type; |
57
|
|
|
if (strpos($type, '\\') === false) { |
58
|
|
|
$returnType = $this->baseNamespace . '\\' . $type; |
59
|
|
|
if (!class_exists($returnType)) { |
60
|
|
|
$returnType = $type; |
61
|
|
|
if (!class_exists($returnType)) { |
62
|
|
|
throw new InvalidFilterException('Filter type must exist: ' . $returnType); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
$this->validateFilterType($returnType); |
67
|
|
|
return $returnType; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
protected function validateFilterType($returnType) |
71
|
|
|
{ |
72
|
|
|
if ($returnType == $this->baseNamespace . '\AbstractFilterType') { |
73
|
|
|
return true; |
74
|
|
|
} |
75
|
|
|
$reflection = new \ReflectionClass($returnType); |
76
|
|
|
if (!$reflection->isSubclassOf($this->baseNamespace . '\AbstractFilterType')) { |
77
|
|
|
throw new InvalidFilterException('Filter type must extend AbstractFilterType: ' . $returnType); |
78
|
|
|
} |
79
|
|
|
return true; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
|
83
|
|
|
public function addFilterType($type) |
84
|
|
|
{ |
85
|
|
|
$type = $this->filterTypeName($type); |
86
|
|
|
|
87
|
|
|
array_unshift($this->filterTypes, $type); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
|
91
|
|
|
public function removeFilterType($type) |
92
|
|
|
{ |
93
|
|
|
$type = $this->filterTypeName($type); |
94
|
|
|
$key = array_search($type, $this->filterTypes); |
95
|
|
|
if ($key !== false) { |
96
|
|
|
unset($this->filterTypes[$key]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function replaceFilterType($replace, $with) |
101
|
|
|
{ |
102
|
|
|
$replace = $this->filterTypeName($replace); |
103
|
|
|
$with = $this->filterTypeName($with); |
104
|
|
|
$key = array_search($replace, $this->filterTypes); |
105
|
|
|
if ($key !== false) { |
106
|
|
|
$this->filterTypes[$key] = $with; |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* For the purposes of this extractor filters refer both to filters and facets |
112
|
|
|
* |
113
|
|
|
* @return array |
114
|
|
|
*/ |
115
|
|
|
|
116
|
|
|
public function getFilterNames() |
117
|
|
|
{ |
118
|
|
|
return $this->filterNames; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param $filter |
123
|
|
|
* @return AbstractFilterType |
124
|
|
|
* @throws InvalidFilterException |
125
|
|
|
*/ |
126
|
|
|
|
127
|
|
|
public function getFilter($filter) |
128
|
|
|
{ |
129
|
|
|
$filterKey = strtolower($filter); |
130
|
|
|
if (isset($this->filterValues[$filterKey])) { |
131
|
|
|
return $this->filterValues[$filterKey]; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
throw new MissingFilterException('Could not find the filter: ' . $filter); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
public function extract() |
138
|
|
|
{ |
139
|
|
|
|
140
|
|
|
$filters = $this->webDriver->byXpath($this->theme->getLayeredNavigationBaseXpath()); |
|
|
|
|
141
|
|
|
/* I cannot find any way that normalizes filter types without requiring XPath 2 or other shenanigans. For that |
142
|
|
|
* reason I am pulling just the HTML and doing direct XPath queries on it; so I can normalize the filter types |
143
|
|
|
*/ |
144
|
|
|
$html = $filters->getAttribute('innerHTML'); |
145
|
|
|
$doc = new \DOMDocument(); |
146
|
|
|
$doc->loadHTML($html); |
147
|
|
|
|
148
|
|
|
$xPath = new \DOMXPath($doc); |
149
|
|
|
$filters = $xPath->query($this->theme->getLayeredNavigationFilterNameXpath()); |
|
|
|
|
150
|
|
|
/* @var $filters \DOMElement[] */ |
151
|
|
|
foreach ($filters as $filter) { |
152
|
|
|
$this->filterNames[] = $filter = trim($filter->nodeValue); |
153
|
|
|
$filterKey = strtolower($filter); |
154
|
|
|
$this->filterValues[$filterKey] = []; |
155
|
|
|
|
156
|
|
|
foreach ($this->filterTypes as $type) { |
157
|
|
|
if (is_subclass_of($type, $this->filterTypeName('AbstractFilterType'))) { |
|
|
|
|
158
|
|
|
$type = new $type( |
159
|
|
|
$filter, |
160
|
|
|
$doc, |
161
|
|
|
$this->testCase, |
162
|
|
|
$this->theme, |
163
|
|
|
$this->webDriver |
164
|
|
|
); |
165
|
|
|
if ($type->filterApplies()) { |
166
|
|
|
$this->filterValues[$filterKey] = $type; |
167
|
|
|
break; |
168
|
|
|
} |
169
|
|
|
} else { |
170
|
|
|
throw new InvalidFilterException('Filter type does not extend AbstractFilterType: ' . $type); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
|
176
|
|
|
} |
177
|
|
|
} |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: