Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / vendor / behat / mink / src / Exception / ExpectationException.php
1 <?php
2
3 /*
4  * This file is part of the Mink package.
5  * (c) Konstantin Kudryashov <ever.zet@gmail.com>
6  *
7  * For the full copyright and license information, please view the LICENSE
8  * file that was distributed with this source code.
9  */
10
11 namespace Behat\Mink\Exception;
12
13 use Behat\Mink\Driver\DriverInterface;
14 use Behat\Mink\Session;
15
16 /**
17  * Exception thrown for failed expectations.
18  *
19  * Some specialized child classes are available to customize the error rendering.
20  *
21  * @author Konstantin Kudryashov <ever.zet@gmail.com>
22  */
23 class ExpectationException extends Exception
24 {
25     private $session;
26     private $driver;
27
28     /**
29      * Initializes exception.
30      *
31      * @param string                  $message   optional message
32      * @param DriverInterface|Session $driver    driver instance (or session for BC)
33      * @param \Exception|null         $exception expectation exception
34      */
35     public function __construct($message, $driver, \Exception $exception = null)
36     {
37         if ($driver instanceof Session) {
38             @trigger_error('Passing a Session object to the ExpectationException constructor is deprecated as of Mink 1.7. Pass the driver instead.', E_USER_DEPRECATED);
39
40             $this->session = $driver;
41             $this->driver = $driver->getDriver();
42         } elseif (!$driver instanceof DriverInterface) {
43             // Trigger an exception as we cannot typehint a disjunction
44             throw new \InvalidArgumentException('The ExpectationException constructor expects a DriverInterface or a Session.');
45         } else {
46             $this->driver = $driver;
47         }
48
49         if (!$message && null !== $exception) {
50             $message = $exception->getMessage();
51         }
52
53         parent::__construct($message, 0, $exception);
54     }
55
56     /**
57      * Returns exception message with additional context info.
58      *
59      * @return string
60      */
61     public function __toString()
62     {
63         try {
64             $pageText = $this->pipeString($this->trimString($this->getContext())."\n");
65             $string = sprintf("%s\n\n%s%s", $this->getMessage(), $this->getResponseInfo(), $pageText);
66         } catch (\Exception $e) {
67             return $this->getMessage();
68         }
69
70         return $string;
71     }
72
73     /**
74      * Gets the context rendered for this exception.
75      *
76      * @return string
77      */
78     protected function getContext()
79     {
80         return $this->trimBody($this->driver->getContent());
81     }
82
83     /**
84      * Returns driver.
85      *
86      * @return DriverInterface
87      */
88     protected function getDriver()
89     {
90         return $this->driver;
91     }
92
93     /**
94      * Returns exception session.
95      *
96      * @return Session
97      *
98      * @deprecated since 1.7, to be removed in 2.0. Use getDriver and the driver API instead.
99      */
100     protected function getSession()
101     {
102         if (null === $this->session) {
103             throw new \LogicException(sprintf('The deprecated method %s cannot be used when passing a driver in the constructor', __METHOD__));
104         }
105
106         @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.'));
107
108         return $this->session;
109     }
110
111     /**
112      * Prepends every line in a string with pipe (|).
113      *
114      * @param string $string
115      *
116      * @return string
117      */
118     protected function pipeString($string)
119     {
120         return '|  '.strtr($string, array("\n" => "\n|  "));
121     }
122
123     /**
124      * Removes response header/footer, letting only <body /> content.
125      *
126      * @param string $string response content
127      *
128      * @return string
129      */
130     protected function trimBody($string)
131     {
132         $string = preg_replace(array('/^.*<body>/s', '/<\/body>.*$/s'), array('<body>', '</body>'), $string);
133
134         return $string;
135     }
136
137     /**
138      * Trims string to specified number of chars.
139      *
140      * @param string $string response content
141      * @param int    $count  trim count
142      *
143      * @return string
144      */
145     protected function trimString($string, $count = 1000)
146     {
147         $string = trim($string);
148
149         if ($count < mb_strlen($string)) {
150             return mb_substr($string, 0, $count - 3).'...';
151         }
152
153         return $string;
154     }
155
156     /**
157      * Returns response information string.
158      *
159      * @return string
160      */
161     protected function getResponseInfo()
162     {
163         $driver = basename(str_replace('\\', '/', get_class($this->driver)));
164
165         $info = '+--[ ';
166         try {
167             $info .= 'HTTP/1.1 '.$this->driver->getStatusCode().' | ';
168         } catch (UnsupportedDriverActionException $e) {
169             // Ignore the status code when not supported
170         }
171         $info .= $this->driver->getCurrentUrl().' | '.$driver." ]\n|\n";
172
173         return $info;
174     }
175 }