silverstripe-behat-extension/tests/Context/SilverStripeContextTest.php

91 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2015-04-07 13:15:22 +02:00
<?php
namespace SilverStripe\BehatExtension\Tests;
use SilverStripe\BehatExtension\Context\SilverStripeContext;
use Behat\Mink\Mink;
2015-04-07 13:15:22 +02:00
class SilverStripeContextTest extends \PHPUnit_Framework_TestCase
{
2015-04-07 13:15:22 +02:00
protected $backupGlobals = false;
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot find 'region_map' in the behat.yml
*/
public function testGetRegionObjThrowsExceptionOnUnknownSelector()
{
$context = $this->getContextMock();
$context->getRegionObj('.unknown');
}
2015-04-07 13:15:22 +02:00
/**
* @expectedException \LogicException
* @expectedExceptionMessage Cannot find the specified region in the behat.yml
*/
public function testGetRegionObjThrowsExceptionOnUnknownRegion()
{
$context = $this->getContextMock();
$context->setRegionMap(array('MyRegion' => '.my-region'));
$context->getRegionObj('.unknown');
}
2015-04-07 13:15:22 +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
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
protected function getContextMock()
{
$pageMock = $this->getMockBuilder('Behat\Mink\Element\DocumentElement')
->disableOriginalConstructor()
->setMethods(array('find'))
->getMock();
$sessionMock = $this->getMockBuilder('Behat\Mink\Session')
->setConstructorArgs(array(
2017-03-24 03:00:41 +01:00
$this->getMockBuilder('Behat\Mink\Driver\DriverInterface')->getMock(),
$this->getMockBuilder('Behat\Mink\Selector\SelectorsHandler')->getMock()
))
->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
$context = new SilverStripeContext(array());
$context->setMink($mink);
2015-04-07 13:15:22 +02:00
return $context;
}
2015-04-07 13:15:22 +02:00
protected function getElementMock()
{
return $this->getMockBuilder('Behat\Mink\Element\Element')
->disableOriginalConstructor()
->getMock();
}
}