OntoWiki
OntoWiki copied to clipboard
How to test an extension: creating example model with data inside
How do i create an example model for each test (and remove it after the test)? The case is to check results from a couple of getXYZ functions of a controller. To be sure to have clear data, i want to use a prepared example data set.
I tried using OntoWiki_Test_ControllerTestCase, extend my own test class from it and set the following setUp function. But the model will not be created nor filled with data.
/**
* create and fill example cube
*/
$exampleCubeUri = "http://example.cubeviz.org/datacube/";
// create model and grant rights
$this->_storeAdapter->createModel($exampleCubeUri);
$this->_ac->setUserModelRight($exampleCubeUri, 'view', 'grant');
$this->_ac->setUserModelRight($exampleCubeUri, 'edit', 'grant');
$ttl = file_get_contents ( __DIR__ .'/../assets/exampleCube.ttl' );
// import given file content
$this->_storeAdapter->importRdf (
$exampleCubeUri, $ttl, 'ttl', Erfurt_Syntax_RdfParser::LOCATOR_DATASTRING
);
Is the instance of $this->_storeAdapter the same as in the controller i test now?
I would recommend you write a unit test. This will automatically set up the store adapter to be the test adapter. Please refer to the class in Erfurt to see how to use it. I think import of RDF files is not yet supported, but you can add statements. We either need to extend the class to also support import of RDF files or you need to import it yourself in your setUp method.
Import RDF files is already possible, i used a workaround. My CubevizController.php contains an action which is callable if extension is in development mode:
/**
*
*/
public function createexamplecubeAction()
{
$this->_helper->viewRenderer->setNoRender();
$this->_helper->layout->disableLayout();
if('development' === $this->_privateConfig->get('context')) {
try {
$exampleCubeNs = "http://example.cubeviz.org/datacube/";
$m = Erfurt_App::getInstance()->getStore()->getNewModel(
$exampleCubeNs, '', Erfurt_Store::MODEL_TYPE_OWL, false
);
// set file related stuff
$ttl = file_get_contents ( __DIR__ .'/assets/exampleCube.ttl' );
// import given file content
Erfurt_App::getInstance ()->getStore()->importRdf (
$exampleCubeNs, $ttl, 'ttl', Erfurt_Syntax_RdfParser::LOCATOR_DATASTRING
);
$code = 200;
$content = array(
'code' => $code,
'message' => 'Model was successfully created'
);
} catch (Exception $e) {
$code = 400;
$content = array(
'code' => $code,
'message' => $e->getMessage()
);
}
$this->_sendJSONResponse($content, $code);
}
}
Not elegant but works like a charm. As you wrote, you think this is not possible to realize in a Unit Test yet, did you?