FIX Phockito-based tests to pass

This commit is contained in:
Hamish Friedlander 2013-07-25 13:27:15 +12:00
parent bb2e5624f3
commit d6cf16c391
2 changed files with 37 additions and 20 deletions

View File

@ -13,8 +13,7 @@
"silverstripe/framework": ">=3.1.0-beta1,<4.0" "silverstripe/framework": ">=3.1.0-beta1,<4.0"
}, },
"require-dev": { "require-dev": {
"hafriedlander/phockito": "*", "hafriedlander/silverstripe-phockito": "*"
"silverstripe/subsites": "*", },
"silverstripe/cms": ">=3.1.0-beta1,<4.0" "minimum-stability": "dev"
}
} }

View File

@ -4,11 +4,22 @@ class SolrIndexTest extends SapphireTest {
function setUpOnce() { function setUpOnce() {
parent::setUpOnce(); parent::setUpOnce();
Phockito::include_hamcrest(); if (class_exists('Phockito')) Phockito::include_hamcrest();
} }
function setUp() {
if (!class_exists('Phockito')) {
$this->markTestSkipped("These tests need the Phockito module installed to run");
$this->skipTest = true;
}
parent::setUp();
}
function testBoost() { function testBoost() {
$serviceMock = $this->getServiceMock(); $serviceMock = $this->getServiceMock();
Phockito::when($serviceMock)->search()->return($this->getFakeRawSolrResponse());
$index = new SolrIndexTest_FakeIndex(); $index = new SolrIndexTest_FakeIndex();
$index->setService($serviceMock); $index->setService($serviceMock);
@ -20,10 +31,7 @@ class SolrIndexTest extends SapphireTest {
); );
$index->search($query); $index->search($query);
Phockito::verify($serviceMock)->search( Phockito::verify($serviceMock)->search('+(Field1:term^1.5 OR HasOneObject_Field1:term^3)');
'+(Field1:term^1.5 OR HasOneObject_Field1:term^3)',
anything(), anything(), anything(), anything()
);
} }
function testIndexExcludesNullValues() { function testIndexExcludesNullValues() {
@ -85,24 +93,34 @@ class SolrIndexTest extends SapphireTest {
function testAddCopyField() { function testAddCopyField() {
$index = new SolrIndexTest_FakeIndex(); $index = new SolrIndexTest_FakeIndex();
$index->addCopyField('sourceField', 'destField'); $index->addCopyField('sourceField', 'destField');
$defs = simplexml_load_string('<fields>' . $index->getCopyFieldDefinitions() . '</fields>');
$lastDef = array_pop($defs);
$this->assertEquals('sourceField', $lastDef['source']); $defs = simplexml_load_string('<fields>' . $index->getCopyFieldDefinitions() . '</fields>');
$this->assertEquals('destField', $lastDef['dest']); $copyField = $defs->xpath('copyField');
$this->assertEquals('sourceField', $copyField[0]['source']);
$this->assertEquals('destField', $copyField[0]['dest']);
}
protected function getServiceMock() {
return Phockito::mock('SolrService');
} }
protected function getServiceSpy() { protected function getServiceSpy() {
$serviceSpy = Phockito::spy('SolrService'); $serviceSpy = Phockito::spy('SolrService');
$fakeResponse = new Apache_Solr_Response(new Apache_Solr_HttpTransport_Response(null, null, null)); Phockito::when($serviceSpy)->_sendRawPost()->return($this->getFakeRawSolrResponse());
Phockito::when($serviceMock) return $serviceSpy;
->_sendRawPost(anything(), anything(), anything(), anything())
->return($fakeResponse);
return $serviceMock;
} }
protected function getFakeRawSolrResponse() {
return new Apache_Solr_Response(
new Apache_Solr_HttpTransport_Response(
null,
null,
'{}'
)
);
}
} }
class SolrIndexTest_FakeIndex extends SolrIndex { class SolrIndexTest_FakeIndex extends SolrIndex {