2015-04-07 13:15:22 +02:00
|
|
|
<?php
|
2014-08-02 08:30:27 +02:00
|
|
|
|
2015-04-07 13:15:22 +02:00
|
|
|
namespace SilverStripe\BehatExtension\Tests;
|
|
|
|
|
2014-08-02 08:30:27 +02:00
|
|
|
use Behat\Mink\Element\DocumentElement;
|
|
|
|
use Behat\Mink\Selector\SelectorsHandler;
|
|
|
|
use Behat\Mink\Session;
|
2016-08-10 03:35:13 +02:00
|
|
|
use Behat\Mink\Mink;
|
2014-08-02 08:30:27 +02:00
|
|
|
use Behat\Mink\Driver\DriverInterface;
|
|
|
|
use Behat\Mink\Element\Element;
|
2021-10-27 06:14:44 +02:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2014-08-02 08:30:27 +02:00
|
|
|
use SilverStripe\BehatExtension\Tests\SilverStripeContextTest\FeatureContext;
|
2017-08-02 01:48:53 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2021-01-18 23:33:18 +01:00
|
|
|
class SilverStripeContextTest extends SapphireTest
|
2016-08-10 03:35:13 +02:00
|
|
|
{
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
protected $backupGlobals = false;
|
2016-02-24 10:24:07 +01:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function testGetRegionObjThrowsExceptionOnUnknownSelector()
|
|
|
|
{
|
2021-10-27 06:14:44 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage("Cannot find 'region_map' in the behat.yml");
|
2016-08-10 03:35:13 +02:00
|
|
|
$context = $this->getContextMock();
|
|
|
|
$context->getRegionObj('.unknown');
|
|
|
|
}
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function testGetRegionObjThrowsExceptionOnUnknownRegion()
|
|
|
|
{
|
2021-10-27 06:14:44 +02:00
|
|
|
$this->expectException(\LogicException::class);
|
|
|
|
$this->expectExceptionMessage("Cannot find the specified region in the behat.yml");
|
2016-08-10 03:35:13 +02:00
|
|
|
$context = $this->getContextMock();
|
|
|
|
$context->setRegionMap(array('MyRegion' => '.my-region'));
|
|
|
|
$context->getRegionObj('.unknown');
|
|
|
|
}
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function testGetRegionObjFindsBySelector()
|
|
|
|
{
|
|
|
|
$context = $this->getContextMock();
|
|
|
|
$context->getSession()->getPage()
|
|
|
|
->expects($this->any())
|
|
|
|
->method('find')
|
|
|
|
->will($this->returnValue($this->getElementMock()));
|
|
|
|
$obj = $context->getRegionObj('.some-selector');
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
}
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
public function testGetRegionObjFindsByRegion()
|
|
|
|
{
|
|
|
|
$context = $this->getContextMock();
|
|
|
|
$el = $this->getElementMock();
|
|
|
|
$context->getSession()->getPage()
|
|
|
|
->expects($this->any())
|
|
|
|
->method('find')
|
|
|
|
->will($this->returnCallback(function ($type, $selector) use ($el) {
|
|
|
|
return ($selector == '.my-region') ? $el : null;
|
|
|
|
}));
|
|
|
|
$context->setRegionMap(array('MyRegion' => '.my-asdf'));
|
|
|
|
$obj = $context->getRegionObj('.my-region');
|
|
|
|
$this->assertNotNull($obj);
|
|
|
|
}
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2014-08-02 08:30:27 +02:00
|
|
|
/**
|
|
|
|
* @return FeatureContext
|
|
|
|
*/
|
2016-08-10 03:35:13 +02:00
|
|
|
protected function getContextMock()
|
|
|
|
{
|
2014-08-02 08:30:27 +02:00
|
|
|
$pageMock = $this->getMockBuilder(DocumentElement::class)
|
2016-08-10 03:35:13 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods(array('find'))
|
|
|
|
->getMock();
|
2014-08-02 08:30:27 +02:00
|
|
|
$sessionMock = $this->getMockBuilder(Session::class)
|
2016-08-10 03:35:13 +02:00
|
|
|
->setConstructorArgs(array(
|
2014-08-02 08:30:27 +02:00
|
|
|
$this->getMockBuilder(DriverInterface::class)->getMock(),
|
|
|
|
$this->getMockBuilder(SelectorsHandler::class)->getMock()
|
2016-08-10 03:35:13 +02:00
|
|
|
))
|
|
|
|
->setMethods(array('getPage'))
|
|
|
|
->getMock();
|
|
|
|
$sessionMock->expects($this->any())
|
|
|
|
->method('getPage')
|
|
|
|
->will($this->returnValue($pageMock));
|
|
|
|
$mink = new Mink(array('default' => $sessionMock));
|
|
|
|
$mink->setDefaultSessionName('default');
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2014-08-02 08:30:27 +02:00
|
|
|
$context = new FeatureContext(array());
|
2016-08-10 03:35:13 +02:00
|
|
|
$context->setMink($mink);
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2016-08-10 03:35:13 +02:00
|
|
|
return $context;
|
|
|
|
}
|
2015-04-07 13:15:22 +02:00
|
|
|
|
2014-08-02 08:30:27 +02:00
|
|
|
/**
|
2021-10-27 06:14:44 +02:00
|
|
|
* @return Element|MockObject
|
2014-08-02 08:30:27 +02:00
|
|
|
*/
|
2016-08-10 03:35:13 +02:00
|
|
|
protected function getElementMock()
|
|
|
|
{
|
2014-08-02 08:30:27 +02:00
|
|
|
return $this->getMockBuilder(Element::class)
|
2016-08-10 03:35:13 +02:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
}
|
|
|
|
}
|