Yii 2.0 And Tests

Hi

I write a unit test, and when trying to use a class that is in my common/frontent/backend it fails.

Can you please help?

Thanks

Zvi.

Provide more info on your use-case and exceptions. Or you can wait for official integration for yii2-advanced, will be soon.

Can you please elaborate your problem and give examples?

Hello,

Don’t know if codeception integration is achieved, but I can’t make it work with basic template.

I just install the basic template, no modifications, install codeception with composer, make codecept build, then codecept run.

Here is the result :


Codeception PHP Testing Framework v1.8.3

Powered by PHPUnit 3.7.31 by Sebastian Bergmann.




                                                                                                                                                                 

  [Exception]                                                                                                                                                    

  DateTime::__construct(): It is not safe to rely on the system's timezone settings. You are *required* to use the date.timezone setting or the date_default_ti  

  mezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We s  

  elected the timezone 'UTC' for now, but please set date.timezone to select your timezone.                                                                      

                                                                                                                                                                 




run [-c|--config="..."] [--report] [--html] [--xml] [--tap] [--json] [--colors] [--no-colors] [--silent] [--steps] [-d|--debug] [--coverage] [--no-exit] [--defer-flush] [-g|--group="..."] [-s|--skip="..."] [--skip-group="..."] [--env="..."] [suite] [test]




Pretty sure you can fix that by just setting date.timezone in your php.ini or calling date_default_timezone_set in your entry script, mate (or anywhere before the first call to DateTime).

Yes, about timezone it is known issue, not codeception issue but php itself (https://github.com/Codeception/Codeception/issues/272). It is recommended that you set timezone in your script or in php.ini file, as for me i prefer php.ini setting to be set to my timezone explicitly.

Many thanks, unlocked my learning effort.

I had to solve further difficulties (may help some others) :

  • Had to change web server port in acceptance.suite.yml (localhost instead of localhost:8080).

  • Had to change rights in runtime directory because acceptance and functional tests were run by www-data user, when unit tests were run by me (some folders were own by me and some others by www-data).

But I still have one failure. Acceptance et unit tests are all right. But functional test on Login page fails on step 15 :


Sorry, I couldn't see "Incorrect username or password."

By hand, it works great, but automated test fails.

Any idea ?

>But I still have one failure. Acceptance et unit tests are all right. But functional test on Login page fails on step 15 :

not sure why it is so, current tests in travis build passing (https://travis-ci.org/yiisoft/yii2/builds/18947191). Have you updated your url as needed (and if needed) in main _bootstrap.php file? I’ve tested it right now and got all fine:




vendor/bin/codecept run functional,unit

Codeception PHP Testing Framework v1.8.3

Powered by PHPUnit 3.7.31-1-ga0e176b by Sebastian Bergmann.


Functional Tests (4) --------------------------------------------

Trying to ensure that about works (AboutCept.php)           Ok

Trying to ensure that contact works (ContactCept.php)       Ok

Trying to ensure that home page works (HomeCept.php)        Ok

Trying to ensure that login works (LoginCept.php)           Ok

-----------------------------------------------------------------


Unit Tests (4) ------------------------------------------------------------------------------------------

Trying to test contact (tests\unit\models\ContactFormTest::testContact)                             Ok

Trying to test login no user (tests\unit\models\LoginFormTest::testLoginNoUser)                     Ok

Trying to test login wrong password (tests\unit\models\LoginFormTest::testLoginWrongPassword)       Ok

Trying to test login correct (tests\unit\models\LoginFormTest::testLoginCorrect)                    Ok

---------------------------------------------------------------------------------------------------------




Time: 340 ms, Memory: 33.75Mb


OK (8 tests, 37 assertions)



>I had to solve further difficulties (may help some others) :

  • Had to change web server port in acceptance.suite.yml (localhost instead of localhost:8080).

  • Had to change rights in runtime directory because acceptance and functional tests were run by www-data user, when unit tests were run by me (some folders were own by me and some others by www-data).

well it is better to work in user home directory) However will write test guide when i’ll have time, currently working on codeception and yii2-advanced

I’m working in my home directory, but unit tests are executed through phpunit that is run by the user (jbcrouigneau in my case), when acceptance tests are executed through apache that is run by www-data user. So files and directories created during unit testing are owned by the user, when files or directories created during acceptance or functional tests are owned by www-data.

>when files or directories created during acceptance

yeah, right, i just use php builtin server to avoid all troubles) and nginx when needed))

>or functional tests are owned by www-data.

that cant be so, since you run it in your home directory as "vendor\bin\codecept run functional" this process will have your uid and all files will be created according your rights not www-data (ofcourse until you have added yourself to that group, but anyway i dont think it is so).

so have your problem with functional tests been solved? As i said i cant reproduce it, try this steps that i do, for example:

  1. cd

  2. php composer.phar create-project --prefer-dist --stability=dev yiisoft/yii2-app-basic basic;

  3. cd basic;

  4. cp ../composer.phar ./

  5. php composer.phar require --dev "codeception/codeception: 1.8.*@dev" "codeception/specify: *" "codeception/verify: *"

  6. vendor/bin/codecept build

  7. vendor/bin/codecept run unit,functional

