Improved test behaviour (too much mocking)

This commit is contained in:
Ingo Schommer 2015-04-08 08:08:35 +12:00
parent cc6aa5cd46
commit b28b179df7

View File

@ -27,15 +27,24 @@ class SilverStripeContextTest extends \PHPUnit_Framework_TestCase {
public function testGetRegionObjFindsBySelector() { public function testGetRegionObjFindsBySelector() {
$context = $this->getContextMock(); $context = $this->getContextMock();
$context = $this->returnsElement($context); $context->getSession()->getPage()
->expects($this->any())
->method('find')
->will($this->returnValue($this->getElementMock()));
$obj = $context->getRegionObj('.some-selector'); $obj = $context->getRegionObj('.some-selector');
$this->assertNotNull($obj); $this->assertNotNull($obj);
} }
public function testGetRegionObjFindsByRegion() { public function testGetRegionObjFindsByRegion() {
$context = $this->getContextMock(); $context = $this->getContextMock();
$context = $this->returnsElement($context); $el = $this->getElementMock();
$context->setRegionMap(array('MyRegion' => '.my-region')); $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'); $obj = $context->getRegionObj('.my-region');
$this->assertNotNull($obj); $this->assertNotNull($obj);
} }
@ -64,15 +73,9 @@ class SilverStripeContextTest extends \PHPUnit_Framework_TestCase {
return $context; return $context;
} }
protected function returnsElement($context) { protected function getElementMock() {
$el = $this->getMockBuilder('Behat\Mink\Element\Element') return $this->getMockBuilder('Behat\Mink\Element\Element')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$context->getSession()->getPage()
->expects($this->any())
->method('find')
->will($this->returnValue($el));
return $context;
} }
} }