forked from minkphp/driver-testsuite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBestPracticesTest.php
86 lines (69 loc) · 2.63 KB
/
BestPracticesTest.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
<?php
namespace Behat\Mink\Tests\Driver\Basic;
use Behat\Mink\Driver\CoreDriver;
use Behat\Mink\Tests\Driver\TestCase;
/**
* This testcase ensures that the driver implementation follows recommended practices for drivers.
*/
final class BestPracticesTest extends TestCase
{
public function testExtendsCoreDriver(): void
{
$driver = $this->createDriver();
$this->assertInstanceOf(CoreDriver::class, $driver);
}
/**
* @depends testExtendsCoreDriver
*/
public function testImplementFindXpath(): void
{
$driver = $this->createDriver();
$this->assertMethodIsNotImplemented('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
$this->assertMethodIsImplemented('findElementXpaths', $driver, 'The driver must be able to find elements.');
$this->assertMethodIsNotImplemented('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
}
/**
* @dataProvider provideRequiredMethods
*/
public function testImplementBasicApi(string $method): void
{
$driver = $this->createDriver();
$this->assertMethodIsImplemented($method, $driver, 'The driver is unusable when this method is not implemented.');
}
/**
* @return iterable<array{string}>
*/
public static function provideRequiredMethods(): iterable
{
return [
['start'],
['isStarted'],
['stop'],
['reset'],
['visit'],
['getCurrentUrl'],
['getContent'],
['click'],
];
}
private function assertMethodIsImplemented(string $method, object $object, string $reason = ''): void
{
$ref = new \ReflectionClass(get_class($object));
$refMethod = $ref->getMethod($method);
$message = sprintf('The driver should implement the `%s` method.', $method);
if ('' !== $reason) {
$message .= ' ' . $reason;
}
$this->assertNotSame(CoreDriver::class, $refMethod->getDeclaringClass()->name, $message);
}
private function assertMethodIsNotImplemented(string $method, object $object, string $reason = ''): void
{
$ref = new \ReflectionClass(get_class($object));
$refMethod = $ref->getMethod($method);
$message = sprintf('The driver should not implement the `%s` method.', $method);
if ('' !== $reason) {
$message .= ' ' . $reason;
}
$this->assertSame(CoreDriver::class, $refMethod->getDeclaringClass()->name, $message);
}
}