How to mock request component


(Kebyang1511) #1

Im having some trouble or hard time mocking the request component.

The error that Im having is : Unable to determine the request URI.

What am I missing ?

Any help would be gladly appreaciated/.




<?php

namespace tests\codeception\unit\models;


use Yii;

use yii\codeception\TestCase;

use app\models\LoginForm;

use Codeception\Specify;


class UrlTest extends TestCase {

    use Specify;

    protected function setUp() {

        

    }


    protected function tearDown() {

        

    }


    // tests

    public function testValidation() {

        $urlobj = new \app\models\Url();

        $urlobj->url = 'http://www.biturl.test/asdfwe';

        $urlobj->shortcut = 'asdfw1';

        $this->assertTrue($urlobj->validate(), "Validate that using valid url  should pass.");

        $urlobj->url = 'http://localhost/';

        $urlobj->shortcut = 'asdfw1';

        $this->assertFalse($urlobj->validate(), "Validate that using invalid url  should pass.");

        $mockApplication = $this->mockApplication([

            'components' => [

                'request' => [

                    'url' => 'http://www.biturl.test'

                ]

            ]

        ]);

        Yii::$app= $mockApplication;

        $urlobj->url = 'http://www.biturl.test/asdfw1';

        $urlobj->shortcut = 'asdfw1';

        $this->assertFalse($urlobj->validate(), "Validate that using url  that has the same url with shortcode url.");

    }


}



I need to mock the request component because inside Url model

I used




$redirectShortcutUrl = str_replace(Yii::$app->request->url, "", Yii::$app->request->absoluteUrl).'/'.$this->shortcut;