Recently I updated Codeception from 2.2.2 to 2.2.4 and all tests that use Yii2 related things become broken.
var_dump(Yii::$app) returns NULL.
I have no idea what've been changed, I've read changelog and haven't seen anything specific to this problem. Codeception 2.2.2 works pretty well without changing a line of code.
/vagrant composer exec codecept run unit SomeCardTest -vvv
Reading ./composer.json
Loading config file /home/vagrant/.config/composer/auth.json
Loading config file ./composer.json
Checked CA file /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem: valid
Executing command (/vagrant): git branch --no-color --no-abbrev -v
Executing command (/vagrant): git describe --exact-match --tags
Executing command (/vagrant): git log --pretty="%H" -n1 HEAD
Reading /home/vagrant/.config/composer/composer.json
Loading config file /home/vagrant/.config/composer/auth.json
Loading config file /home/vagrant/.config/composer/composer.json
Loading config file /home/vagrant/.config/composer/auth.json
Reading /home/vagrant/.config/composer/auth.json
Reading /vagrant/vendor/composer/installed.json
Reading /home/vagrant/.config/composer/vendor/composer/installed.json
Loading plugin yii\composer\Plugin
Loading plugin Fxp\Composer\AssetPlugin\FxpAssetPlugin
Running 1.2.0 (2016-07-19 01:28:52) with PHP 7.0.9 on Linux / 3.10.0-327.28.2.el7.x86_64
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
> __exec_command: codecept 'run' 'unit' 'SomeCardTest'
Executing command (CWD): codecept 'run' 'unit' 'SomeCardTest'
Codeception PHP Testing Framework v2.2.4
Powered by PHPUnit 5.4.8 by Sebastian Bergmann and contributors.
Unit Tests (1) ---------------------------------------------------------------
✔ SomeCardTest: Update new card (0.02s)
PHP Fatal error: Uncaught Error: Call to a member function getModule() on null in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php:32
Stack trace:
#0 /vagrant/vendor/yiisoft/yii2/base/ErrorHandler.php(105): bedezign\yii2\audit\components\console\ErrorHandler->logException(Object(Error))
#1 [internal function]: yii\base\ErrorHandler->handleException(Object(Error))
#2 {main}
thrown in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php on line 32
FATAL ERROR. TESTS NOT FINISHED.
Uncaught Error: Call to a member function getModule() on null in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php:32
Stack trace:
#0 /vagrant/vendor/yiisoft/yii2/base/ErrorHandler.php(105): bedezign\yii2\audit\components\console\ErrorHandler->logException(Object(Error))
#1 [internal function]: yii\base\ErrorHandler->handleException(Object(Error))
#2 {main}
thrown
in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php:32
PHP Fatal error: Uncaught Error: Call to a member function getModule() on null in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php:32
Stack trace:
#0 /vagrant/vendor/yiisoft/yii2/base/ErrorHandler.php(239): bedezign\yii2\audit\components\console\ErrorHandler->logException(Object(yii\base\ErrorException))
#1 [internal function]: yii\base\ErrorHandler->handleFatalError()
#2 {main}
thrown in /vagrant/vendor/bedezign/yii2-audit/src/components/base/ErrorHandlerTrait.php on line 32
Script codecept handling the __exec_command event returned with error code 255
Here's blank test:
class SomeCardTest extends \yii\codeception\TestCase
{
public $appConfig = '@tests/codeception/_config/unit.php';
/**
* @var \UnitTester
*/
protected $tester;
// tests
public function testUpdateNewCard()
{
}
}
/vagrant composer show
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
2amigos/yii2-chartjs-widget 2.0.1 ChartJs widget for Yii2.
2amigos/yii2-ckeditor-widget 1.0.4 CKEditor widget for Yii2.
almasaeed2010/adminlte v2.3.6 AdminLTE - admin control panel and dashbo...
bedezign/yii2-audit 1.0.7 Yii2 Audit records and displays web/cli r...
behat/gherkin v4.4.1 Gherkin DSL parser for PHP 5.3
beowulfenator/yii2-json-editor 1.1.3 Yii2 wrapper for jdorn/json-editor
bower-asset/ace-builds v1.1.9
bower-asset/bootstrap v3.3.7 The most popular front-end framework for ...
bower-asset/chartjs v2.1.6 Simple HTML5 Charts using the canvas element
bower-asset/jquery 2.2.4
bower-asset/jquery-cookie v1.4.1
bower-asset/jquery-ui 1.11.4
bower-asset/jquery.inputmask 3.2.7 jquery.inputmask is a jquery plugin which...
bower-asset/json-editor v0.7.28 JSON Schema based editor
bower-asset/jsoneditor v3.2.0 A web-based tool to view, edit and format...
bower-asset/microplugin v0.0.3 A lightweight plugin / dependency system ...
bower-asset/punycode v1.3.2
bower-asset/selectize v0.12.1 Selectize is a jQuery-based custom <selec...
bower-asset/sifter v0.4.5 A library for textually searching arrays ...
bower-asset/typeahead.js v0.11.1
bower-asset/yii2-pjax v2.0.6
cebe/js-search 0.9.2 A client side search engine for use on st...
cebe/markdown 1.0.2 A super fast, highly extensible markdown ...
cebe/markdown-latex 1.0.0 A super fast highly extensible markdown p...
cebe/yii2-gravatar 1.1 Gravatar Widget for Yii 2
cmrcx/phptidy v2.16 PHP code formatter
codeception/codeception 2.2.4 BDD-style testing framework
codeception/specify 0.4.3 BDD code blocks for PHPUnit and Codeception
codeception/verify 0.3.1 BDD assertion library for PHPUnit
codemix/yii2-localeurls 1.4.3 Automatic locale/language management for ...
codemix/yii2-streamlog 1.0.0 A Yii 2 log target for streams in URL format
creocoder/yii2-nested-sets 0.9.0 The nested sets behavior for the Yii fram...
dektrium/yii2-rbac 0.3.0 RBAC management module for Yii2
dektrium/yii2-user 0.9.9 Flexible user registration and authentica...
devgroup/yii2-jsoneditor v1.0.0 Yii2 jsoneditor widget
dmstr/yii2-adminlte-asset 2.3.3 AdminLTE backend theme asset bundle for Y...
dmstr/yii2-backend-module 0.4.1 Admin Dashboard
dmstr/yii2-bootstrap 0.2.0 yii2 bootstrap components and extensions ...
dmstr/yii2-cms-dev-metapackage 1.1.1 Content Management Development Extensions...
dmstr/yii2-cms-metapackage 4.4.0 Content Management Extensions for Phundam...
dmstr/yii2-db 0.7.4 Database extensions
dmstr/yii2-helpers 0.4.0 Yii2 Helpers
dmstr/yii2-libs-metapackage 0.3.0 Core Library Extensions for Phundament 4 ...
dmstr/yii2-migrate-command 0.3.1 Console Migration Command with multiple p...
dmstr/yii2-pages-module 0.15.2 Application sitemap and navigation manage...
dmstr/yii2-prototype-module 0.2.9 Backend UI for static files
dmstr/yii2-web 0.3.1 Yii2 Framework web traits
doctrine/instantiator 1.0.5 A small, lightweight utility to instantia...
ezyang/htmlpurifier v4.8.0 Standards compliant HTML filter written i...
facebook/webdriver 1.1.3 A PHP client for WebDriver
friendsofphp/php-cs-fixer v1.11.6 A tool to automatically fix PHP code style
fzaninotto/faker v1.6.0 Faker is a PHP library that generates fak...
guzzlehttp/guzzle 6.2.1 Guzzle is a PHP HTTP client library
guzzlehttp/promises 1.2.0 Guzzle promises library
guzzlehttp/psr7 1.3.1 PSR-7 message implementation
insolita/yii2-adminlte-widgets v1.1.6 Widgets for AdminLTE theme (Box with reme...
kartik-v/bootstrap-fileinput v4.3.4 An enhanced HTML 5 file input for Bootstr...
kartik-v/mpdf v1.0.2 A PHP class to generate PDF files from HT...
kartik-v/yii2-krajee-base 1.8.5 Base library and foundation components fo...
kartik-v/yii2-mpdf v1.0.0 A Yii2 wrapper component for the mPDF lib...
kartik-v/yii2-tree-manager v1.0.5 An enhanced tree management module with t...
kartik-v/yii2-widget-activeform v1.4.8 Enhanced Yii2 active-form and active-fiel...
kartik-v/yii2-widget-fileinput dev-master 6fc8922 An enhanced FileInput widget for Bootstra...
kartik-v/yii2-widget-select2 v2.0.8 Enhanced Yii2 wrapper for the Select2 jQu...
lajax/yii2-translate-manager 1.6.0 Online Translate
lstrojny/functional-php 1.2.3 Functional primitives for PHP
mikehaertl/php-shellcommand 1.2.2 An object oriented interface to shell com...
mikevanriel/text-to-latex 1.0.1 A converter class that converts normal AS...
miloschuman/yii2-highcharts-widget dev-master cb0cb30 Highcharts widget for Yii 2 Framework.
myclabs/deep-copy 1.5.1 Create deep copies (clones) of your objects
nikic/php-parser v0.9.5 A PHP parser written in PHP
pheme/yii2-settings 0.4 Yii2 Database settings
pheme/yii2-toggle-column 0.4 Provides a toggle data column and action
phpdocumentor/reflection 1.0.7 Reflection library to do Static Analysis ...
phpdocumentor/reflection-docblock 2.0.4
phpspec/php-diff v1.1.0 A comprehensive library for generating di...
phpspec/prophecy v1.6.1 Highly opinionated mocking framework for ...
phpunit/php-code-coverage 4.0.1 Library that provides collection, process...
phpunit/php-file-iterator 1.4.1 FilterIterator implementation that filter...
phpunit/php-text-template 1.2.1 Simple template engine.
phpunit/php-timer 1.0.8 Utility class for timing
phpunit/php-token-stream 1.4.8 Wrapper around PHP's tokenizer extension.
phpunit/phpunit 5.4.8 The PHP Unit Testing framework.
phpunit/phpunit-mock-objects 3.2.3 Mock Object library for PHPUnit
psr/http-message 1.0.1 Common interface for HTTP messages
psr/log 1.0.0 Common interface for logging libraries
rmrevin/yii2-fontawesome 2.15.1 Asset Bundle for Yii2 with Font Awesome
sabre/uri 1.1.0 Functions for making sense out of URIs.
sabre/xml 1.4.2 sabre/xml is an XML library that you may ...
schmunk42/retry 0.0.1 A tiny library for retrying failing opera...
schmunk42/yii2-giiant 0.8.4 Gii CRUD generator for Yii 2 Framework
schmunk42/yii2-markdocs-module 0.3.0 Renders markdown files from local folder ...
scrivo/highlight.php v8.9.1 Server side syntax highlighter that suppo...
sebastian/code-unit-reverse-lookup 1.0.0 Looks up which function or method a line ...
sebastian/comparator 1.2.0 Provides the functionality to compare PHP...
sebastian/diff 1.4.1 Diff implementation
sebastian/environment 1.3.7 Provides functionality to handle HHVM/PHP...
sebastian/exporter 1.2.2 Provides the functionality to export PHP ...
sebastian/global-state 1.1.1 Snapshotting of global state
sebastian/object-enumerator 1.0.0 Traverses array structures and object gra...
sebastian/recursion-context 1.0.2 Provides functionality to recursively pro...
sebastian/resource-operations 1.0.0 Provides a list of PHP built-in functions...
sebastian/version 2.0.0 Library that helps with managing the vers...
squizlabs/php_codesniffer 2.6.2 PHP_CodeSniffer tokenizes PHP, JavaScript...
swiftmailer/swiftmailer v5.4.3 Swiftmailer, free feature-rich PHP mailer
symfony/browser-kit v3.1.3 Symfony BrowserKit Component
symfony/console v3.1.3 Symfony Console Component
symfony/css-selector v3.1.3 Symfony CssSelector Component
symfony/dom-crawler v3.1.3 Symfony DomCrawler Component
symfony/event-dispatcher v3.1.3 Symfony EventDispatcher Component
symfony/filesystem v3.1.3 Symfony Filesystem Component
symfony/finder v3.1.3 Symfony Finder Component
symfony/polyfill-mbstring v1.2.0 Symfony polyfill for the Mbstring extension
symfony/process v3.1.3 Symfony Process Component
symfony/stopwatch v3.1.3 Symfony Stopwatch Component
symfony/yaml v3.1.3 Symfony Yaml Component
trntv/yii2-aceeditor 2.0.0 Yii2 ajax.org Ace Editor widget
twig/twig v1.24.1 Twig, the flexible, fast, and secure temp...
vlucas/phpdotenv v2.3.0 Loads environment variables from `.env` t...
yiisoft/yii2 2.0.9 Yii PHP Framework Version 2
yiisoft/yii2-apidoc 2.0.5 API Documentation generator for the Yii f...
yiisoft/yii2-authclient 2.1.0 External authentication via OAuth and Ope...
yiisoft/yii2-bootstrap 2.0.6 The Twitter Bootstrap extension for the Y...
yiisoft/yii2-codeception 2.0.5 The Codeception integration for the Yii f...
yiisoft/yii2-coding-standards 2.0.1 Yii PHP Framework Version 2 - Coding stan...
yiisoft/yii2-composer 2.0.4 The composer plugin for Yii extension ins...
yiisoft/yii2-debug 2.0.6 The debugger extension for the Yii framework
yiisoft/yii2-faker 2.0.3 Fixture generator. The Faker integration ...
yiisoft/yii2-gii 2.0.5 The Gii extension for the Yii framework
yiisoft/yii2-httpclient 2.0.1 HTTP client extension for the Yii framework
yiisoft/yii2-jui 2.0.6 The Jquery UI extension for the Yii frame...
yiisoft/yii2-swiftmailer 2.0.5 The SwiftMailer integration for the Yii f...
yiisoft/yii2-twig 2.0.4 The Twig integration for the Yii framework
class_name: UnitTester
modules:
enabled:
- Asserts
- Yii2
config:
Yii2:
configFile: 'tests/codeception/_config/unit.php'
I have no idea what've been changed, I've read changelog and haven't seen anything specific to this problem. Codeception 2.2.2 works pretty well without changing a line of code.
There was major refactoring. One of part of it is deprcation of yiisoft/yii2-codeception package. You can remove it now and use Codeception only.
Try to use class SomeCardTest extends \Codeception\Test\Unit
also use
config:
Yii2:
part: init
configFile: 'tests/codeception/_config/unit.php'
More http://codeception.com/for/yii#unit-tests
@DavertMik Ok, I got it, thank you. But there was a useful method globalFixtures() in DbTestCase class, it was executed before all fixtures and did some initialization, we used it for migration purposes before tests. So what should we use now?
Sorry, I'm replying after 1.5 month but it may probably be helpful to others.
Now it is recommended to create your own helper with _beforeSuite to implement your own global setup .
I tried this in my helper
public function _beforeSuite($settings = [])
{
$migrateController = new MigrateController('migrate', \Yii::$app);
$migrateController->interactive = false;
$migrateController->runAction('up');
}
It doesn't work due to Yii app haven't been initialized yet I think, but works _before method, Anyway I have thougts that I'm doing something wrong. Could you explain a bit? I want migrations executing once right after dump loading, what method should I use?
So what is the proper way of defining globalFixtures() for tests after all the changes?
I want to ask @DavertMik about status of my solution (for functional tests to support db in actual condition before tests). Is it workaround or is it normal solution.
At first we have to create a module as described here https://barryvanveen.nl/blog/37-how-to-create-and-configure-a-custom-module-in-codeception. In result we'll have something like this:
/**
* Cleaning up our database
*/
public function _afterSuite()
{
$this->getModule('Db')->_getDriver()->cleanup();
}
/**
* Make migrations up
* @param type $settings
*/
public function _beforeSuite($settings = [])
{
$migrateController = new \yii\console\controllers\MigrateController('migrate', \Yii::$app);
$migrateController->interactive = false;
//$migrateController->compact = true; from 2.0.13
$migrateController->runAction('up');
}
Then we register newly created module in our .yaml file. BUT, our module must be registered after codeception "DB" module. Because everything is happens in one thread. So, to make migrations up, we must have prepared database from our dump file.
Then we have a problem: \Yii::$app is NULL. To make it object we must refresh our memory about _bootstrap.php file (local for functional tests or global for all kinds of tests).
I put in it such content:
define('YII_ENV', 'test');
defined('YII_DEBUG') or define('YII_DEBUG', true);
require_once __DIR__ . '/../vendor/yiisoft/yii2/Yii.php';
require __DIR__ . '/../vendor/autoload.php';
$config = require(__DIR__ . '/../config/test.php');
(new \yii\web\Application($config))->run();
Yii::setAlias('@tests', __DIR__);
As result, everything works as expected. Migration command is calling only once. Database is cleaning after test suites are finished. BTW, configuration of DB module is
cleanup: false
populate: true
I hope it will help somebody.
Most helpful comment
There was major refactoring. One of part of it is deprcation of
yiisoft/yii2-codeceptionpackage. You can remove it now and use Codeception only.Try to use
class SomeCardTest extends \Codeception\Test\Unitalso use
More http://codeception.com/for/yii#unit-tests