Skip to content

Commit 22adbb0

Browse files
committed
Remove the dependency on the Session in expectation exceptions
Injecting the Session in exceptions is now deprecated in favor of injecting the driver.
1 parent d95155e commit 22adbb0

12 files changed

Lines changed: 125 additions & 130 deletions

src/Element/Element.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,9 +193,6 @@ public function getOuterHtml()
193193
/**
194194
* Builds an ElementNotFoundException
195195
*
196-
* This is an helper to build the ElementNotFoundException without
197-
* needing to use the deprecated getSession accessor in child classes.
198-
*
199196
* @param string $type
200197
* @param string|null $selector
201198
* @param string|null $locator
@@ -204,6 +201,6 @@ public function getOuterHtml()
204201
*/
205202
protected function elementNotFound($type, $selector = null, $locator = null)
206203
{
207-
return new ElementNotFoundException($this->session, $type, $selector, $locator);
204+
return new ElementNotFoundException($this->driver, $type, $selector, $locator);
208205
}
209206
}

src/Exception/ElementHtmlException.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Behat\Mink\Exception;
1212

13-
use Behat\Mink\Session;
13+
use Behat\Mink\Driver\DriverInterface;
1414
use Behat\Mink\Element\Element;
1515

1616
/**
@@ -30,16 +30,16 @@ class ElementHtmlException extends ExpectationException
3030
/**
3131
* Initializes exception.
3232
*
33-
* @param string $message optional message
34-
* @param Session $session session instance
35-
* @param Element $element element
36-
* @param \Exception $exception expectation exception
33+
* @param string $message optional message
34+
* @param DriverInterface $driver driver instance
35+
* @param Element $element element
36+
* @param \Exception $exception expectation exception
3737
*/
38-
public function __construct($message, Session $session, Element $element, \Exception $exception = null)
38+
public function __construct($message, DriverInterface $driver, Element $element, \Exception $exception = null)
3939
{
4040
$this->element = $element;
4141

42-
parent::__construct($message, $session, $exception);
42+
parent::__construct($message, $driver, $exception);
4343
}
4444

4545
protected function getContext()

src/Exception/ElementNotFoundException.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Behat\Mink\Exception;
1212

13+
use Behat\Mink\Driver\DriverInterface;
1314
use Behat\Mink\Session;
1415

1516
/**
@@ -22,12 +23,12 @@ class ElementNotFoundException extends ExpectationException
2223
/**
2324
* Initializes exception.
2425
*
25-
* @param Session $session session instance
26-
* @param string $type element type
27-
* @param string $selector element selector type
28-
* @param string $locator element locator
26+
* @param DriverInterface|Session $driver driver instance
27+
* @param string $type element type
28+
* @param string $selector element selector type
29+
* @param string $locator element locator
2930
*/
30-
public function __construct(Session $session, $type = null, $selector = null, $locator = null)
31+
public function __construct($driver, $type = null, $selector = null, $locator = null)
3132
{
3233
$message = '';
3334

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

4950
$message .= ' not found.';
5051

51-
parent::__construct($message, $session);
52+
parent::__construct($message, $driver);
5253
}
5354
}

src/Exception/ExpectationException.php

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
namespace Behat\Mink\Exception;
1212

13+
use Behat\Mink\Driver\DriverInterface;
1314
use Behat\Mink\Session;
1415

