/
BEMPropertyDecorator.php
166 lines (144 loc) · 5.01 KB
/
BEMPropertyDecorator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
<?php
/**
* This file is part of the QA-Tools library.
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*
* @copyright Alexander Obuhovich <aik.bold@gmail.com>
* @link https://github.com/qa-tools/qa-tools
*/
namespace QATools\QATools\BEM\PropertyDecorator;
use QATools\QATools\BEM\Annotation\BEMAnnotation;
use QATools\QATools\BEM\BEMPage;
use QATools\QATools\BEM\Element\IBlock;
use QATools\QATools\PageObject\ElementLocator\IElementLocator;
use QATools\QATools\PageObject\ElementLocator\IElementLocatorFactory;
use QATools\QATools\PageObject\Exception\AnnotationException;
use QATools\QATools\PageObject\IPageFactory;
use QATools\QATools\PageObject\Proxy\IProxy;
use QATools\QATools\PageObject\Property;
use QATools\QATools\PageObject\PropertyDecorator\DefaultPropertyDecorator;
/**
* Default decorator for use with PageFactory. Will decorate 1) all of the
* WebElement fields and 2) List<WebElement> fields that have @FindBy or
* \@FindBys annotation with a proxy that locates the elements using the passed
* in ElementLocatorFactory.
*
* @method \Mockery\Expectation shouldReceive(string $name)
*/
class BEMPropertyDecorator extends DefaultPropertyDecorator
{
/**
* BEM block interface.
*
* @var string
*/
private $_blockInterface = '\\QATools\\QATools\\BEM\\Element\\IBlock';
/**
* BEM element interface.
*
* @var string
*/
private $_elementInterface = '\\QATools\\QATools\\BEM\\Element\\IElement';
/**
* Creates decorator instance.
*
* @param IElementLocatorFactory $locator_factory Locator factory.
* @param IPageFactory $page_factory Page factory.
*/
public function __construct(IElementLocatorFactory $locator_factory, IPageFactory $page_factory)
{
parent::__construct($locator_factory, $page_factory);
$this->elementToProxyMapping[$this->_blockInterface] = '\\QATools\\QATools\\BEM\\Proxy\\BlockProxy';
$this->elementToProxyMapping[$this->_elementInterface] = '\\QATools\\QATools\\BEM\\Proxy\\ElementProxy';
}
/**
* Perform actual decoration.
*
* @param Property $property The property that may be decorated.
* @param IElementLocator $locator Locator.
*
* @return IProxy|null
*/
protected function doDecorate(Property $property, IElementLocator $locator)
{
$proxy_class = $this->getProxyClass($property);
if ( !$proxy_class ) {
return null;
}
if ( $this->_isBEMBlock($property) || $this->_isBEMElement($property) ) {
/* @var $annotations BEMAnnotation[] */
$annotations = $property->getAnnotationsFromPropertyOrClass('@bem');
$this->_assertAnnotationUsage($annotations, $locator);
if ( !($annotations[0] instanceof BEMAnnotation) ) {
return null;
}
$name = $this->_isBEMBlock($property) ? $annotations[0]->block : $annotations[0]->element;
/* @var $proxy IProxy */
$proxy = new $proxy_class($name, $locator, $this->pageFactory);
$proxy->setClassName($property->getDataType());
return $proxy;
}
return parent::doDecorate($property, $locator);
}
/**
* Checks, that given class is BEM Block or it's descendant.
*
* @param Property $property Property.
*
* @return boolean
*/
private function _isBEMBlock(Property $property)
{
return $this->classMatches($property->getDataType(), $this->_blockInterface);
}
/**
* Checks, that given class is BEM Element or it's descendant.
*
* @param Property $property Property.
*
* @return boolean
*/
private function _isBEMElement(Property $property)
{
return $this->classMatches($property->getDataType(), $this->_elementInterface);
}
/**
* Verifies, that annotations are being correctly used.
*
* @param array $annotations Annotations.
* @param IElementLocator $locator Locator.
*
* @return void
* @throws AnnotationException When annotation is being used incorrectly.
*/
private function _assertAnnotationUsage(array $annotations, IElementLocator $locator)
{
if ( !$annotations || !($annotations[0] instanceof BEMAnnotation) ) {
throw new AnnotationException(
'BEM block/element must be specified as annotation',
AnnotationException::TYPE_REQUIRED
);
}
/** @var BEMAnnotation $annotation */
$annotation = $annotations[0];
if ( ($annotation->element && $annotation->block) || (!$annotation->element && !$annotation->block) ) {
throw new AnnotationException(
"Either 'block' or 'element' key with non-empty value must be specified in the annotation",
AnnotationException::TYPE_INCORRECT_USAGE
);
}
elseif ( $annotation->element && !($locator->getSearchContext() instanceof IBlock) ) {
throw new AnnotationException(
'BEM element can only be used in Block sub-class (or any class, implementing IBlock interface) property',
AnnotationException::TYPE_INCORRECT_USAGE
);
}
elseif ( $annotation->block && !($locator->getSearchContext() instanceof BEMPage) ) {
throw new AnnotationException(
'BEM block can only be used in BEMPage sub-class property',
AnnotationException::TYPE_INCORRECT_USAGE
);
}
}
}