step 7 should work out of the box without anything, because it is basic app;

  1. cd web

  2. php -S 127.0.0.1:8000

  3. adjust your acceptance suite to use 8000 port and set in main _bootstrap.php file url like "/index-test.php"

  4. vendor/bin/codecept run acceptance

This detailed steps should work for sure. However cant guess what is error that you have or already have not with your functional tests.

OK, it worked like a charm.

Can’t figure out why it didn’t work in my previous workspace.

I reinstalled all from scratch and it works.

Many thanks for the support.

Hi,

Sorry it took me time, but here are some more information

I use codeception for my tests.

My directory structure:

<project dir>

          /src (all my sources)  


          /tests (all my tests)  

Under the tests directory, I have _bootstrap.php that looks like:




<?php

// This is global bootstrap for autoloading 


// ensure we get report on all possible php errors

error_reporting(-1);


define('YII_ENABLE_ERROR_HANDLER', false);

define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;

$_SERVER['SCRIPT_FILENAME'] = __FILE__;


// require composer autoloader if available

$composerAutoload = __DIR__ . '/../src/vendor/autoload.php';

if (is_file($composerAutoload)) {

require_once($composerAutoload);

}

require_once(__DIR__ . '/../src/vendor/yiisoft/yii2/Yii.php');


Yii::setAlias('@test', __DIR__);



I have a file called tests\acceptance\SignupCept.php.

In case I try to do in this file something like:


$aActiveRecord = new ActiveRecord();

All is good.

In case I try to do something like


$aActiveRecord = new MyActiveRecord();

I fail because it can’t find the class (MyActiveRecord is one of my classes under the common directory)

Thanks for your help!

Zvi.

it cant find it because it is under "common" part of yii2-advanced (if you use this boilerplate), you forgot to include aliases file, for example see: https://github.com/yiisoft/yii2/blob/master/apps/advanced/environments/dev/frontend/web/index.php#L7. Not sure what boilerplate you are using, so just include alias file or where they are declared in your app.

Thanks for the fast answer,

I use yii2-advanced. I changed my _bootstrap.php to be like:




<?php

// This is global bootstrap for autoloading 


// ensure we get report on all possible php errors

error_reporting(-1);


define('YII_ENABLE_ERROR_HANDLER', false);

define('YII_DEBUG', true);

$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;

$_SERVER['SCRIPT_FILENAME'] = __FILE__;


// require composer autoloader if available

$composerAutoload = __DIR__ . '/../src/vendor/autoload.php';

if (is_file($composerAutoload)) {

require_once($composerAutoload);

}

require_once(__DIR__ . '/../src/vendor/yiisoft/yii2/Yii.php');

require_once(__DIR__ . '/../src/vendor/autoload.php');

require_once(__DIR__ . '/../src/common/config/aliases.php');


Yii::setAlias('@test', __DIR__);



And it works well.

When I try to do something that is related to DB it complains about the following:


Call to a member function getDb() on a non-object in yii2\db\ActiveRecord.php on line 52

I guess I need to configure something else?

Thanks

Zvi

yes, you have not created application instance. it should be created in needed suite bootstrap file. You can also wait a little, i will provide codeception for yii2-advanced today, almost finished it.

Thanks!

Still on basic template. I tried to enable coverage :

Added


coverage:

    enabled: true

in codeception.yml

Then run test : vendor/bin/codecept run --coverage --xml --html

Got this result :


Codeception PHP Testing Framework v1.8.3

Powered by PHPUnit 3.7.31 by Sebastian Bergmann.


Acceptance Tests (4) --------------------------------------------

Trying to ensure that about works (AboutCept.php)           Ok

Trying to ensure that contact works (ContactCept.php)       Ok

Trying to ensure that home page works (HomeCept.php)        Ok

Trying to ensure that login works (LoginCept.php)           Ok

-----------------------------------------------------------------




                                                                                                                                

  [yii\base\ErrorException]                                                                                                     

  file_get_contents(http://localhost/c3/report/serialized): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found  

                                                                                                                                

                                                                                                                                




run [-c|--config="..."] [--report] [--html] [--xml] [--tap] [--json] [--colors] [--no-colors] [--silent] [--steps] [-d|--debug] [--coverage] [--no-exit] [--defer-flush] [-g|--group="..."] [-s|--skip="..."] [--skip-group="..."] [--env="..."] [suite] [test]



Edit : to follow in http://www.yiiframework.com/forum/index.php/topic/51800-codeception-and-coverage/

not sure about it, it actually tries to get it by xdebug and other things, better ask it in codeception repo.

Done, yii2-advanced now has tests and integrated with codeception.

Hello.

How to install extensions for advansed apps?

Example in yii2-icons

Your requirements could not be resolved to an installable set of packages.

Problem 1

- The requested package kartik-v/yii2-icons could not be found in any version, there may be a typo in the package name.

For base apps works fine!