Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions src/Element/Element.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function __construct(Session $session)
*/
public function getSession()
{
@trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);
@trigger_error(sprintf('The method %s is deprecated as of 1.6 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);

return $this->session;
}
Expand Down Expand Up @@ -197,17 +197,18 @@ public function getOuterHtml()
/**
* Builds an ElementNotFoundException
*
* This is an helper to build the ElementNotFoundException without
* needing to use the deprecated getSession accessor in child classes.
*
* @param string $type
* @param string|null $selector
* @param string|null $locator
*
* @return ElementNotFoundException
*
* @deprecated as of 1.7, to be removed in 2.0
*/
protected function elementNotFound($type, $selector = null, $locator = null)
Copy link
Member Author

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)

Copy link
Member

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.

Copy link
Member Author

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

{
return new ElementNotFoundException($this->session, $type, $selector, $locator);
@trigger_error(sprintf('The method %s is deprecated as of 1.7 and will be removed in 2.0', __METHOD__), E_USER_DEPRECATED);

return new ElementNotFoundException($this->driver, $type, $selector, $locator);
}
}
2 changes: 1 addition & 1 deletion src/Element/NodeElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ public function selectOption($option, $multiple = false)
$opt = $this->find('named', array('option', $option));

if (null === $opt) {
throw $this->elementNotFound('select option', 'value|text', $option);
throw new ElementNotFoundException($this->getDriver(), 'select option', 'value|text', $option);
}

$this->getDriver()->selectOption($this->getXpath(), $opt->getValue(), $multiple);
Expand Down
14 changes: 7 additions & 7 deletions src/Element/TraversableElement.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public function clickLink($locator)
$link = $this->findLink($locator);

if (null === $link) {
throw $this->elementNotFound('link', 'id|title|alt|text', $locator);
throw new ElementNotFoundException($this->getDriver(), 'link', 'id|title|alt|text', $locator);
}

$link->click();
Expand Down Expand Up @@ -109,7 +109,7 @@ public function pressButton($locator)
$button = $this->findButton($locator);

