1010
1111namespace Behat \Mink \Exception ;
1212
13+ use Behat \Mink \Driver \DriverInterface ;
1314use Behat \Mink \Session ;
1415
1516/**
2223class ExpectationException extends Exception
2324{
2425 private $ session ;
26+ private $ driver ;
2527
2628 /**
2729 * Initializes exception.
2830 *
29- * @param string $message optional message
30- * @param Session $session session instance
31- * @param \Exception $exception expectation exception
31+ * @param string $message optional message
32+ * @param DriverInterface| Session $driver driver instance (or session for BC)
33+ * @param \Exception|null $exception expectation exception
3234 */
33- public function __construct ($ message , Session $ session , \Exception $ exception = null )
35+ public function __construct ($ message , $ driver , \Exception $ exception = null )
3436 {
35- $ this ->session = $ session ;
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+ }
3648
3749 if (!$ message && null !== $ exception ) {
3850 $ message = $ exception ->getMessage ();
@@ -65,7 +77,17 @@ public function __toString()
6577 */
6678 protected function getContext ()
6779 {
68- return $ this ->trimBody ($ this ->getSession ()->getPage ()->getContent ());
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 ;
6991 }
7092
7193 /**
@@ -75,6 +97,12 @@ protected function getContext()
7597 */
7698 protected function getSession ()
7799 {
100+ if (null === $ this ->session ) {
101+ throw new \LogicException (sprintf ('The deprecated method %s cannot be used when passing a driver in the constructor ' , __METHOD__ ));
102+ }
103+
104+ @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. ' ));
105+
78106 return $ this ->session ;
79107 }
80108
@@ -130,15 +158,15 @@ protected function trimString($string, $count = 1000)
130158 */
131159 protected function getResponseInfo ()
132160 {
133- $ driver = basename (str_replace ('\\' , '/ ' , get_class ($ this ->session -> getDriver () )));
161+ $ driver = basename (str_replace ('\\' , '/ ' , get_class ($ this ->driver )));
134162
135163 $ info = '+--[ ' ;
136164 try {
137- $ info .= 'HTTP/1.1 ' .$ this ->session ->getStatusCode ().' | ' ;
165+ $ info .= 'HTTP/1.1 ' .$ this ->driver ->getStatusCode ().' | ' ;
138166 } catch (UnsupportedDriverActionException $ e ) {
139167 // Ignore the status code when not supported
140168 }
141- $ info .= $ this ->session ->getCurrentUrl ().' | ' .$ driver ." ] \n| \n" ;
169+ $ info .= $ this ->driver ->getCurrentUrl ().' | ' .$ driver ." ] \n| \n" ;
142170
143171 return $ info ;
144172 }
0 commit comments