mercredi 14 novembre 2018

Unit Testing Activerecord model

Good day. I have a controller method for yii 1. I want to write a unit test for it on phpunit. When testing the method, I do not want the request dataset to be saved to the database through the RainbowVideoWeb model. RainbowVideoWeb -- ActiveRecord model. I do not understand how I can replace the RainbowVideoWeb model with something that does not interact with the database. I need help

    public function actionIndex()
{
    try {
        $this->layout = '';
        header('Content-type: application/json');
        Yii::log("RAINBOW: " . serialize(Yii::app()->request->getRestParams()));

        $ticketVariant = Yii::app()->request->getPost('ticketVariant');
        $barcode = Yii::app()->request->getPost('ticketCode');
        $stream = Yii::app()->request->getPost('stream');
        $downloadMp4 = Yii::app()->request->getPost('downloadMp4');
        $downloadAvi = Yii::app()->request->getPost('downloadAvi');
        $requestId = Yii::app()->request->getPost('id');

        $rainbowVideo = new RainbowVideoWeb;
        $rainbowVideo->created_at = null;
        $rainbowVideo->ticket_variant = $ticketVariant;
        $rainbowVideo->barcode = $barcode;
        $rainbowVideo->stream = $stream;
        $rainbowVideo->download_mp4 = $downloadMp4;
        $rainbowVideo->download_avi = $downloadAvi;
        $rainbowVideo->request_id = $requestId;

        if ($rainbowVideo->save()) {
            echo json_encode(['status' => 'ok']);
        } else {
            $errors = $rainbowVideo->getErrors();
            throw new Exception('Validation Errors. ' . print_r($errors, true));
        }

        Yii::app()->end();

    } catch (Exception $e) {
        Yii::log("RAINBOW error. " . $e->getMessage(), CLogger::LEVEL_ERROR);
        echo json_encode(['status' => 'error']);
    }
}

Aucun commentaire:

Enregistrer un commentaire