Run Codeception API Test with Yii2


(Stefano Mtangoo) #1

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:808…obile/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:




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.....




Also asked in SO

TIA