phundament / app

Dockerized Yii2 web application base
http://phundament.com
Other
310 stars 129 forks source link

Codeception yii console application init added. #198

Closed StalkAlex closed 8 years ago

StalkAlex commented 8 years ago

Not sure if this should work this way or not, but without it codeception throwed me "class not found" exceptions, when I tried to use classes from my components folder $helper = new \app\components\XmlHelper(); something like that in my test function.

schmunk42 commented 8 years ago

In which type/suite did you get errors (unit, functional, acceptance)?

StalkAlex commented 8 years ago

Unit, one test file with one test method. It seems logically, codeception can't see application classes because framework class loader has not been loaded/initialised.

schmunk42 commented 8 years ago

@akaSStalkALEX Could you double check your unit tests.

I've just had a similar problem, but extending from yii\codeception\TestCase with the correct $appConfig setting should take care about application initialization.

I've added some tests for login/logout with the user module, which requires correct initialization of several application components. See also https://github.com/phundament/app/blob/master/tests/codeception/unit/prod/models/UserTest.php

I've also tried your PR, which also makes the tests pass, because an application gets initialized. But it would be cleaner, if the TestCase would do the job.

CC: @Quexer69 Unit test setup.

schmunk42 commented 8 years ago

And they pass when you've done the correct user setup, see https://travis-ci.org/phundament/app/jobs/113484331#L1181

StalkAlex commented 8 years ago

I changed my test as you proposed. It works. But thing is test that I used was generated by console command codecept generate:test unit Example. It looks like this

class ExampleTest extends \Codeception\TestCase\Test
{
    /**
     * @var \UnitTester
     */
    protected $tester;

    protected function _before()
    {
    }

    protected function _after()
    {
    }

    // tests
    public function testMe()
    {

    }
}

Codecept generates this file in unit folder and it doesn't include $appConfig by default. Also with $this->tester->assertTrue(a,b) code completion works in PhpStorm, but with $this->assertTrue(a,b) I can't see hints in PhpStorm (assertFalse, assertNull etc.).

If I change extends \Codeception\TestCase\Test to extends \yii\codeception\TestCase then generated version works.

schmunk42 commented 8 years ago

@akaSStalkALEX About the generated tests ... can you specify a template for the tests in Codeception?

Alternatively we could provide a custom gii command.

A phpdoc variable defnition like

<?php
/* @var $this Codeception\ClassXYZ */
?>

should fix the code-completion.

schmunk42 commented 8 years ago

By the way: Which codecept version are you using? There were some changes from 2.0 to 2.1. Do you use Phundament with Docker or directly on your machine?

StalkAlex commented 8 years ago
  1. Unfortunately we're unsing Phundament app without Docker due to our local architecture.
  2. Codeception's verison is 2.1.5 installed through composer.
  3. Not sure where template can be found, but templates folder in unit folder contains only _bootstrap.php and fixtures.