Skip to content
This repository was archived by the owner on Sep 24, 2018. It is now read-only.

Latest commit

 

History

History
67 lines (50 loc) · 1.85 KB

usage-reconfiguring-components-for-testing.md

File metadata and controls

67 lines (50 loc) · 1.85 KB

Reconfiguring components for testing

You can reconfigure a component for testing, for this purpose in your setUp method of your test case you can do this for example:

<?php

use \yii\codeception\TestCase;
use Yii;

class MailTest extends TestCase
{

    protected function setUp()
    {
        // don't forget to call parent method that will setup Yii application
        parent::setUp();

        Yii::$app->mailer->fileTransportCallback = function ($mailer, $message) {
            return 'testing_message.eml';
        };
    }

}

Tip: You also can reconfigure Yii components and properties with method Yii::configure().

You don't need to worry about application instances and isolation because application will be created each time before any of test method will be executed in test case. You can mock application in a different way. For this purposes you have method mockApplication available in your test case. This method creates new application instance and replaces old one with it and is handy when you need to create application with a config that is different from other test methods in the current test suite. For example:

use \yii\codeception\TestCase;

class SomeMyTest extends TestCase
{

    public function testOne()
    {
        ...
    }

    public function testTwo()
    {
        $this->mockApplication([
            'language' => 'ru-RU',
            'components' => [
                'db' => [
                    //your custom configuration here
                ],
            ],
        ]);

        //your expectations and assertions goes here
    }

    public function testThree()
    {
        ...
    }

}