1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Magento\Extractors\Admin\Xtree; |
4
|
|
|
|
5
|
|
|
use Facebook\WebDriver\Exception\ElementNotVisibleException; |
6
|
|
|
use Facebook\WebDriver\WebDriverElement; |
7
|
|
|
use Magium\AbstractTestCase; |
8
|
|
|
use Magium\Extractors\AbstractExtractor; |
9
|
|
|
use Magium\InvalidInstructionException; |
10
|
|
|
use Magium\Magento\Themes\Admin\ThemeConfiguration; |
11
|
|
|
use Magium\WebDriver\ExpectedCondition; |
12
|
|
|
use Magium\WebDriver\WebDriver; |
13
|
|
|
|
14
|
|
|
class CategoryTreeNode extends AbstractExtractor |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
const EXTRACTOR = 'Admin\Xtree\CategoryTreeNode'; |
18
|
|
|
|
19
|
|
|
protected $node; |
20
|
|
|
protected $nodePath; |
21
|
|
|
protected $rootCategory; |
22
|
|
|
|
23
|
|
|
public function __construct(WebDriver $webDriver, AbstractTestCase $testCase, ThemeConfiguration $theme) |
24
|
|
|
{ |
25
|
|
|
parent::__construct($webDriver, $testCase, $theme); |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @param $nodePath |
30
|
|
|
* @return WebDriverElement |
31
|
|
|
*/ |
32
|
|
|
|
33
|
|
|
public function getNode($nodePath) |
34
|
|
|
{ |
35
|
|
|
$this->nodePath = $nodePath; |
36
|
|
|
$this->extract(); |
37
|
|
|
return $this->node; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
public function setRootCategory($rootCategory) |
41
|
|
|
{ |
42
|
|
|
$this->rootCategory = $rootCategory; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function extract() |
46
|
|
|
{ |
47
|
|
|
if (!$this->nodePath) { |
48
|
|
|
throw new InvalidInstructionException('Node path was not specified'); |
49
|
|
|
} |
50
|
|
|
$parts = explode('/', $this->nodePath); |
51
|
|
|
|
52
|
|
|
$xpath = $this->theme->getXTreeRootXpath(); |
|
|
|
|
53
|
|
|
if ($this->rootCategory) { |
54
|
|
|
$xpath = $this->theme->getXTreeNamedRootXpath($this->rootCategory); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
$xpath .= $this->theme->getXTreeChildNodePrefixXpath(); |
|
|
|
|
57
|
|
|
|
58
|
|
|
$previousExpandXpath = ''; |
59
|
|
|
$element = null; |
60
|
|
|
foreach ($parts as $part) { |
61
|
|
|
$xpath .= '/' . $this->theme->getXTreeChildXpath($part); |
|
|
|
|
62
|
|
View Code Duplication |
if(!$this->webDriver->elementExists($xpath, WebDriver::BY_XPATH)) { |
|
|
|
|
63
|
|
|
if ($previousExpandXpath) { |
64
|
|
|
$this->testCase->assertElementExists($previousExpandXpath, WebDriver::BY_XPATH); |
65
|
|
|
$this->webDriver->byXpath($previousExpandXpath)->click(); |
66
|
|
|
$this->webDriver->wait()->until(ExpectedCondition::elementExists($xpath, WebDriver::BY_XPATH)); |
|
|
|
|
67
|
|
|
$this->testCase->sleep('1s'); // Give the menu time to render |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
$this->testCase->assertElementExists($xpath, WebDriver::BY_XPATH); |
71
|
|
|
$element = $this->webDriver->byXpath($xpath); |
72
|
|
|
if (!$element->isDisplayed()) { |
73
|
|
|
throw new ElementNotVisibleException('Node element is not visible and cannot be made visible: ' . $part); |
74
|
|
|
} |
75
|
|
|
$previousExpandXpath = $xpath . $this->theme->getXTreeChildNodeExpandPrefixXpath(); |
|
|
|
|
76
|
|
|
$xpath .= $this->theme->getXTreeChildNodePrefixXpath(); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
$this->node = $element; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
} |
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: