Skip to content

Commit 97123e0

Browse files
committed
Implement the conversion of XPath to NodeElement in CoreDriver
The goal is to ease the development of Mink 2.0 by allowing drivers to be compatible with both Mink 1.7 and 2.0 on a single code base. The only change required for drivers is now handled at the level of the CoreDriver shipped in Mink when drivers should to override findElementXpaths rather than find.
1 parent 87a18d7 commit 97123e0

2 files changed

Lines changed: 63 additions & 1 deletion

File tree

src/Driver/CoreDriver.php

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

1111
namespace Behat\Mink\Driver;
1212

13+
use Behat\Mink\Element\NodeElement;
1314
use Behat\Mink\Exception\UnsupportedDriverActionException;
1415
use Behat\Mink\Session;
1516

@@ -21,12 +22,17 @@
2122
*/
2223
abstract class CoreDriver implements DriverInterface
2324
{
25+
/**
26+
* @var Session
27+
*/
28+
private $session;
29+
2430
/**
2531
* {@inheritdoc}
2632
*/
2733
public function setSession(Session $session)
2834
{
29-
throw new UnsupportedDriverActionException('Setting the session is not supported by %s', $this);
35+
$this->session = $session;
3036
}
3137

3238
/**
@@ -89,6 +95,28 @@ public function getContent()
8995
* {@inheritdoc}
9096
*/
9197
public function find($xpath)
98+
{
99+
$elements = array();
100+
101+
foreach ($this->findElementXpaths($xpath) as $xpath) {
102+
$elements[] = new NodeElement($xpath, $this->session);
103+
}
104+
105+
return $elements;
106+
}
107+
108+
/**
109+
* Finds elements with specified XPath query.
110+
*
111+
* @see find()
112+
*
113+
* @param string $xpath
114+
*
115+
* @return string[] The XPath of the matched elements
116+
*
117+
* @throws UnsupportedDriverActionException When operation not supported by the driver
118+
*/
119+
protected function findElementXpaths($xpath)
92120
{
93121
throw new UnsupportedDriverActionException('Finding elements is not supported by %s', $this);
94122
}

tests/Driver/CoreDriverTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
namespace Behat\Mink\Tests\Driver;
44

5+
use Behat\Mink\Element\NodeElement;
6+
57
class CoreDriverTest extends \PHPUnit_Framework_TestCase
68
{
79
public function testNoExtraMethods()
@@ -17,6 +19,34 @@ public function testNoExtraMethods()
1719
}
1820
}
1921

22+
public function testCreateNodeElements()
23+
{
24+
$driver = $this->getMockBuilder('Behat\Mink\Driver\CoreDriver')
25+
->setMethods(array('findElementXpaths'))
26+
->getMockForAbstractClass();
27+
28+
$session = $this->getMockBuilder('Behat\Mink\Session')
29+
->disableOriginalConstructor()
30+
->getMock();
31+
32+
$driver->setSession($session);
33+
34+
$driver->expects($this->once())
35+
->method('findElementXpaths')
36+
->with('xpath')
37+
->willReturn(array('xpath1', 'xpath2'));
38+
39+
/** @var NodeElement[] $elements */
40+
$elements = $driver->find('xpath');
41+
42+
$this->assertInternalType('array', $elements);
43+
$this->assertCount(2, $elements);
44+
$this->assertContainsOnlyInstancesOf('Behat\Mink\Element\NodeElement', $elements);
45+
46+
$this->assertSame('xpath1', $elements[0]->getXpath());
47+
$this->assertSame('xpath2', $elements[1]->getXpath());
48+
}
49+
2050
/**
2151
* @dataProvider getDriverInterfaceMethods
2252
*/
@@ -29,6 +59,10 @@ public function testInterfaceMethods(\ReflectionMethod $method)
2959
sprintf('CoreDriver should implement a dummy %s method', $method->getName())
3060
);
3161

62+
if ('setSession' === $method->getName()) {
63+
return; // setSession is actually implemented, so we don't expect an exception here.
64+
}
65+
3266
$driver = $this->getMockForAbstractClass('Behat\Mink\Driver\CoreDriver');
3367

3468
$this->setExpectedException('Behat\Mink\Exception\UnsupportedDriverActionException');

0 commit comments

Comments
 (0)