Yii Codeception Module

I cant find yii module or something else for the codeception, so is there any module, or i need to write it by myself( ?

Guys, i’ve created basic integration with codeception(BDD framework)https://github.com/Codeception/Codeception, http://codeception.com/ for Yii, in this way u can now run your functional tests without selenium (or with it, codeception also has module for selenium1 and selenium2) and they can be faster. So you are very welcome to test and use it. Please see this PR (https://github.com/Codeception/Codeception/pull/115).

Guys, module was included in official release, so u can try it and make some PR or issues )

http://codeception.com/02-08-2013/codeception-1-5-3.html

Hi, that’s nice that you interested in integration codeception to yii. Codeception is looking very nice and promising. I also decided to play/try it but have some problems.

First of all what is

public function setHeaders()

{

$this->_headers = Yii::app()->request->getAllHeaders();

}

Does CHttpRequest have getAllHeaders ?

And what about unit tests? In order to load yii enviroment I need to create Yii application again in _unit/bootstrap and in this case I got exception from YiiBase

throw new CException(Yii::t(‘yii’,‘Yii application can only be created once.’));

So for now I have to override base CWebApplication class by mine and check if Yii::app() then do not initialize it again.

In the Yii1 module of codeception the are some guide about this, all u need is to replace your http-request component with one from the plugins\framworks\yii\web\ in codeception directory. So u do not need to create Yii application in _bootstrap file, all you need is to include in yaml configuration file Yii1 module, please see docs here for more information https://github.com/Codeception/Codeception/blob/master/src/Codeception/Module/Yii1.php

Woh… spent a few hours to figure out how it works and make to work your module.

Look. At first in order to use your module for functional and unit tests I added it in functional.suite and unit.suite config files

modules:

enabled: […, Yii1]

So therefore I initialize and include Codeception\Module\Yii1 2 times.

$this->appSettings = require_once($this->config[‘appPath’]); //get application settings in the entry script

$this->_appConfig = require_once($this->appSettings[‘config’]);

when I include it next time require_once return null and it didn’t work. Also I need to add Yii::setApplication(null); as you do in Codeception\Util\Connector\Yii1




public function _initialize()

	{

		$this->appSettings = require($this->config['appPath']); //get application settings in the entry script

		$this->_appConfig = require($this->appSettings['config']);


		$_SERVER['SCRIPT_NAME'] = str_replace('http://localhost','',$this->config['url']);

		$_SERVER['SCRIPT_FILENAME'] = $this->config['appPath'];


		Yii::setApplication(null);

		Yii::createApplication($this->appSettings['class'],$this->_appConfig);

	}



Now as for Codeception\Util\Connector\Yii1 yep when I changed request class it helped. But still required additional changes. Look inside

public function doRequest($request)

For instance I have uri http://mydomain.local/post/3

First of all

$uri = str_replace(‘http://localhost’,’’,$request->getUri());

$scriptName = str_replace(‘http://localhost’,’’,$this->url);

What is localhost, I bet this is not correct or useless. I briefly scan the code perhaps localhost was set in

Symfony\Component\BrowserKit\Client as default. But you have to config it somehow. And definitely localhost has no relation to scriptname. But ok you can change config value and this will work.

But take a look in

if (strpos($uri,$scriptName) === false)

$uri = $scriptName.$queryString;

this is incorrect.

At this point for my example uri I have $uri = ‘post/3’ and $scriptName = ‘index-test.php’ and finally for all pages my $uri = ‘index-test.php’

Please review my changes and update module.

Thanks for comment, will review your issue and make some PR, btw did not get it, what is the problem with 2 different suites? i try to reproduce it and everything works fine. Btw. hack with $uri is for not path url-manager, so will also think how to solve it, because it must be done before application is created. Created issue on codeception, if u want can continue there https://github.com/Codeception/Codeception/issues/182

Well guys there were some improvements with Yii1 module in codeception like correct handling unit-tests from the “out-of-the-box” and other important fixes that were made by Yii github users. We really appreciate your help, so test it, use it and feel free to create issues about bugs or questions :)

Codeception 1.5.7 is out, there are some changes in Yii1 module too. Still have some issues marked by “yii” label opened, but i think if there will be more yii users on codeception repo we will solve them. Again thanks all users that collaborate and work on yii and codeception integration :)

So, guys, 1.6.0 is out, thanks to contributors and DavertMik(core-developer), there are some interesting things like interactive console and other, but anyway, the one thing that must be noticed here because of Yii1 module integration is an additional option for running tests, its called --defer-flush (see this PR https://github.com/Codeception/Codeception/pull/245), so because of some problems that was in Yii integration (like headers, sessions, etc), you can use this option to be sure that no output will be before some important function usage. So for IndexCept.php (with scenario from Yii1 module) it will be like this:

>php codecept.phar --steps --defer-flush run functional IndexCept.php

Also feel free to ask some questions or submit bug-issues or PR in codeception repo about Yii1 module, your help is really appreciated).

Ciao Ragazzo,

first, thanks for your work and excuse me if I start asking silly or provoking questions ;) … also I am not very experienced with testing.

I got similar problems like radzserg when using your module. Why did you change the index-test.php file? Like returning an array instead of creating the webapp.

Wouldn’t it be better to specify the application class and config in the Yii1 module configuration and then initialize the app with these settings in the module?

And why do you need the module at all, what are your use-cases? - I asking just because it runs fine for me without the module. But I had to include the PhpBrowser module for my functional tests, is this the “slow” stuff? I still don’t fully understand et the difference between acceptance and functional tests.

