forked from michalochman/SilverStripeExtension
-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathSilverStripeContextTest.php
102 lines (89 loc) · 3.3 KB
/
SilverStripeContextTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
<?php
namespace SilverStripe\BehatExtension\Tests;
use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Selector\SelectorsHandler;
use Behat\Mink\Session;
use Behat\Mink\Mink;
use Behat\Mink\Driver\DriverInterface;
use Behat\Mink\Element\Element;
use PHPUnit\Framework\MockObject\MockObject;
use SilverStripe\BehatExtension\Tests\SilverStripeContextTest\FeatureContext;
use SilverStripe\Dev\SapphireTest;
class SilverStripeContextTest extends SapphireTest
{
protected $backupGlobals = false;
protected bool $doSetSupportedModuleLocaleToUS = false;
public function testGetRegionObjThrowsExceptionOnUnknownSelector()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Cannot find 'region_map' in the behat.yml");
$context = $this->getContextMock();
$context->getRegionObj('.unknown');
}
public function testGetRegionObjThrowsExceptionOnUnknownRegion()
{
$this->expectException(\LogicException::class);
$this->expectExceptionMessage("Cannot find the specified region in the behat.yml");
$context = $this->getContextMock();
$context->setRegionMap(array('MyRegion' => '.my-region'));
$context->getRegionObj('.unknown');
}
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);
}
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);
}
/**
* @return FeatureContext
*/
protected function getContextMock()
{
$pageMock = $this->getMockBuilder(DocumentElement::class)
->disableOriginalConstructor()
->setMethods(array('find'))
->getMock();
$sessionMock = $this->getMockBuilder(Session::class)
->setConstructorArgs(array(
$this->getMockBuilder(DriverInterface::class)->getMock(),
$this->getMockBuilder(SelectorsHandler::class)->getMock()
))
->setMethods(array('getPage'))
->getMock();
$sessionMock->expects($this->any())
->method('getPage')
->will($this->returnValue($pageMock));
$mink = new Mink(array('default' => $sessionMock));
$mink->setDefaultSessionName('default');
$context = new FeatureContext(array());
$context->setMink($mink);
return $context;
}
/**
* @return Element|MockObject
*/
protected function getElementMock()
{
return $this->getMockBuilder(Element::class)
->disableOriginalConstructor()
->getMock();
}
}