-
Notifications
You must be signed in to change notification settings - Fork 286
Remove the dependency on the Session in expectation exceptions #663
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| namespace Behat\Mink\Exception; | ||
|
|
||
| use Behat\Mink\Driver\DriverInterface; | ||
| use Behat\Mink\Session; | ||
|
|
||
| /** | ||
|
|
@@ -22,17 +23,28 @@ | |
| class ExpectationException extends Exception | ||
| { | ||
| private $session; | ||
| private $driver; | ||
|
|
||
| /** | ||
| * Initializes exception. | ||
| * | ||
| * @param string $message optional message | ||
| * @param Session $session session instance | ||
| * @param \Exception $exception expectation 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, Session $session, \Exception $exception = null) | ||
| public function __construct($message, $driver, \Exception $exception = null) | ||
| { | ||
| $this->session = $session; | ||
| 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); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the point of using error hiding (via
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see #662 |
||
|
|
||
| $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(); | ||
|
|
@@ -65,16 +77,34 @@ public function __toString() | |
| */ | ||
| protected function getContext() | ||
| { | ||
| return $this->trimBody($this->getSession()->getPage()->getContent()); | ||
| 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__)); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exception message says, that method is deprecated, but we're missing
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. right. This is a mistake |
||
| } | ||
|
|
||
| @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.')); | ||
|
|
||
| return $this->session; | ||
| } | ||
|
|
||
|
|
@@ -130,15 +160,15 @@ protected function trimString($string, $count = 1000) | |
| */ | ||
| protected function getResponseInfo() | ||
| { | ||
| $driver = basename(str_replace('\\', '/', get_class($this->session->getDriver()))); | ||
| $driver = basename(str_replace('\\', '/', get_class($this->driver))); | ||
|
|
||
| $info = '+--[ '; | ||
| try { | ||
| $info .= 'HTTP/1.1 '.$this->session->getStatusCode().' | '; | ||
| $info .= 'HTTP/1.1 '.$this->driver->getStatusCode().' | '; | ||
| } catch (UnsupportedDriverActionException $e) { | ||
| // Ignore the status code when not supported | ||
| } | ||
| $info .= $this->session->getCurrentUrl().' | '.$driver." ]\n|\n"; | ||
| $info .= $this->driver->getCurrentUrl().' | '.$driver." ]\n|\n"; | ||
|
|
||
| return $info; | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -417,7 +417,7 @@ public function elementExists($selectorType, $selector, ElementInterface $contai | |
| $selector = implode(' ', $selector); | ||
| } | ||
|
|
||
| throw new ElementNotFoundException($this->session, 'element', $selectorType, $selector); | ||
| throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nope. this class uses the Session API. |
||
| } | ||
|
|
||
| return $node; | ||
|
|
@@ -635,7 +635,7 @@ public function fieldExists($field, TraversableElement $container = null) | |
| $node = $container->findField($field); | ||
|
|
||
| if (null === $node) { | ||
| throw new ElementNotFoundException($this->session, 'form field', 'id|name|label|value', $field); | ||
| throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field); | ||
| } | ||
|
|
||
| return $node; | ||
|
|
@@ -767,7 +767,7 @@ private function assert($condition, $message) | |
| return; | ||
| } | ||
|
|
||
| throw new ExpectationException($message, $this->session); | ||
| throw new ExpectationException($message, $this->session->getDriver()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -784,7 +784,7 @@ private function assertResponseText($condition, $message) | |
| return; | ||
| } | ||
|
|
||
| throw new ResponseTextException($message, $this->session); | ||
| throw new ResponseTextException($message, $this->session->getDriver()); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -802,7 +802,7 @@ private function assertElement($condition, $message, Element $element) | |
| return; | ||
| } | ||
|
|
||
| throw new ElementHtmlException($message, $this->session, $element); | ||
| throw new ElementHtmlException($message, $this->session->getDriver(), $element); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -820,7 +820,7 @@ private function assertElementText($condition, $message, Element $element) | |
| return; | ||
| } | ||
|
|
||
| throw new ElementTextException($message, $this->session, $element); | ||
| throw new ElementTextException($message, $this->session->getDriver(), $element); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering whether we should throw exceptions in place now and deprecate this method (this was added in 1.6 just so that our own child classes can avoid using the deprecated Session getter)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this just an internal method that contained some logic before, but not now? If so, then we'd better show exception where needed instead of calling method, that throws them for us. This would also give us shorter stacktrace for that exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
agreed. Precisely my point.
there was no logic before, but it allowed to access the session without using the deprecated getter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done