Best regards,

schmunk

PS: On the codeception page it states, that Codeception is the first testing suite for Yii, that’s not really true, Yii always ships with a test folder and corresponding configs.

I am not from Italia, hm)

me too…)

Well, main reason was that module must be easy-install with little config params, and some developers also has different settings in "index.php" entry scripts, so if just put only config and app class in Yii1 module config it will be wrong because some sttings in entry scripts will not be applied.

yes, correct, module was designed and written about 3 months ago so in that time functional tests were tests that can be run without server only through symfony browser-kit, codeception works fine through borwser-kit with symfony2 integrations as i know and with Kohana/Zend integrations too, so module for Yii1 was created with those reasons.

right, i also use PhpBrowser for functional tests, it is not so fast as use only integration with browser-kit, but it is faster than selenium tests. So you are correct here to use it for functional tests.

well the main difference is that acceptance tests do not know anything about application inner code (if we talking about functional tests you can use for them also some modules like FileSystem). So the main difference on testing web-pages is that selenium tests are very slow, and phpbrowser test are much faster, thats why you prefer phpbrowser for functional tests to cover some basic functionality, and some functionality that not covered by functional tests (with phpbrowser)will be covered with acceptance tests (with Selenium2). As for me my functional tests (lets say 10 of them, differen tests, some of them of course CRUD-tests) can run in 50 sec. when acceptance tests will take a 4-5 minute at least.

BTW. the difference beetwen new Cests-forman and Cepts is that Cests are more like phpunit-selenium tests i think, in Cests you have _before/_after/_failed methods to manage your fixtures or smth. else, and cepts are only flat-files scenarios, so when u test some CRUD-functionality you can write 1 long cept, or you can use new Cest format, with same verbose level and user (TestGuy\WebGuy) and split each of (CRUD) in separate method.

Looks like some misunderstanding, ofcourse Yii has out-of-the-box support for phpunit tests, can you please point where is this written will change it of course.

Thanks for your answers!

About the statement, it’s here:

http://codeception.com/docs/05-FunctionalTests#Yii

It is totally not clear what you need to do to use this with Yii.

Not to me, anyway. :)

So, I downloaded codeception.phar into the root of my yii application.

Now what?

functional.suite.yml - I have to create that myself?

In the root folder?

I don’t quite get the thing about [i]You need to use CodeceptionHttpRequest from plugins directory (plugins\frameworks\yii\web),

[/i]…

Do I tell Yii about this in what config? and how?

test.php in protected/config?

I will do some more testing tomorrow when I am less dense, but I think the docs could be greatly improved.

Also, remember: Phundament is Schmunk’s framework, it’s not vanilla Yii, so any guides from it is rather irrelevant (but maybe helpful).

I see elsewhere that Schmunk put the phar in protected/vendor/codeception and ran this:


vendor/bin/codecept bootstrap tests/codeception

I will do that tomorrow. ;)

I ran


php vendors/codecept.phar bootstrap

in my protected directory.

I guess I should continue the guide for the Yii module from here.

Point the CHttpRequest to CodeceptionHttpRequest in protected/config/test.php, right?

After adding the path alias to the codeception yii module, of course.

I still think the docs assumes too much on behalf of the user. ;)

So, I edit the yml like this:




class_name: TestGuy

modules:

    enabled: [Yii1, TestHelper]

    config:

        Yii1:

            appPath: '/path/to/index-test.php'

            url: 'http://localhost/path/to/index-test.php'



Where, of course, the config points to config/test.php where I tell Yii to use the Codeception HttpRequest component.

So, then I run codeception from the protected folder.

That sounds like a deal.

I’ll do that tomorrow - I am wasted now. :lol:

<edit>

Should I put it in a separate ‘codeception’ folder in tests?

I see elsewhere that for unit tests, the Yii unit tests is preferred over Codeception’s unit tests?

Well, yes, as for me, my folders structure is like that:

tests/

 codeception/  #all codecepiton tests goes here (codecept.phar bootstrap command)


 functional/   #some end-to-end tests with behat by itself


 unit/         #some unit-tests with phpunit

I also think that maybe you can use PhpBrowser it set more request headers and other params because it involves server, for now i am not using Yii1 module and switched to PhpBrowser for functional tests because of some problems with cyrillic and not-standart symbols in Yii1 module. PhpBrowser also as fast as tests with symfony browser kit with Yii1 module.

@jacmoe:

I install codeception via, guess what, composer, then it is placed into ./vendor/bin

I also used the global installation, but had some path issues there.

I briefly toyed with the idea of using composer, but … you know, it’s just a simple wget and that’s it. :lol:

I gave up, actually.

I accidentally ran the sample code for Yii as acceptance test and it gave me an OK, even though none of the tests were actually true.

And then I ran into db issues, etc.

But might tackle it again.

The documentation is really scarce, and assumes a lot of things (like prior exposure to Codeception, which is silly, actually), I also looked at your setup, but it didn’t work. Mainly due to db failures…

I see that you are using a dump.sql file?

I might try that.

Does it work against MySQL?

I am using fixtures in Yii, but that doesn’t seem to work with Codeception.

I will probably use a data mock library/tool and generate a test db.

One last thing: do you need to set up the PHP 5.4 browser? From php.ini, I mean?

@Ragazzo:

I’ve heard that you are writing a survival guide.

That sounds great. ;)