1516
/**
@@ -22,17 +23,28 @@
2223
class 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
}

src/Exception/ResponseTextException.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ class ResponseTextException extends ExpectationException
1919
{
2020
protected function getContext()
2121
{
22-
return $this->getSession()->getPage()->getText();
22+
return $this->getDriver()->getText('//html');
2323
}
2424
}

src/WebAssert.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ public function elementExists($selectorType, $selector, ElementInterface $contai
417417
$selector = implode(' ', $selector);
418418
}
419419

420-
throw new ElementNotFoundException($this->session, 'element', $selectorType, $selector);
420+
throw new ElementNotFoundException($this->session->getDriver(), 'element', $selectorType, $selector);
421421
}
422422

423423
return $node;
@@ -635,7 +635,7 @@ public function fieldExists($field, TraversableElement $container = null)
635635
$node = $container->findField($field);
636636

637637
if (null === $node) {
638-
throw new ElementNotFoundException($this->session, 'form field', 'id|name|label|value', $field);
638+
throw new ElementNotFoundException($this->session->getDriver(), 'form field', 'id|name|label|value', $field);
639639
}
640640

641641
return $node;
@@ -767,7 +767,7 @@ private function assert($condition, $message)
767767
return;
768768
}
769769

770-
throw new ExpectationException($message, $this->session);
770+
throw new ExpectationException($message, $this->session->getDriver());
771771
}
772772

773773
/**
@@ -784,7 +784,7 @@ private function assertResponseText($condition, $message)
784784
return;
785785
}
786786

787-
throw new ResponseTextException($message, $this->session);
787+
throw new ResponseTextException($message, $this->session->getDriver());
788788
}
789789

790790
/**
@@ -802,7 +802,7 @@ private function assertElement($condition, $message, Element $element)
802802
return;
803803
}
804804

805-
throw new ElementHtmlException($message, $this->session, $element);
805+
throw new ElementHtmlException($message, $this->session->getDriver(), $element);
806806
}
807807

808808
/**
@@ -820,7 +820,7 @@ private function assertElementText($condition, $message, Element $element)
820820
return;
821821
}
822822

823-
throw new ElementTextException($message, $this->session, $element);
823+
throw new ElementTextException($message, $this->session->getDriver(), $element);
824824
}
825825

826826
/**

tests/Exception/ElementHtmlExceptionTest.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ public function testExceptionToString()
1111
$driver = $this->getMock('Behat\Mink\Driver\DriverInterface');
1212
$element = $this->getElementMock();
1313

14-
$session = $this->getSessionMock();
15-
$session->expects($this->any())
16-
->method('getDriver')
17-
->will($this->returnValue($driver));
18-
$session->expects($this->any())
14+
$driver->expects($this->any())
1915
->method('getStatusCode')
2016
->will($this->returnValue(200));
21-
$session->expects($this->any())
17+
$driver->expects($this->any())
2218
->method('getCurrentUrl')
2319
->will($this->returnValue('http://localhost/test'));
2420

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

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

43-
$exception = new ElementHtmlException('Html error', $session, $element);
39+
$exception = new ElementHtmlException('Html error', $driver, $element);
4440

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

48-
private function getSessionMock()
49-
{
50-
return $this->getMockBuilder('Behat\Mink\Session')
51-
->disableOriginalConstructor()
52-
->getMock();
53-
}
54-
5544
private function getElementMock()
5645
{
5746
return $this->getMockBuilder('Behat\Mink\Element\NodeElement')

tests/Exception/ElementNotFoundExceptionTest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,9 @@ class ElementNotFoundExceptionTest extends \PHPUnit_Framework_TestCase
1111
*/
1212
public function testBuildMessage($message, $type, $selector = null, $locator = null)
1313
{
14-
$session = $this->getMockBuilder('Behat\Mink\Session')
15-
->disableOriginalConstructor()
16-
->getMock();
14+
$driver = $this->getMock('Behat\Mink\Driver\DriverInterface');
1715

18-
$exception = new ElementNotFoundException($session, $type, $selector, $locator);
16+
$exception = new ElementNotFoundException($driver, $type, $selector, $locator);
1917

2018
$this->assertEquals($message, $exception->getMessage());
2119
}
@@ -31,4 +29,22 @@ public function provideExceptionMessage()
3129
array('Tag with name "foobar" not found.', null, 'name', 'foobar'),
3230
);
3331
}
32+
33+
/**
34+
* @group legacy
35+
*/
36+
public function testConstructWithSession()
37+
{
38+
$driver = $this->getMock('Behat\Mink\Driver\DriverInterface');
39+
$session = $this->getMockBuilder('Behat\Mink\Session')
40+
->disableOriginalConstructor()
41+
->getMock();
42+
$session->expects($this->any())
43+
->method('getDriver')
44+
->will($this->returnValue($driver));
45+
46+
$exception = new ElementNotFoundException($session);
47+
48+
$this->assertEquals('Tag not found.', $exception->getMessage());
49+
}
3450
}

tests/Exception/ElementTextExceptionTest.php

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,10 @@ public function testExceptionToString()
1111
$driver = $this->getMock('Behat\Mink\Driver\DriverInterface');
1212
$element = $this->getElementMock();
1313

14-
$session = $this->getSessionMock();
15-
$session->expects($this->any())
16-
->method('getDriver')
17-
->will($this->returnValue($driver));
18-
$session->expects($this->any())
14+
$driver->expects($this->any())
1915
->method('getStatusCode')
2016
->will($this->returnValue(200));
21-
$session->expects($this->any())
17+
$driver->expects($this->any())
2218
->method('getCurrentUrl')
2319
->will($this->returnValue('http://localhost/test'));
2420

@@ -38,18 +34,11 @@ public function testExceptionToString()
3834

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

41-
$exception = new ElementTextException('Text error', $session, $element);
37+
$exception = new ElementTextException('Text error', $driver, $element);
4238

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

46-
private function getSessionMock()
47-
{
48-
return $this->getMockBuilder('Behat\Mink\Session')
49-
->disableOriginalConstructor()
50-
->getMock();
51-
}
52-
5342
private function getElementMock()
5443
{
5544
return $this->getMockBuilder('Behat\Mink\Element\NodeElement')

0 commit comments

Comments
 (0)