K
K
Kirill Djonua2016-04-05 12:34:45
Yii
Kirill Djonua, 2016-04-05 12:34:45

How to create a stub for a controller method in codecption?

Hello,
I have a console controller, a method is called from its action. I need to test what data is passed to this method. The test is like this (thanks @onqu):

$appConfig = require('unit/config/config.php');
$this->mockApplication($appConfig);
$controller = new \namespace\controllers\MyController('id', \Yii::$app);
$result = $controller->run('my-action');
$this->assertEquals(1, $result);

I need to create a $controller Stub object now? Or is it done differently?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Igor Makarov, 2016-04-05
@kdjonua

In this case, mock is more convenient, but before that it is better to check that the method is being called at all. Stub is good for a complete replacement of something.

// аргументы конструктора MyController
$constructorArgs = [
    'id', 
    Yii::$app,
];

// заменяемые методы и свойства MyController
$methods = [
    // метод, который тестируем
    // подойдет любой тип callable
    'methodToTest' => function() {
        $args = func_get_args();
        
        ... тестируем аргументы
    },
];

$controllerMock = \Codeception\Util\Stub::construct(
    '\namespace\controllers\MyController',
    $constructorArgs,
    $methods
);
$controllerMock->run('action', []);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question