diff --git a/src/Element/Element.php b/src/Element/Element.php index 7a594883b..05df64d95 100644 --- a/src/Element/Element.php +++ b/src/Element/Element.php @@ -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; } @@ -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) { - 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); } } diff --git a/src/Element/NodeElement.php b/src/Element/NodeElement.php index d79fb8d60..1064a7d8a 100644 --- a/src/Element/NodeElement.php +++ b/src/Element/NodeElement.php @@ -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); diff --git a/src/Element/TraversableElement.php b/src/Element/TraversableElement.php index e6bb8d7ab..f73d49558 100644 --- a/src/Element/TraversableElement.php +++ b/src/Element/TraversableElement.php @@ -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(); @@ -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(); @@ -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); @@ -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(); @@ -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(); @@ -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); @@ -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); diff --git a/src/Exception/ElementHtmlException.php b/src/Exception/ElementHtmlException.php index 99e2ea601..fb2fed68e 100644 --- a/src/Exception/ElementHtmlException.php +++ b/src/Exception/ElementHtmlException.php @@ -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. @@ -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() diff --git a/src/Exception/ElementNotFoundException.php b/src/Exception/ElementNotFoundException.php index 95fd5f0b8..6716ab3ff 100644 --- a/src/Exception/ElementNotFoundException.php +++ b/src/Exception/ElementNotFoundException.php @@ -10,6 +10,7 @@ namespace Behat\Mink\Exception; +use Behat\Mink\Driver\DriverInterface; use Behat\Mink\Session; /** @@ -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 = ''; @@ -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); } } diff --git a/src/Exception/ExpectationException.php b/src/Exception/ExpectationException.php index 25034d1e7..6cbaadaf9 100644 --- a/src/Exception/ExpectationException.php +++ b/src/Exception/ExpectationException.php @@ -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); + + $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__)); + } + + @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; } diff --git a/src/Exception/ResponseTextException.php b/src/Exception/ResponseTextException.php index 145ed6da0..515c5694d 100644 --- a/src/Exception/ResponseTextException.php +++ b/src/Exception/ResponseTextException.php @@ -19,6 +19,6 @@ class ResponseTextException extends ExpectationException { protected function getContext() { - return $this->getSession()->getPage()->getText(); + return $this->getDriver()->getText('//html'); } } diff --git a/src/WebAssert.php b/src/WebAssert.php index 213b416a3..01ea2b969 100644 --- a/src/WebAssert.php +++ b/src/WebAssert.php @@ -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); } 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); } /** diff --git a/tests/Exception/ElementHtmlExceptionTest.php b/tests/Exception/ElementHtmlExceptionTest.php index 63d39dd0c..7d6ea8252 100644 --- a/tests/Exception/ElementHtmlExceptionTest.php +++ b/tests/Exception/ElementHtmlExceptionTest.php @@ -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')); @@ -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') diff --git a/tests/Exception/ElementNotFoundExceptionTest.php b/tests/Exception/ElementNotFoundExceptionTest.php index 8cb4e5d89..6faa55c53 100644 --- a/tests/Exception/ElementNotFoundExceptionTest.php +++ b/tests/Exception/ElementNotFoundExceptionTest.php @@ -11,11 +11,9 @@ class ElementNotFoundExceptionTest extends \PHPUnit_Framework_TestCase */ public function testBuildMessage($message, $type, $selector = null, $locator = null) { - $session = $this->getMockBuilder('Behat\Mink\Session') - ->disableOriginalConstructor() - ->getMock(); + $driver = $this->getMock('Behat\Mink\Driver\DriverInterface'); - $exception = new ElementNotFoundException($session, $type, $selector, $locator); + $exception = new ElementNotFoundException($driver, $type, $selector, $locator); $this->assertEquals($message, $exception->getMessage()); } @@ -31,4 +29,22 @@ public function provideExceptionMessage() array('Tag with name "foobar" not found.', null, 'name', 'foobar'), ); } + + /** + * @group legacy + */ + public function testConstructWithSession() + { + $driver = $this->getMock('Behat\Mink\Driver\DriverInterface'); + $session = $this->getMockBuilder('Behat\Mink\Session') + ->disableOriginalConstructor() + ->getMock(); + $session->expects($this->any()) + ->method('getDriver') + ->will($this->returnValue($driver)); + + $exception = new ElementNotFoundException($session); + + $this->assertEquals('Tag not found.', $exception->getMessage()); + } } diff --git a/tests/Exception/ElementTextExceptionTest.php b/tests/Exception/ElementTextExceptionTest.php index 73cef5bc3..f8d5e4c8e 100644 --- a/tests/Exception/ElementTextExceptionTest.php +++ b/tests/Exception/ElementTextExceptionTest.php @@ -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')); @@ -38,18 +34,11 @@ public function testExceptionToString() $expected = sprintf($expected.' ', get_class($driver)); - $exception = new ElementTextException('Text error', $session, $element); + $exception = new ElementTextException('Text 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') diff --git a/tests/Exception/ExpectationExceptionTest.php b/tests/Exception/ExpectationExceptionTest.php index 466bcca63..a327fcbd6 100644 --- a/tests/Exception/ExpectationExceptionTest.php +++ b/tests/Exception/ExpectationExceptionTest.php @@ -8,7 +8,7 @@ class ExpectationExceptionTest extends \PHPUnit_Framework_TestCase { public function testEmptyMessageAndPreviousException() { - $exception = new ExpectationException('', $this->getSessionMock(), new \Exception('Something failed')); + $exception = new ExpectationException('', $this->getMock('Behat\Mink\Driver\DriverInterface'), new \Exception('Something failed')); $this->assertEquals('Something failed', $exception->getMessage()); } @@ -16,24 +16,16 @@ public function testEmptyMessageAndPreviousException() public function testExceptionToString() { $driver = $this->getMock('Behat\Mink\Driver\DriverInterface'); - $page = $this->getPageMock(); - $session = $this->getSessionMock(); - $session->expects($this->any()) - ->method('getDriver') - ->will($this->returnValue($driver)); - $session->expects($this->any()) - ->method('getPage') - ->will($this->returnValue($page)); - $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')); $html = "
Test
\n"; - $page->expects($this->any()) + $driver->expects($this->any()) ->method('getContent') ->will($this->returnValue($html)); @@ -51,7 +43,7 @@ public function testExceptionToString() $expected = sprintf($expected.' ', get_class($driver)); - $exception = new ExpectationException('Expectation failure', $session); + $exception = new ExpectationException('Expectation failure', $driver); $this->assertEquals($expected, $exception->__toString()); } @@ -59,26 +51,18 @@ public function testExceptionToString() public function testBigContent() { $driver = $this->getMock('Behat\Mink\Driver\DriverInterface'); - $page = $this->getPageMock(); - $session = $this->getSessionMock(); - $session->expects($this->any()) - ->method('getDriver') - ->will($this->returnValue($driver)); - $session->expects($this->any()) - ->method('getPage') - ->will($this->returnValue($page)); - $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')); $body = str_repeat('a', 1001 - strlen('')); $html = sprintf("