|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Magento\Extractors\Catalog\Product; |
|
4
|
|
|
|
|
5
|
|
|
use Facebook\WebDriver\WebDriverBy; |
|
6
|
|
|
use Magium\AbstractTestCase; |
|
7
|
|
|
use Magium\Extractors\AbstractExtractor; |
|
8
|
|
|
use Magium\Magento\Themes\AbstractThemeConfiguration; |
|
9
|
|
|
use Magium\Themes\ThemeConfigurationInterface; |
|
10
|
|
|
use Magium\WebDriver\WebDriver; |
|
11
|
|
|
use Magium\WebDriver\WebDriverElementProxy; |
|
12
|
|
|
|
|
13
|
|
|
class ConfigurableProductOptions extends AbstractExtractor |
|
14
|
|
|
{ |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var AbstractThemeConfiguration |
|
18
|
|
|
*/ |
|
19
|
|
|
|
|
20
|
|
|
protected $theme; |
|
21
|
|
|
|
|
22
|
|
|
const EXTRACTOR = 'Catalog\Product\ConfigurableProductOptions'; |
|
23
|
|
|
|
|
24
|
|
|
const EXCEPTION_MISSING_CONFIGURATION = 'Magium\Magento\Extractors\Catalog\Product\MissingConfigurableSwatchConfigurationException'; |
|
25
|
|
|
const EXCEPTION_MISSING_SWATCH_NAME = 'Magium\Magento\Extractors\Catalog\Product\MissingSwatchNameException'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var Option[] |
|
29
|
|
|
*/ |
|
30
|
|
|
|
|
31
|
|
|
protected $items = []; |
|
32
|
|
|
|
|
33
|
|
|
protected $swatchProcessor; |
|
34
|
|
|
protected $standardProcessor; |
|
35
|
|
|
|
|
36
|
|
|
public function __construct( |
|
37
|
|
|
WebDriver $webDriver, |
|
38
|
|
|
AbstractTestCase $testCase, |
|
39
|
|
|
ThemeConfigurationInterface $theme, |
|
40
|
|
|
SwatchProcessor $swatchProcessor, |
|
41
|
|
|
StandardProcessor $standardProcessor |
|
42
|
|
|
) |
|
43
|
|
|
{ |
|
44
|
|
|
parent::__construct($webDriver, $testCase, $theme); |
|
45
|
|
|
$this->swatchProcessor = $swatchProcessor; |
|
46
|
|
|
$this->standardProcessor = $standardProcessor; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function getOptionNames() |
|
51
|
|
|
{ |
|
52
|
|
|
$names = []; |
|
53
|
|
|
foreach ($this->items as $item) { |
|
54
|
|
|
$names[] = $item->getName(); |
|
55
|
|
|
} |
|
56
|
|
|
return $names; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param $name |
|
61
|
|
|
* @return Option|null |
|
62
|
|
|
*/ |
|
63
|
|
|
|
|
64
|
|
|
public function getOption($name) |
|
65
|
|
|
{ |
|
66
|
|
|
$name = strtolower($name); |
|
67
|
|
|
foreach ($this->items as $item) { |
|
68
|
|
|
if (strtolower($item->getName()) == $name) { |
|
69
|
|
|
return $item; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
public function extract() |
|
78
|
|
|
{ |
|
79
|
|
|
$this->items = []; |
|
80
|
|
|
$labelXpath = $this->theme->getConfigurableLabelXpath(); |
|
81
|
|
|
|
|
82
|
|
|
$elements = $this->webDriver->findElements(WebDriverBy::xpath($labelXpath)); |
|
83
|
|
|
foreach ($elements as $count => $element) { |
|
84
|
|
|
// Gotta do this because the text is an unencapsulated DOMText node in Magento 1 (properly done in M2) |
|
85
|
|
|
$doc = new \DOMDocument(); |
|
86
|
|
|
$html = trim($element->getAttribute('innerHTML')); |
|
87
|
|
|
if ($html != htmlspecialchars($html)) { |
|
88
|
|
|
$doc->loadHTML($html); |
|
89
|
|
|
$xpath = new \DOMXPath($doc); |
|
90
|
|
|
$elements = $xpath->query('//text()'); |
|
91
|
|
|
$name = null; |
|
92
|
|
|
foreach ($elements as $e) { |
|
93
|
|
|
/* @var $e \DOMText */ |
|
94
|
|
|
|
|
95
|
|
|
// text nodes that are not encapsulated by a tag have a nodeName of "body" |
|
96
|
|
|
if ($e->parentNode->nodeName == 'body') { |
|
97
|
|
|
$testName = $this->theme->filterConfigurableProductOptionName(trim($e->nodeValue)); |
|
98
|
|
|
if ($testName) { |
|
99
|
|
|
$name = $testName; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
} else { |
|
104
|
|
|
$name = $html; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
if ($name === null) { |
|
108
|
|
|
throw new MissingSwatchNameException('Unable to extract the swatch name from HTML: ' . $element->getAttribute('innerHTML')); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
$isSwatch = $this->swatchProcessor->isConfigurableSwatch($count+1); |
|
112
|
|
|
if ($isSwatch) { |
|
113
|
|
|
$this->items[] = $this->swatchProcessor->process($name, $count+1); |
|
114
|
|
|
} else { |
|
115
|
|
|
// gotta find some way to do this recursively, the options are populated based off of previous option selections |
|
116
|
|
|
$this->items[] = $this->standardProcessor->process($name, $count+1); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
|
|
124
|
|
|
|
|
125
|
|
|
} |