-
Notifications
You must be signed in to change notification settings - Fork 279
/
Copy pathCoreDriverTest.php
148 lines (113 loc) · 4.41 KB
/
CoreDriverTest.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
<?php
namespace Behat\Mink\Tests\Driver;
use Behat\Mink\Element\NodeElement;
use PHPUnit\Framework\TestCase;
class CoreDriverTest extends TestCase
{
public function testNoExtraMethods()
{
$interfaceRef = new \ReflectionClass('Behat\Mink\Driver\DriverInterface');
$coreDriverRef = new \ReflectionClass('Behat\Mink\Driver\CoreDriver');
foreach ($coreDriverRef->getMethods(\ReflectionMethod::IS_PUBLIC) as $method) {
$this->assertTrue(
$interfaceRef->hasMethod($method->getName()),
sprintf('CoreDriver should not implement methods which are not part of the DriverInterface but %s found', $method->getName())
);
}
}
public function testCreateNodeElements()
{
if(\PHPUnit\Runner\Version::id() >= 10) {
$driver = $this->getMockBuilder('Behat\Mink\Driver\CoreDriver')
->onlyMethods(array('findElementXpaths'))
->getMockForAbstractClass();
} else {
$driver = $this->getMockBuilder('Behat\Mink\Driver\CoreDriver')
->setMethods(array('findElementXpaths'))
->getMockForAbstractClass();
}
$session = $this->getMockBuilder('Behat\Mink\Session')
->disableOriginalConstructor()
->getMock();
$driver->setSession($session);
$driver->expects($this->once())
->method('findElementXpaths')
->with('xpath')
->willReturn(array('xpath1', 'xpath2'));
/** @var NodeElement[] $elements */
$elements = $driver->find('xpath');
$this->assertIsArray($elements);
$this->assertCount(2, $elements);
$this->assertContainsOnlyInstancesOf('Behat\Mink\Element\NodeElement', $elements);
$this->assertSame('xpath1', $elements[0]->getXpath());
$this->assertSame('xpath2', $elements[1]->getXpath());
}
/**
* @dataProvider getDriverInterfaceMethods
*/
public function testInterfaceMethods(\ReflectionMethod $method)
{
$refl = new \ReflectionClass('Behat\Mink\Driver\CoreDriver');
$coreDriverMethod = $refl->getMethod($method->getName());
$this->assertFalse(
$coreDriverMethod->isAbstract(),
sprintf('CoreDriver should implement a dummy %s method', $method->getName())
);
if ('setSession' === $method->getName()) {
return; // setSession is actually implemented, so we don't expect an exception here.
}
$driver = $this->getMockForAbstractClass('Behat\Mink\Driver\CoreDriver');
$this->expectException('Behat\Mink\Exception\UnsupportedDriverActionException');
$coreDriverMethod->invokeArgs($driver, $this->getArguments($method));
}
public static function getDriverInterfaceMethods()
{
$ref = new \ReflectionClass('Behat\Mink\Driver\DriverInterface');
return array_map(function ($method) {
return array($method);
}, $ref->getMethods());
}
/**
* @return list<mixed>
*/
private function getArguments(\ReflectionMethod $method)
{
$arguments = array();
foreach ($method->getParameters() as $parameter) {
$arguments[] = $this->getArgument($parameter);
}
return $arguments;
}
/**
* @return mixed
*/
private function getArgument(\ReflectionParameter $argument)
{
if ($argument->isOptional()) {
return $argument->getDefaultValue();
}
if ($argument->allowsNull()) {
return null;
}
$type = $argument->getType();
if ($type instanceof \ReflectionNamedType) {
switch ($type->getName()) {
case 'string':
return '';
case 'int':
return 0;
case 'bool':
return false;
case 'float':
return 0.0;
default:
if ($type->isBuiltin()) {
throw new \UnexpectedValueException(sprintf('The type "%s" is not supported by the generation of fake value. Please update the implementation.', $type->getName()));
}
\assert(class_exists($type->getName()) || interface_exists($type->getName()));
return $this->createStub($type->getName());
}
}
return null;
}
}