Как тестировать модели с BlameableBehavior
Как тестировать модели с BlameableBehavior
Как писать unit test для модели у которого подключен BlameableBehavior?
Re: Как тестировать модели с BlameableBehavior
Интеграционный:damasco писал(а):Как писать unit test для модели у которого подключен BlameableBehavior?
Код: Выделить всё
public function testBlameable()
{
Yii::$app->user->login(new User(['id' => 5]));
$post = new Post();
$post->save();
$this->assertEquals(5, $post->author_id);
}
Код: Выделить всё
public function testBlameable()
{
$user = $this->getMock('yii\web\User', ['getIsGuest', 'getId'])
$user->method('getIsGuest')->willReturn(false);
$user->method('getId')->willReturn(5);
Yii::$app->set('user', $user);
$post = $this->getMockBuilder('\app\model\Post') ->setMethods(['attributes']) ->getMock();
$post->method('attributes')->willReturn(['id', 'title', 'author_id', 'text']);
$post->beforeSave(true);
$this->assertEquals(5, $post->author_id);
}
Код: Выделить всё
public function testBlameable()
{
$user = $this->getMock('yii\web\User', ['getIsGuest', 'getId'])
$user->method('getIsGuest')->willReturn(false);
$user->method('getId')->willReturn(5);
Yii::$app->set('user', $user);
$schema = $this->getMock('yii\db\Schema', ['insert', 'getTableSchema']);
$schema->method('insert')->willReturn(['id' => 1]);
$schema->method('getTableSchema')->willReturn(new yii\db\TableSchema([
'columns' => [
'id' => new yii\db\ColumnSchema([
'name' => 'id',
'type' => 'integer',
'phpType' => 'integer',
'dbType' => 'int(11)',
'size' => 11,
'precision' => 11,
'isPrimaryKey' => 1,
'autoIncrement' => 1,
]),
'author_id' => new yii\db\ColumnSchema([
'name' => 'id',
'type' => 'integer',
'phpType' => 'integer',
'dbType' => 'int(11)',
'size' => 11,
'precision' => 11,
'isPrimaryKey' => 0,
'autoIncrement' => 0,
]),
],
]);
$db = $this->getMock('yii\db\Connection', ['getSchema']);
$db->method('getSchema')->willReturn($schema);
Yii::$app->set('db', $db);
$post = new Post();
$post->save(false);
$this->assertEquals(5, $post->author_id);
}
Re: Как тестировать модели с BlameableBehavior
Спасибо, за развернутый ответ.