if (null === $button) {
throw $this->elementNotFound('button', 'id|name|title|alt|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'button', 'id|name|title|alt|value', $locator);
}

$button->press();
Expand Down Expand Up @@ -154,7 +154,7 @@ public function fillField($locator, $value)
$field = $this->findField($locator);

if (null === $field) {
throw $this->elementNotFound('form field', 'id|name|label|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
}

$field->setValue($value);
Expand Down Expand Up @@ -204,7 +204,7 @@ public function checkField($locator)
$field = $this->findField($locator);

if (null === $field) {
throw $this->elementNotFound('form field', 'id|name|label|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
}

$field->check();
Expand All @@ -222,7 +222,7 @@ public function uncheckField($locator)
$field = $this->findField($locator);

if (null === $field) {
throw $this->elementNotFound('form field', 'id|name|label|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
}

$field->uncheck();
Expand Down Expand Up @@ -256,7 +256,7 @@ public function selectFieldOption($locator, $value, $multiple = false)
$field = $this->findField($locator);

if (null === $field) {
throw $this->elementNotFound('form field', 'id|name|label|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
}

$field->selectOption($value, $multiple);
Expand Down Expand Up @@ -289,7 +289,7 @@ public function attachFileToField($locator, $path)
$field = $this->findField($locator);

if (null === $field) {
throw $this->elementNotFound('form field', 'id|name|label|value', $locator);
throw new ElementNotFoundException($this->getDriver(), 'form field', 'id|name|label|value', $locator);
}

$field->attachFile($path);
Expand Down
15 changes: 8 additions & 7 deletions src/Exception/ElementHtmlException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@

namespace Behat\Mink\Exception;

use Behat\Mink\Session;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Element\Element;
use Behat\Mink\Session;

/**
* Exception thrown when an expectation on the HTML of an element fails.
Expand All @@ -30,16 +31,16 @@ class ElementHtmlException extends ExpectationException
/**
* Initializes exception.
*
* @param string $message optional message
* @param Session $session session instance
* @param Element $element element
* @param \Exception $exception expectation exception
* @param string $message optional message
* @param DriverInterface|Session $driver driver instance
* @param Element $element element
* @param \Exception $exception expectation exception
*/
public function __construct($message, Session $session, Element $element, \Exception $exception = null)
public function __construct($message, $driver, Element $element, \Exception $exception = null)
{
$this->element = $element;

parent::__construct($message, $session, $exception);
parent::__construct($message, $driver, $exception);
}

protected function getContext()
Expand Down
13 changes: 7 additions & 6 deletions src/Exception/ElementNotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Behat\Mink\Exception;

use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Session;

/**
Expand All @@ -22,12 +23,12 @@ class ElementNotFoundException extends ExpectationException
/**
* Initializes exception.
*
* @param Session $session session instance
* @param string $type element type
* @param string $selector element selector type
* @param string $locator element locator
* @param DriverInterface|Session $driver driver instance
* @param string $type element type
* @param string $selector element selector type
* @param string $locator element locator
*/
public function __construct(Session $session, $type = null, $selector = null, $locator = null)
public function __construct($driver, $type = null, $selector = null, $locator = null)
{
$message = '';

Expand All @@ -48,6 +49,6 @@ public function __construct(Session $session, $type = null, $selector = null, $l

$message .= ' not found.';

parent::__construct($message, $session);
parent::__construct($message, $driver);
}
}
48 changes: 39 additions & 9 deletions src/Exception/ExpectationException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Behat\Mink\Exception;

use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Session;

/**
Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the point of using error hiding (via @) if we want to trigger an error?

Copy link
Member Author

Choose a reason for hiding this comment

The 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();
Expand Down Expand Up @@ -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__));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Exception message says, that method is deprecated, but we're missing @deprecated in the DocBlock.

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}

Expand Down Expand Up @@ -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;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/ResponseTextException.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class ResponseTextException extends ExpectationException
{
protected function getContext()
{
return $this->getSession()->getPage()->getText();
return $this->getDriver()->getText('//html');
}
}
12 changes: 6 additions & 6 deletions src/WebAssert.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't $this->driver available in this class?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope. this class uses the Session API.

}

return $node;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -767,7 +767,7 @@ private function assert($condition, $message)
return;
}

throw new ExpectationException($message, $this->session);
throw new ExpectationException($message, $this->session->getDriver());
}

/**
Expand All @@ -784,7 +784,7 @@ private function assertResponseText($condition, $message)
return;
}

throw new ResponseTextException($message, $this->session);
throw new ResponseTextException($message, $this->session->getDriver());
}

/**
Expand All @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down
17 changes: 3 additions & 14 deletions tests/Exception/ElementHtmlExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,10 @@ public function testExceptionToString()
$driver = $this->getMock('Behat\Mink\Driver\DriverInterface');
$element = $this->getElementMock();

$session = $this->getSessionMock();
$session->expects($this->any())
->method('getDriver')
->will($this->returnValue($driver));
$session->expects($this->any())
$driver->expects($this->any())
->method('getStatusCode')
->will($this->returnValue(200));
$session->expects($this->any())
$driver->expects($this->any())
->method('getCurrentUrl')
->will($this->returnValue('http://localhost/test'));

Expand All @@ -40,18 +36,11 @@ public function testExceptionToString()

$expected = sprintf($expected.' ', get_class($driver));

$exception = new ElementHtmlException('Html error', $session, $element);
$exception = new ElementHtmlException('Html error', $driver, $element);

$this->assertEquals($expected, $exception->__toString());
}

private function getSessionMock()
{
return $this->getMockBuilder('Behat\Mink\Session')
->disableOriginalConstructor()
->getMock();
}

private function getElementMock()
{
return $this->getMockBuilder('Behat\Mink\Element\NodeElement')
Expand Down
Loading