Stefano Mtangoo
Stefano Mtangoo

Reputation: 6534

Run Codeception API Test with Yii2

I have been fighting with this problem for hours and cannot get through. I want to run API tests with Yii2 and (of course) Codeception. Here is my api.suite.yml

class_name: ApiTester
modules:
    enabled:
        - REST:
            url: /mobile
            depends: Yii2
            part: Json
        - \Helper\Api
    config:
        Yii2:
            entryUrl: http://localhost:8080/index-test.php

and my test file UserLoginCept.php

<?php 
$I = new ApiTester($scenario);
$I->wantTo('Test User Login');
$I->sendPOST('mobile/login', ['username' => 'uname', 'password' => '123456']);
$I->seeResponseCodeIs(\Codeception\Util\HttpCode::OK);
$I->seeResponseContainsJson(['success'=>true]);

Results are logged below. The problem is the Test is calling site/index which is in the root project not mobile module. I can sense that it is picking wrong URL somewhere as I cannot see any trace of the module being called. If I try URL on Browser it works fine http://localhost:8080/index.php/mobile/api/login

{

    "success": false,
    "token": ""

}

can someone help me spot what am doing wrong? I have read as much as I could could not find the issue.

Codeception Results

$~ codecept --debug run api
Codeception PHP Testing Framework v2.2.10
Powered by PHPUnit 4.8.35 by Sebastian Bergmann and contributors.

  Rebuilding ApiTester...

Api Tests (1) -----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: REST, Yii2, \Helper\Api
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
UserLoginCept: Test User Login
Signature: UserLoginCept
Test: tests/api/UserLoginCept.php
Scenario --
 I send post "/mobile/api/login",{"username":"uname","password":"123456"}
  [Request] POST /mobile/mobile/api/login {"username":"uname","password":"123456"}
  [Request Headers] []
  [yii\db\Connection::open] 'Opening DB connection: mysql:host=localhost;dbname=database_name'
 ERROR 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
1) UserLoginCept: Test user login
 Test  tests/api/UserLoginCept.php

  [Error] Call to a member function isAdmin() on null  


Scenario Steps:

 1. $I->sendPOST("/mobile/api/login",{"username":"uname","password":"123456"}) at tests/api/UserLoginCept.php:4

#1  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:328
#2  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/View.php:250
#3  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:396
#4  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:382
#5  /Users/hosanna/Projects/Volcano/WebApp/controllers/SiteController.php:74
#6  app\controllers\SiteController->actionIndex
#7  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/InlineAction.php:57
#8  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Controller.php:156
#9  /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/base/Module.php:523
#10 /Users/hosanna/Projects/Volcano/WebApp/vendor/yiisoft/yii2/web/Application.php:102
<!DOCTYPE html>
<html lang="en-US">
..... rest of HTML.....

Upvotes: 2

Views: 2526

Answers (1)

Stefano Mtangoo
Stefano Mtangoo

Reputation: 6534

So here is how I solved it: changed suite.api.yaml to use test-index.php

class_name: ApiTester
modules:
    enabled:
        - Yii2 
        - REST:
            url: http://localhost:8080/index-test.php/mobile/
            depends: Yii2
            part: Json
            configFile: 'config/test.php'
        - \Helper\Api
    config:
        Yii2:

I then changed the config file referred by text-index (config/test.php) to include pretty URLs:

<?php
$params = require(__DIR__ . '/params.php');
$dbParams = require(__DIR__ . '/test_db.php');

/**
 * Application configuration shared by all test types
 */
return [
    'id' => 'basic-tests',
    'basePath' => dirname(__DIR__),    
    'language' => 'en-US',
    'modules' => [
        'mobile' => [
            'class' => 'app\modules\mobile\Module',
        ],
     ],
    'components' => [
        'db' => $dbParams,
        'mailer' => [
            'useFileTransport' => true,
        ],
        'assetManager' => [            
            'basePath' => __DIR__ . '/../web/assets',
        ],
        'urlManager' => [
            'enablePrettyUrl' => true,
            'enableStrictParsing' => false,
            'showScriptName' => true,
            'rules' => [
                ['class' => 'yii\rest\UrlRule', 'controller' => 'mobile/api'],
            ],
        ],
        'user' => [
            'identityClass' => 'app\modules\mobile\models\User',
        ],        
        'request' => [
            'cookieValidationKey' => 'test',
            'enableCsrfValidation' => false,
            // but if you absolutely need it set cookie domain to localhost
            /*
            'csrfCookie' => [
                'domain' => 'localhost',
            ],
            */
        ],        
    ],
    'params' => $params,
];

After that tests were running fine!

Upvotes: 1

Related Questions