ExpectationException.php
4.72 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
<?php
/*
* This file is part of the Mink package.
* (c) Konstantin Kudryashov <ever.zet@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Behat\Mink\Exception;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Session;
/**
* Exception thrown for failed expectations.
*
* Some specialized child classes are available to customize the error rendering.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class ExpectationException extends Exception
{
private $session;
private $driver;
/**
* Initializes exception.
*
* @param string $message optional message
* @param DriverInterface|Session $driver driver instance (or session for BC)
* @param \Exception|null $exception expectation exception
*/
public function __construct($message, $driver, \Exception $exception = null)
{
if ($driver instanceof Session) {
@trigger_error('Passing a Session object to the ExpectationException constructor is deprecated as of Mink 1.7. Pass the driver instead.', E_USER_DEPRECATED);
$this->session = $driver;
$this->driver = $driver->getDriver();
} elseif (!$driver instanceof DriverInterface) {
// Trigger an exception as we cannot typehint a disjunction
throw new \InvalidArgumentException('The ExpectationException constructor expects a DriverInterface or a Session.');
} else {
$this->driver = $driver;
}
if (!$message && null !== $exception) {
$message = $exception->getMessage();
}
parent::__construct($message, 0, $exception);
}
/**
* Returns exception message with additional context info.
*
* @return string
*/
public function __toString()
{
try {
$pageText = $this->pipeString($this->trimString($this->getContext())."\n");
$string = sprintf("%s\n\n%s%s", $this->getMessage(), $this->getResponseInfo(), $pageText);
} catch (\Exception $e) {
return $this->getMessage();
}
return $string;
}
/**
* Gets the context rendered for this exception.
*
* @return string
*/
protected function getContext()
{
return $this->trimBody($this->driver->getContent());
}
/**
* Returns driver.
*
* @return DriverInterface
*/
protected function getDriver()
{
return $this->driver;
}
/**
* Returns exception session.
*
* @return Session
*
* @deprecated since 1.7, to be removed in 2.0. Use getDriver and the driver API instead.
*/
protected function getSession()
{
if (null === $this->session) {
throw new \LogicException(sprintf('The deprecated method %s cannot be used when passing a driver in the constructor', __METHOD__));
}
@trigger_error(sprintf('The method %s is deprecated as of Mink 1.7 and will be removed in 2.0. Use getDriver and the driver API instead.', __METHOD__), E_USER_DEPRECATED);
return $this->session;
}
/**
* Prepends every line in a string with pipe (|).
*
* @param string $string
*
* @return string
*/
protected function pipeString($string)
{
return '| '.strtr($string, array("\n" => "\n| "));
}
/**
* Removes response header/footer, letting only <body /> content.
*
* @param string $string response content
*
* @return string
*/
protected function trimBody($string)
{
$string = preg_replace(array('/^.*<body>/s', '/<\/body>.*$/s'), array('<body>', '</body>'), $string);
return $string;
}
/**
* Trims string to specified number of chars.
*
* @param string $string response content
* @param int $count trim count
*
* @return string
*/
protected function trimString($string, $count = 1000)
{
$string = trim($string);
if ($count < mb_strlen($string)) {
return mb_substr($string, 0, $count - 3).'...';
}
return $string;
}
/**
* Returns response information string.
*
* @return string
*/
protected function getResponseInfo()
{
$driver = basename(str_replace('\\', '/', get_class($this->driver)));
$info = '+--[ ';
try {
$info .= 'HTTP/1.1 '.$this->driver->getStatusCode().' | ';
} catch (UnsupportedDriverActionException $e) {
// Ignore the status code when not supported
}
$info .= $this->driver->getCurrentUrl().' | '.$driver." ]\n|\n";
return $info;
}
}