-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StoreCommandRunnerTest.php
174 lines (160 loc) · 7.35 KB
/
StoreCommandRunnerTest.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
167
168
169
170
171
172
173
174
<?php
namespace Tienvx\Bundle\MbtBundle\Tests\Command\Runner;
use Facebook\WebDriver\WebDriverBy;
use Facebook\WebDriver\WebDriverElement;
use Tienvx\Bundle\MbtBundle\Command\CommandRunner;
use Tienvx\Bundle\MbtBundle\Command\Runner\StoreCommandRunner;
use Tienvx\Bundle\MbtBundle\ValueObject\Model\Command;
/**
* @covers \Tienvx\Bundle\MbtBundle\Command\Runner\StoreCommandRunner
* @covers \Tienvx\Bundle\MbtBundle\Command\CommandRunner
*
* @uses \Tienvx\Bundle\MbtBundle\Model\Model\Revision\Command
*/
class StoreCommandRunnerTest extends RunnerTestCase
{
protected function createRunner(): CommandRunner
{
return new StoreCommandRunner();
}
public function testStore(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE);
$command->setTarget('1');
$command->setValue('count');
$this->values->expects($this->once())->method('setValue')->with('count', '1');
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreAttribute(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_ATTRIBUTE);
$command->setTarget('css=.readmore@href');
$command->setValue('readmoreLink');
$this->values->expects($this->once())->method('setValue')->with('readmoreLink', 'http://example.com');
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('getAttribute')->with('href')->willReturn('http://example.com');
$this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) {
return $selector instanceof WebDriverBy
&& 'css selector' === $selector->getMechanism()
&& '.readmore' === $selector->getValue();
}))->willReturn($element);
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreElementCount(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_ELEMENT_COUNT);
$command->setTarget('css=.item');
$command->setValue('itemCount');
$this->values->expects($this->once())->method('setValue')->with('itemCount', 2);
$element = $this->createMock(WebDriverElement::class);
$this->driver->expects($this->once())->method('findElements')->with($this->callback(function ($selector) {
return $selector instanceof WebDriverBy
&& 'css selector' === $selector->getMechanism()
&& '.item' === $selector->getValue();
}))->willReturn([$element, $element]);
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreJson(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_JSON);
$command->setTarget('{ "items": [1, 2, 3] }');
$command->setValue('json');
$this->values->expects($this->once())->method('setValue')->with(
'json',
$this->callback(fn ($object) => $object instanceof \stdClass && $object->items === [1, 2, 3])
);
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreText(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_TEXT);
$command->setTarget('css=.head-line');
$command->setValue('headLine');
$this->values->expects($this->once())->method('setValue')->with('headLine', 'Welcome to our site');
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('getText')->willReturn('Welcome to our site');
$this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) {
return $selector instanceof WebDriverBy
&& 'css selector' === $selector->getMechanism()
&& '.head-line' === $selector->getValue();
}))->willReturn($element);
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreTitle(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_TITLE);
$command->setTarget('title');
$this->values->expects($this->once())->method('setValue')->with('title', 'Welcome');
$this->driver->expects($this->once())->method('getTitle')->willReturn('Welcome');
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreValue(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_VALUE);
$command->setTarget('css=.age');
$command->setValue('age');
$this->values->expects($this->once())->method('setValue')->with('age', 23);
$element = $this->createMock(WebDriverElement::class);
$element->expects($this->once())->method('getAttribute')->with('value')->willReturn(23);
$this->driver->expects($this->once())->method('findElement')->with($this->callback(function ($selector) {
return $selector instanceof WebDriverBy
&& 'css selector' === $selector->getMechanism()
&& '.age' === $selector->getValue();
}))->willReturn($element);
$this->runner->run($command, $this->values, $this->driver);
}
public function testStoreWindowHandle(): void
{
$command = new Command();
$command->setCommand(StoreCommandRunner::STORE_WINDOW_HANDLE);
$command->setTarget('windowHandle');
$this->values->expects($this->once())->method('setValue')->with('windowHandle', 'window-123');
$this->driver->expects($this->once())->method('getWindowHandle')->willReturn('window-123');
$this->runner->run($command, $this->values, $this->driver);
}
public function targetProvider(): array
{
return [
[StoreCommandRunner::STORE_ATTRIBUTE, null, false],
[StoreCommandRunner::STORE_ATTRIBUTE, 'anything', false],
[StoreCommandRunner::STORE_ATTRIBUTE, 'xpath=//path/to/element@attribute', true],
[StoreCommandRunner::STORE_TEXT, null, false],
[StoreCommandRunner::STORE_TEXT, 'anything', false],
[StoreCommandRunner::STORE_TEXT, 'xpath=//path/to/element', true],
[StoreCommandRunner::STORE_JSON, null, false],
[StoreCommandRunner::STORE_JSON, 'anything', false],
[StoreCommandRunner::STORE_JSON, '{"key": "value"}', true],
];
}
public function commandsRequireTarget(): array
{
return [
StoreCommandRunner::STORE_ATTRIBUTE,
StoreCommandRunner::STORE_ELEMENT_COUNT,
StoreCommandRunner::STORE_JSON,
StoreCommandRunner::STORE_TEXT,
StoreCommandRunner::STORE_TITLE,
StoreCommandRunner::STORE_VALUE,
StoreCommandRunner::STORE_WINDOW_HANDLE,
];
}
public function commandsRequireValue(): array
{
return [
StoreCommandRunner::STORE,
StoreCommandRunner::STORE_ATTRIBUTE,
StoreCommandRunner::STORE_ELEMENT_COUNT,
StoreCommandRunner::STORE_JSON,
StoreCommandRunner::STORE_TEXT,
StoreCommandRunner::STORE_TITLE,
StoreCommandRunner::STORE_VALUE,
];
}
}