-
Notifications
You must be signed in to change notification settings - Fork 28
Expand file tree
/
Copy pathTestCase.php
More file actions
178 lines (158 loc) · 4.17 KB
/
TestCase.php
File metadata and controls
178 lines (158 loc) · 4.17 KB
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
175
176
177
178
<?php
namespace Behat\Mink\Tests\Driver;
use Behat\Mink\Element\NodeElement;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Behat\Mink\WebAssert;
use PHPUnit\Framework\TestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
public const WEB_FIXTURES_DIR = __DIR__ . '/../web-fixtures';
public const KERNEL_FIXTURES_DIR = __DIR__ . '/../http-kernel-fixtures';
private const DRIVER_CONFIG_FACTORY_KEY = 'driver_config_factory';
private const MINK_SESSION_KEY = 'sess';
/**
* Mink session manager.
*
* @var Mink|null
*/
private static $mink;
/**
* @var AbstractConfig|null
*/
private static $config;
/**
* @beforeClass
*
* @return void
*/
public static function prepareSession()
{
if (null === self::$mink) {
$session = new Session(self::getConfig()->createDriver());
self::$mink = new Mink([self::MINK_SESSION_KEY => $session]);
}
}
/**
* @throws \UnexpectedValueException
*/
private static function createConfig(): AbstractConfig
{
$config = call_user_func($GLOBALS[self::DRIVER_CONFIG_FACTORY_KEY]);
if (!$config instanceof AbstractConfig) {
throw new \UnexpectedValueException(sprintf(
'The "%s" global variable must return a %s.',
self::DRIVER_CONFIG_FACTORY_KEY,
AbstractConfig::class
));
}
return $config;
}
private static function getConfig(): AbstractConfig
{
return self::$config ?? (self::$config = self::createConfig());
}
/**
* @before
*
* @return void
*/
protected function checkSkippedTest()
{
if (null !== $message = self::getConfig()->skipMessage(get_class($this), $this->getName(false))) {
$this->markTestSkipped($message);
}
}
/**
* @after
* @return void
*/
protected function resetSessions()
{
if (null !== self::$mink) {
self::$mink->resetSessions();
}
}
/**
* Returns session.
*
* @return Session
*/
protected function getSession()
{
assert(self::$mink !== null);
return self::$mink->getSession(self::MINK_SESSION_KEY);
}
/**
* Returns assert session.
*
* @return WebAssert
*/
protected function getAssertSession()
{
assert(self::$mink !== null);
return self::$mink->assertSession(self::MINK_SESSION_KEY);
}
/**
* @param string $id
*
* @return NodeElement
*/
protected function findById($id)
{
return $this->getAssertSession()->elementExists('named', ['id', $id]);
}
/**
* Creates a new driver instance.
*
* This driver is not associated to a session. It is meant to be used for tests on the driver
* implementation itself rather than test using the Mink API.
*
* @return \Behat\Mink\Driver\DriverInterface
*/
protected function createDriver()
{
return self::getConfig()->createDriver();
}
/**
* Map remote file path.
*
* @param string $file File path
*
* @return string
*/
protected function mapRemoteFilePath($file)
{
$realPath = realpath($file);
if (false !== $realPath) {
$file = $realPath;
}
return self::getConfig()->mapRemoteFilePath($file);
}
/**
* @param string $path
*
* @return string
*/
protected function pathTo($path)
{
return rtrim(self::getConfig()->getWebFixturesUrl(), '/') . '/' . ltrim($path, '/');
}
/**
* @param int $time
* @param string $condition
*
* @return bool
*
* @deprecated To be removed since drivers are should wait for page navigation automatically and meanwhile tests
* shouldn't try fixing it.
*/
protected function safePageWait($time, $condition)
{
@trigger_error(
sprintf('The method %s is deprecated: drivers should wait for page navigation automatically', __METHOD__),
E_USER_DEPRECATED
);
return true;
}
}