jeudi 26 février 2015

How to reuse tests in Yii2 Codeception

I am setting up new project based on Yii2 and Codeception. I am using advanced app template (backend, common, frontend).


I want most of frontend ActiveRecords to be "ReadOnly" so I made special Trait where I am blocking appropriate methods like save, insert, delete, update, ...



trait ReadOnlyActiveRecord {

/**
* @throws \yii\web\MethodNotAllowedHttpException
*/
public function save($runValidation = true, $attributeNames = null)
{
return self::throwNotAllowedException(__FUNCTION__);
}

//...


It simply throws an MethodNotAllowedHttpException.


Now I use this Trait in multiple frontend ARs and want to test them using Codeception like following



use \Codeception\Specify;

/**
* @expectedException \yii\web\MethodNotAllowedHttpException
*/
public function testSaveMethod()
{
$model = new AppLanguage();

$this->specify('model should be unsaveable', function () use ($model) {

expect('save function is not allowed', $model->save())->false();
});
}

/**
* @expectedException \yii\web\MethodNotAllowedHttpException
*/
public function testInsertMethod()
{
$model = new AppLanguage();

$this->specify('model should be unisertable', function () use ($model) {

expect('insert function is not allowed', $model->save())->false();
});
}

// ...


I now I am figuring out how to use these tests in multiple TestCests so I won't rewrite the code again and again in each of Cest.


So I am thinking about something like



/**
* Tests ActiveRecord is read only
*/
public function testReadOnly()
{
$model = new AppLanguage();

$this->processReadOnlyTests($model);
}


So my question is:


Where to put the test methods and how to include and call them in specific Cests?


Any suggestions?


Thank you.


Aucun commentaire:

Enregistrer un commentaire