Skip to content

Commit a76e403

Browse files
committed
Decoupled the DriverInterface from the NodeElement
This avoids a circular dependency. The driver interface now returns an array of XPath for found elements, and Element::find() instantiates the NodeElement for them. The driver is now the low level API which does not depend on any other part of Mink (but used by other parts). The ElementFinder is responsible for instantiating the NodeElement.
1 parent 25facce commit a76e403

10 files changed

Lines changed: 25 additions & 120 deletions

File tree

src/Driver/CoreDriver.php

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

1111
namespace Behat\Mink\Driver;
1212

13-
use Behat\Mink\Element\NodeElement;
1413
use Behat\Mink\Exception\UnsupportedDriverActionException;
1514
use Behat\Mink\KeyModifier;
16-
use Behat\Mink\Session;
1715

1816
/**
1917
* Core driver.
@@ -23,21 +21,6 @@
2321
*/
2422
abstract class CoreDriver implements DriverInterface
2523
{
26-
/**
27-
* @var Session
28-
*/
29-
private $session;
30-
31-
/**
32-
* @return void
33-
*
34-
* @final since 1.11
35-
*/
36-
public function setSession(Session $session)
37-
{
38-
$this->session = $session;
39-
}
40-
4124
/**
4225
* @return void
4326
*/
@@ -99,19 +82,13 @@ public function getContent()
9982
/**
10083
* @param string $xpath
10184
*
102-
* @return NodeElement[]
85+
* @return string[]
10386
*
10487
* @final since 1.11
10588
*/
10689
public function find($xpath)
10790
{
108-
$elements = array();
109-
110-
foreach ($this->findElementXpaths($xpath) as $xpath) {
111-
$elements[] = new NodeElement($xpath, $this, $this->session->getElementFinder());
112-
}
113-
114-
return $elements;
91+
return $this->findElementXpaths($xpath);
11592
}
11693

11794
/**

src/Driver/DriverInterface.php

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,9 @@
1010

1111
namespace Behat\Mink\Driver;
1212

13-
use Behat\Mink\Element\NodeElement;
1413
use Behat\Mink\Exception\DriverException;
1514
use Behat\Mink\Exception\UnsupportedDriverActionException;
1615
use Behat\Mink\KeyModifier;
17-
use Behat\Mink\Session;
1816

1917
/**
2018
* Driver interface.
@@ -23,15 +21,6 @@
2321
*/
2422
interface DriverInterface
2523
{
26-
/**
27-
* Sets driver's current session.
28-
*
29-
* @param Session $session
30-
*
31-
* @return void
32-
*/
33-
public function setSession(Session $session);
34-
3524
/**
3625
* Starts driver.
3726
*
@@ -299,7 +288,7 @@ public function getWindowName();
299288
*
300289
* @param string $xpath
301290
*
302-
* @return NodeElement[]
291+
* @return string[] An array of XPath queries for the found elements
303292
*
304293
* @throws UnsupportedDriverActionException When operation not supported by the driver
305294
* @throws DriverException When the operation cannot be done

src/Element/Element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public function find($selector, $locator)
9595
{
9696
$items = $this->findAll($selector, $locator);
9797

98-
return count($items) ? current($items) : null;
98+
return count($items) ? $items[0] : null;
9999
}
100100

101101
/**

src/Element/ElementFinder.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public function __construct(DriverInterface $driver, SelectorsHandler $selectors
4242
/**
4343
* @param string|array $locator
4444
*
45-
* @return NodeElement[]
45+
* @return list<NodeElement>
4646
*/
47-
public function findAll(string $selector, $locator, string $parentXpath)
47+
public function findAll(string $selector, $locator, string $parentXpath): array
4848
{
4949
if ('named' === $selector) {
5050
$items = $this->findAll('named_exact', $locator, $parentXpath);
@@ -58,7 +58,13 @@ public function findAll(string $selector, $locator, string $parentXpath)
5858
$xpath = $this->selectorsHandler->selectorToXpath($selector, $locator);
5959
$xpath = $this->xpathManipulator->prepend($xpath, $parentXpath);
6060

61-
return $this->driver->find($xpath);
61+
$elements = array();
62+
63+
foreach ($this->driver->find($xpath) as $elementXpath) {
64+
$elements[] = new NodeElement($elementXpath, $this->driver, $this);
65+
}
66+
67+
return $elements;
6268
}
6369

6470
/**

src/Element/NodeElement.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class NodeElement extends TraversableElement
3030
/**
3131
* Initializes node element.
3232
*
33-
* @param string $xpath element xpath
33+
* @param string $xpath element xpath
3434
* @param DriverInterface $driver
3535
* @param ElementFinder $elementFinder
3636
*/

src/Session.php

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@
1212

1313
use Behat\Mink\Driver\DriverInterface;
1414
use Behat\Mink\Element\ElementFinder;
15-
use Behat\Mink\Selector\SelectorsHandler;
1615
use Behat\Mink\Element\DocumentElement;
16+
use Behat\Mink\Selector\SelectorsHandler;
1717

1818
/**
1919
* Mink session.
@@ -30,10 +30,6 @@ class Session
3030
* @var DocumentElement
3131
*/
3232
private $page;
33-
/**
34-
* @var ElementFinder
35-
*/
36-
private $elementFinder;
3733

3834
/**
3935
* @param ElementFinder|SelectorsHandler|null $elementFinder
@@ -49,10 +45,7 @@ public function __construct(DriverInterface $driver, $elementFinder = null)
4945
}
5046

5147
$this->driver = $driver;
52-
$this->elementFinder = $elementFinder;
5348
$this->page = new DocumentElement($driver, $elementFinder);
54-
55-
$driver->setSession($this);
5649
}
5750

5851
/**
@@ -142,26 +135,6 @@ public function getPage()
142135
return $this->page;
143136
}
144137

145-
/**
146-
* @internal
147-
*/
148-
public function getElementFinder(): ElementFinder
149-
{
150-
return $this->elementFinder;
151-
}
152-
153-
/**
154-
* Returns selectors handler.
155-
*
156-
* @return SelectorsHandler
157-
*
158-
* @internal since 1.11
159-
*/
160-
public function getSelectorsHandler()
161-
{
162-
return $this->elementFinder->getSelectorsHandler();
163-
}
164-
165138
/**
166139
* Visit specified URL and automatically start session if not already running.
167140
*

tests/Driver/CoreDriverTest.php

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

33
namespace Behat\Mink\Tests\Driver;
44

5-
use Behat\Mink\Element\ElementFinder;
6-
use Behat\Mink\Element\NodeElement;
75
use PHPUnit\Framework\TestCase;
86

97
class CoreDriverTest extends TestCase
@@ -27,29 +25,19 @@ public function testCreateNodeElements()
2725
->setMethods(array('findElementXpaths'))
2826
->getMockForAbstractClass();
2927

30-
$session = $this->getMockBuilder('Behat\Mink\Session')
31-
->disableOriginalConstructor()
32-
->getMock();
33-
34-
$session->method('getElementFinder')->willReturn($this->createMock(ElementFinder::class));
35-
36-
$driver->setSession($session);
37-
3828
$driver->expects($this->once())
3929
->method('findElementXpaths')
4030
->with('xpath')
4131
->willReturn(array('xpath1', 'xpath2'));
4232

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

4635
$this->assertIsArray($elements);
4736

4837
$this->assertCount(2, $elements);
49-
$this->assertContainsOnlyInstancesOf('Behat\Mink\Element\NodeElement', $elements);
5038

51-
$this->assertSame('xpath1', $elements[0]->getXpath());
52-
$this->assertSame('xpath2', $elements[1]->getXpath());
39+
$this->assertSame('xpath1', $elements[0]);
40+
$this->assertSame('xpath2', $elements[1]);
5341
}
5442

5543
/**

tests/Element/ElementFinderTest.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,6 @@ public function testNotFound()
6363

6464
public function testFound()
6565
{
66-
$element1 = $this->createStub(NodeElement::class);
67-
$element1->method('getXpath')->willReturn('element1');
68-
69-
$element2 = $this->createStub(NodeElement::class);
70-
$element2->method('getXpath')->willReturn('element2');
71-
7266
$this->selectorsHandler->expects($this->once())
7367
->method('selectorToXpath')
7468
->with('css', 'h3 > a')
@@ -82,7 +76,7 @@ public function testFound()
8276
$this->driver->expects($this->once())
8377
->method('find')
8478
->with('full_xpath')
85-
->will($this->returnValue(array($element1, $element2)));
79+
->will($this->returnValue(array('element1', 'element2')));
8680

8781
$results = $this->finder->findAll('css', 'h3 > a', 'parent_xpath');
8882

@@ -94,12 +88,6 @@ public function testFound()
9488

9589
public function testNamedFound()
9690
{
97-
$element1 = $this->createStub(NodeElement::class);
98-
$element1->method('getXpath')->willReturn('element1');
99-
100-
$element2 = $this->createStub(NodeElement::class);
101-
$element2->method('getXpath')->willReturn('element2');
102-
10391
$this->selectorsHandler->expects($this->once())
10492
->method('selectorToXpath')
10593
->with('named_exact', 'test')
@@ -113,7 +101,7 @@ public function testNamedFound()
113101
$this->driver->expects($this->once())
114102
->method('find')
115103
->with('full_xpath')
116-
->will($this->returnValue(array($element1, $element2)));
104+
->will($this->returnValue(array('element1', 'element2')));
117105

118106
$results = $this->finder->findAll('named', 'test', 'parent_xpath');
119107

@@ -125,12 +113,6 @@ public function testNamedFound()
125113

126114
public function testNamedPartialFallback()
127115
{
128-
$element1 = $this->createStub(NodeElement::class);
129-
$element1->method('getXpath')->willReturn('element1');
130-
131-
$element2 = $this->createStub(NodeElement::class);
132-
$element2->method('getXpath')->willReturn('element2');
133-
134116
$this->selectorsHandler->expects($this->exactly(2))
135117
->method('selectorToXpath')
136118
->will($this->returnValueMap(array(
@@ -149,7 +131,7 @@ public function testNamedPartialFallback()
149131
->method('find')
150132
->willReturnMap(array(
151133
array('full_xpath', array()),
152-
array('full_partial_xpath', array($element1, $element2)),
134+
array('full_partial_xpath', array('element1', 'element2')),
153135
));
154136

155137
$results = $this->finder->findAll('named', 'test', 'parent_xpath');

tests/Element/NodeElementTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public function testElementIsValid()
5252
->expects($this->once())
5353
->method('find')
5454
->with($elementXpath)
55-
->willReturn(array($this->createStub(NodeElement::class)));
55+
->willReturn(array($elementXpath));
5656

5757
$this->assertTrue($node->isValid());
5858
}
@@ -78,7 +78,7 @@ public function testElementIsNotValidWithMultipleFound()
7878
->expects($this->once())
7979
->method('find')
8080
->with('some xpath')
81-
->willReturn(array($this->createStub(NodeElement::class), $this->createStub(NodeElement::class)));
81+
->willReturn(array('some xpath', 'some other xpath'));
8282

8383
$this->assertFalse($node->isValid(), 'more then 1 element found is invalid element');
8484
}

tests/SessionTest.php

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ class SessionTest extends TestCase
1414
* @var DriverInterface&MockObject
1515
*/
1616
private $driver;
17-
/**
18-
* @var SelectorsHandler
19-
*/
20-
private $selectorsHandler;
2117

2218
/**
2319
* Session.
@@ -32,8 +28,7 @@ class SessionTest extends TestCase
3228
protected function prepareSession(): void
3329
{
3430
$this->driver = $this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock();
35-
$this->selectorsHandler = $this->getMockBuilder('Behat\Mink\Selector\SelectorsHandler')->getMock();
36-
$this->session = new Session($this->driver, $this->selectorsHandler);
31+
$this->session = new Session($this->driver);
3732
}
3833

3934
public function testGetDriver()
@@ -46,16 +41,11 @@ public function testGetPage()
4641
$this->assertInstanceOf('Behat\Mink\Element\DocumentElement', $this->session->getPage());
4742
}
4843

49-
public function testGetSelectorsHandler()
50-
{
51-
$this->assertSame($this->selectorsHandler, $this->session->getSelectorsHandler());
52-
}
53-
5444
public function testInstantiateWithoutOptionalDeps()
5545
{
56-
$session = new Session($this->driver);
46+
new Session($this->driver);
5747

58-
$this->assertInstanceOf('Behat\Mink\Selector\SelectorsHandler', $session->getSelectorsHandler());
48+
$this->assertTrue(true, 'The instantiation should work.');
5949
}
6050

6151
public function testIsStarted()

0 commit comments

Comments
 (0)