1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk/) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino/WebinoDev/ for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2018 Webino, s. r. o. (http://webino.sk/) |
7
|
|
|
* @license BSD-3-Clause |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace WebinoDev\Test\Selenium; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Trait SourceTrait |
14
|
|
|
* |
15
|
|
|
* @property-read object $session |
16
|
|
|
* |
17
|
|
|
* @method assertContains($haystack, $needle) |
18
|
|
|
* @method assertNotContains($haystack, $needle) |
19
|
|
|
*/ |
20
|
|
|
trait WebTrait |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Return web title |
24
|
|
|
* |
25
|
|
|
* @return string |
26
|
|
|
*/ |
27
|
|
|
public function getWebTitle() |
28
|
|
|
{ |
29
|
|
|
return (string) $this->session->title(); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Return web source |
34
|
|
|
* |
35
|
|
|
* @return string |
36
|
|
|
*/ |
37
|
|
|
public function getWebSource() |
38
|
|
|
{ |
39
|
|
|
return (string) $this->session->source(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
protected function getServerUrl() |
46
|
|
|
{ |
47
|
|
|
$parts = parse_url($this->session->url()); |
48
|
|
|
return $parts['scheme'] . '://' . $parts['host']; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
protected function getSessionId() |
55
|
|
|
{ |
56
|
|
|
return $this->session->getCookie('PHPSESSID')['value']; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return bool |
61
|
|
|
*/ |
62
|
|
|
public function is404() |
63
|
|
|
{ |
64
|
|
|
$url = $this->session->url(); |
65
|
|
|
$headers = get_headers($url); |
66
|
|
|
return empty($headers[0]) ? true : ('HTTP/1.0 404 Not Found' === $headers[0]); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Assert that page source contains given string |
71
|
|
|
* |
72
|
|
|
* @param string $string |
73
|
|
|
* @return $this |
74
|
|
|
*/ |
75
|
|
|
public function assertWebContains($string) |
76
|
|
|
{ |
77
|
|
|
$this->assertContains((string) $string, $this->getWebSource()); |
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Assert that page source does not contains given string |
83
|
|
|
* |
84
|
|
|
* @param string $string |
85
|
|
|
* @return $this |
86
|
|
|
*/ |
87
|
|
|
public function assertWebNotContains($string) |
88
|
|
|
{ |
89
|
|
|
$this->assertNotContains((string) $string, $this->getWebSource()); |
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Assert that web page is without errors |
95
|
|
|
* |
96
|
|
|
* @return $this |
97
|
|
|
*/ |
98
|
|
|
protected function assertWebNotError() |
99
|
|
|
{ |
100
|
|
|
$title = $this->getWebTitle(); |
101
|
|
|
$this->assertNotContains('Error', $title); |
102
|
|
|
$this->assertNotContains('Exception', $title); |
103
|
|
|
|
104
|
|
|
// strip script contents & tags |
105
|
|
|
$text = substr($this->getWebSource(), 0, 10000); |
106
|
|
|
$expr = '~<(script).*?>.*?</script>~si'; |
107
|
|
|
$src = strip_tags(preg_replace($expr, '', $text)); |
108
|
|
|
|
109
|
|
|
$this->assertNotContains('Error', $src); |
110
|
|
|
$this->assertNotContains('Exception', $src); |
111
|
|
|
return $this; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @todo remove, deprecated |
116
|
|
|
* @deprecated use assertWebNotError() instead |
117
|
|
|
* @return $this |
118
|
|
|
*/ |
119
|
|
|
protected function assertNotError() |
120
|
|
|
{ |
121
|
|
|
$this->assertWebNotError(); |
122
|
|
|
return $this; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Opens URI and asserts not error |
127
|
|
|
* |
128
|
|
|
* @param string $path |
129
|
|
|
* @param string|null $caption |
130
|
|
|
* @return $this |
131
|
|
|
*/ |
132
|
|
|
protected function open($path = '', $caption = null) |
133
|
|
|
{ |
134
|
|
|
$this->session->open($this->uri . $path); |
|
|
|
|
135
|
|
|
$this->debugNotify($caption); |
|
|
|
|
136
|
|
|
$this->assertWebNotError(); |
137
|
|
|
return $this; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* @param string $path |
142
|
|
|
* @param string $caption |
143
|
|
|
* @return $this |
144
|
|
|
* @deprecated use open() |
145
|
|
|
* @todo remove |
146
|
|
|
*/ |
147
|
|
|
protected function openOk($path = '', $caption = 'Home') |
148
|
|
|
{ |
149
|
|
|
return $this->open($path, $caption); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Get raw source from URL |
154
|
|
|
* |
155
|
|
|
* @param string $url |
156
|
|
|
* @param null $sessId |
157
|
|
|
* @return string |
158
|
|
|
*/ |
159
|
|
|
protected function source($url, $sessId = null) |
160
|
|
|
{ |
161
|
|
|
$sid = $sessId ? $sessId : $this->getSessionId(); |
162
|
|
|
$opts = ['http' => ['header' => 'Cookie: PHPSESSID=' . $sid ."\r\n"]]; |
163
|
|
|
return file_get_contents($url, false, stream_context_create($opts)); |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: