-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
PathTrait.php
120 lines (102 loc) · 3.14 KB
/
PathTrait.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
<?php
declare(strict_types=1);
namespace DrevOps\BehatSteps;
/**
* Trait PathTrait.
*
* Path-related assertions.
*
* @package DrevOps\BehatSteps
*/
trait PathTrait {
/**
* Assert current page is specified path.
*
* Note that "<front>" is supported as path.
*
* @code
* Then I should be in the "/about-us" path
* Then I should be in the "<front>" path
* @endcode
*
* @Then I should be in the :path path
*/
public function pathAssertCurrent(string $path): void {
$current_path = $this->getSession()->getCurrentUrl();
if (empty($current_path)) {
throw new \Exception('Current path is empty');
}
$current_path = parse_url((string) $current_path, PHP_URL_PATH);
if ($current_path === FALSE) {
throw new \Exception('Current path is not a valid URL');
}
$current_path = ltrim((string) $current_path, '/');
$current_path = $current_path === '' ? '<front>' : $current_path;
if ($current_path !== ltrim($path, '/')) {
throw new \Exception(sprintf('Current path is "%s", but expected is "%s"', $current_path, $path));
}
}
/**
* Assert current page is not specified path.
*
* Note that "<front>" is supported as path.
*
* @code
* Then I should not be in the "/about-us" path
* Then I should not be in the "<front>" path
* @endcode
*
* @Then I should not be in the :path path
*/
public function pathAssertNotCurrent(string $path): bool {
$current_path = $this->getSession()->getCurrentUrl();
if (empty($current_path)) {
throw new \Exception('Current path is empty');
}
$current_path = parse_url((string) $current_path, PHP_URL_PATH);
if ($current_path === FALSE) {
throw new \Exception('Current path is not a valid URL');
}
$current_path = ltrim((string) $current_path, '/');
$current_path = $current_path === '' ? '<front>' : $current_path;
if ($current_path === $path) {
throw new \Exception(sprintf('Current path should not be "%s"', $current_path));
}
return TRUE;
}
/**
* Assert that a path can be visited or not with HTTP credentials.
*
* @code
* Then I "can" visit "/about-us" with HTTP credentials "user" "pass"
* Then I "cannot" visit "/about-us" with HTTP credentials "user" "pass"
* @endcode
*
* @Then I :can visit :path with HTTP credentials :user :pass
*/
public function pathAssertVisitWithBasicAuth(string $can, string $path, string $user, string $pass): void {
$this->getSession()->setBasicAuth($user, $pass);
$this->visitPath($path);
if ($can === 'can') {
$this->assertSession()->statusCodeEquals(200);
}
else {
$this->assertSession()->statusCodeNotEquals(200);
}
}
/**
* Visit a path and assert the final destination.
*
* Useful for pages with redirects.
*
* @code
* When I visit "/node/123" then the final URL should be "/about/us"
* @endcode
*
* @When I visit :path then the final URL should be :alias
*/
public function pathAssertWithRedirect(string $path, string $alias): void {
$this->getSession()->visit($this->locatePath($path));
$this->pathAssertCurrent($alias);
}
}