Skip to content

Commit

Permalink
Merge pull request #633 from stof/forward_compatibility
Browse files Browse the repository at this point in the history
Implement the conversion of XPath to NodeElement in CoreDriver
  • Loading branch information
stof committed Feb 4, 2015
2 parents f9b1497 + 97123e0 commit 1125c2f
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/Driver/CoreDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace Behat\Mink\Driver;

use Behat\Mink\Element\NodeElement;
use Behat\Mink\Exception\UnsupportedDriverActionException;
use Behat\Mink\Session;

Expand All @@ -21,12 +22,17 @@
*/
abstract class CoreDriver implements DriverInterface
{
/**
* @var Session
*/
private $session;

/**
* {@inheritdoc}
*/
public function setSession(Session $session)
{
throw new UnsupportedDriverActionException('Setting the session is not supported by %s', $this);
$this->session = $session;
}

/**
Expand Down Expand Up @@ -89,6 +95,28 @@ public function getContent()
* {@inheritdoc}
*/
public function find($xpath)
{
$elements = array();

foreach ($this->findElementXpaths($xpath) as $xpath) {
$elements[] = new NodeElement($xpath, $this->session);
}

return $elements;
}

/**
* Finds elements with specified XPath query.
*
* @see find()
*
* @param string $xpath
*
* @return string[] The XPath of the matched elements
*
* @throws UnsupportedDriverActionException When operation not supported by the driver
*/
protected function findElementXpaths($xpath)
{
throw new UnsupportedDriverActionException('Finding elements is not supported by %s', $this);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Driver/CoreDriverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Behat\Mink\Tests\Driver;

use Behat\Mink\Element\NodeElement;

class CoreDriverTest extends \PHPUnit_Framework_TestCase
{
public function testNoExtraMethods()
Expand All @@ -17,6 +19,34 @@ public function testNoExtraMethods()
}
}

public function testCreateNodeElements()
{
$driver = $this->getMockBuilder('Behat\Mink\Driver\CoreDriver')
->setMethods(array('findElementXpaths'))
->getMockForAbstractClass();

$session = $this->getMockBuilder('Behat\Mink\Session')
->disableOriginalConstructor()
->getMock();

$driver->setSession($session);

$driver->expects($this->once())
->method('findElementXpaths')
->with('xpath')
->willReturn(array('xpath1', 'xpath2'));

/** @var NodeElement[] $elements */
$elements = $driver->find('xpath');

$this->assertInternalType('array', $elements);
$this->assertCount(2, $elements);
$this->assertContainsOnlyInstancesOf('Behat\Mink\Element\NodeElement', $elements);

$this->assertSame('xpath1', $elements[0]->getXpath());
$this->assertSame('xpath2', $elements[1]->getXpath());
}

/**
* @dataProvider getDriverInterfaceMethods
*/
Expand All @@ -29,6 +59,10 @@ public function testInterfaceMethods(\ReflectionMethod $method)
sprintf('CoreDriver should implement a dummy %s method', $method->getName())
);

if ('setSession' === $method->getName()) {
return; // setSession is actually implemented, so we don't expect an exception here.
}

$driver = $this->getMockForAbstractClass('Behat\Mink\Driver\CoreDriver');

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

0 comments on commit 1125c2f

Please sign in to comment.