My tests run just fine from command line, but from PhpStorm I get very strange issue. For some reason assertNull when called from PhpStorm tries to convert parameter (ActiveObject) to string which in turn causes Codeception code in Step.php to check if the object is mocked, which calls __isset on ActiveRecord (as __mocked is not defined) and that causes ActiveRecord to try and check the attribute in schema which fails as Yii app object is null at that moment. Here is output of both runs as well as code with some comments (I would appreciate concrete answer to why is this happening and how can I solve it instead of some general advice like best practices, code smell, etc. as this code is just temporary and is not meant to be used later on). Thanks in advance:
CODE ***************************************
public function databaseReady(\FunctionalTester $I)
{
try {
$I->assertSame("test", Yii::$app->db->createCommand("SELECT DATABASE()")->queryScalar(), "We are connected to test database");
} catch (Exception $e) {
codecept_debug("Error from test:".$e->getMessage());
}
$user = User::findActive()->one();
codecept_debug($user->first_name);
//this fails from PhpStorm but works from command line, why? What I know so far:
//Codeception step tries to stringify User ($user) object
//Then it checks if it has __mocked and since isset is used on non existing prop, __isset is called on ActiveRecord
//Then active records sees there is no __mocked in attributes
//Then it tries to load it from schema
//Then it fails in ActiveRecord.php 135 because Yii::$app is null at that moment
//If I var_dump Yii::$app after this line it's not null, not sure why it happens but it is real problem
$I->assertNotEmpty($user, "User is loaded from database");
$I->assertSame("admin TEST !5!6!9!", $user->first_name, "Test database user is used");
}
CODE ***************************************
COMMAND LINE ***********************************************************************************************
$ docker exec -it sceal_console_1 bash -c "./vendor/bin/codecept run functional --debug"
Codeception PHP Testing Framework v2.4.1
Powered by PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
[Db] Connected to test
[Db] Connected to test
[Db] Executing Populator: mysql -uroot -ptest -hsceal_testdb_1 test < tests/_data/sceal_clean.sql
mysql: [Warning] Using a password on the command line interface can be insecure.
[Db] Populator Finished.
Functional Tests (1) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Modules: Filesystem, Yii2, Asserts, Db
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------AttachmentsCest: Database ready
Signature: tests\AttachmentsCest:databaseReady
Test: tests/functional/AttachmentsCest.php:databaseReady
Scenario --
Destroying application
Starting application
[Fixtures] Loading fixtures
[Fixtures] Done
[yii\db\Connection::open] 'Opening DB connection: mysql:host=sceal_testdb_1;dbname=test'
I assert same "test","test","We are connected to test database"
admin TEST !5!6!9!
I assert not empty "common\models\User","User is loaded from database"
I assert same "admin TEST !5!6!9!","admin TEST !5!6!9!","Test database user is used"
PASSED
Destroying application
Suite done, restoring $_SERVER to original
Time: 5.99 seconds, Memory: 14.00MB
OK (1 test, 3 assertions)
COMMAND LINE ***********************************************************************************************
PHP STORM *********************************************************************
Testing started at 17:10 ...
docker://sceal_console:latest/php /opt/.phpstorm_helpers/codeception.php run --report -o "reporters: report: PhpStorm_Codeception_ReportPrinter" --no-ansi --no-interaction --debug -c /opt/project/codeception.yml functional
Codeception PHP Testing Framework v2.4.1
Powered by PHPUnit 6.5.7 by Sebastian Bergmann and contributors.
mysql: [Warning] Using a password on the command line interface can be insecure.
An Error occurred while handling another error:
Error: Call to a member function has() on null in /opt/project/vendor/yiisoft/yii2/web/ErrorHandler.php:91
Stack trace:
0 /opt/project/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(Error))
1 [internal function]: yii\base\ErrorHandler->handleException(Object(Error))
2 {main}
Previous exception:
Error: Call to a member function getDb() on null in /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php:135
Stack trace:
0 /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php(382): yii\db\ActiveRecord::getDb()
1 /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php(418): yii\db\ActiveRecord::getTableSchema()
2 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(492): yii\db\ActiveRecord->attributes()
3 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(289): yii\db\BaseActiveRecord->hasAttribute('__mocked')
4 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(335): yii\db\BaseActiveRecord->get('mocked')
5 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(176): yii\db\BaseActiveRecord->isset('mocked')
6 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(164): Codeception\Step->getClassName(Object(common\models\User))
7 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(103): Codeception\Step->stringifyArgument(Object(common\models\User))
8 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(235): Codeception\Step->getArgumentsAsString(200)
9 /opt/.phpstorm_helpers/codeception_24_56.php(337): Codeception\Step->getHumanizedArguments()
10 /opt/project/vendor/phpunit/phpunit/src/Framework/TestResult.php(408): PhpStorm_Codeception_ReportPrinter->endTest(Object(Codeception\Test\Cest), 0.089478015899658)
11 /opt/project/vendor/codeception/codeception/src/Codeception/Test/Test.php(111): PHPUnit\Framework\TestResult->endTest(Object(Codeception\Test\Cest), 0.089478015899658)
12 /opt/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(755): Codeception\Test\Test->run(Object(PHPUnit\Framework\TestResult))
13 /opt/project/vendor/codeception/phpunit-wrapper/src/Runner.php(106): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
14 /opt/project/vendor/codeception/codeception/src/Codeception/SuiteManager.php(157): Codeception\PHPUnit\Runner->doEnhancedRun(Object(Codeception\Suite), Object(PHPUnit\Framework\TestResult), Array)
15 /opt/project/vendor/codeception/codeception/src/Codeception/Codecept.php(189): Codeception\SuiteManager->run(Object(Codeception\PHPUnit\Runner), Object(PHPUnit\Framework\TestResult), Array)
16 /opt/project/vendor/codeception/codeception/src/Codeception/Codecept.php(158): Codeception\Codecept->runSuite(Array, 'functional', NULL)
17 /opt/project/vendor/codeception/codeception/src/Codeception/Command/Run.php(466): Codeception\Codecept->run('functional')
18 /opt/project/vendor/codeception/codeception/src/Codeception/Command/Run.php(361): Codeception\Command\Run->runSuites(Array, Array)
19 /opt/project/vendor/symfony/console/Command/Command.php(252): Codeception\Command\Run->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
20 /opt/project/vendor/symfony/console/Application.php(946): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
21 /opt/project/vendor/symfony/console/Application.php(248): Symfony\Component\Console\Application->doRunCommand(Object(Codeception\Command\Run), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
22 /opt/project/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
23 /opt/project/vendor/codeception/codeception/src/Codeception/Application.php(108): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
24 /opt/.phpstorm_helpers/codeception.php(40): Codeception\Application->run()
25 {main}
An Error occurred while handling another error:
Error: Call to a member function has() on null in /opt/project/vendor/yiisoft/yii2/web/ErrorHandler.php:91
Stack trace:
0 /opt/project/vendor/yiisoft/yii2/base/ErrorHandler.php(111): yii\web\ErrorHandler->renderException(Object(Error))
1 [internal function]: yii\base\ErrorHandler->handleException(Object(Error))
2 {main}
Previous exception:
Error: Call to a member function getDb() on null in /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php:135
Stack trace:
0 /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php(382): yii\db\ActiveRecord::getDb()
1 /opt/project/vendor/yiisoft/yii2/db/ActiveRecord.php(418): yii\db\ActiveRecord::getTableSchema()
2 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(492): yii\db\ActiveRecord->attributes()
3 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(289): yii\db\BaseActiveRecord->hasAttribute('__mocked')
4 /opt/project/vendor/yiisoft/yii2/db/BaseActiveRecord.php(335): yii\db\BaseActiveRecord->get('mocked')
5 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(176): yii\db\BaseActiveRecord->isset('mocked')
6 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(164): Codeception\Step->getClassName(Object(common\models\User))
7 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(103): Codeception\Step->stringifyArgument(Object(common\models\User))
8 /opt/project/vendor/codeception/codeception/src/Codeception/Step.php(235): Codeception\Step->getArgumentsAsString(200)
9 /opt/.phpstorm_helpers/codeception_24_56.php(337): Codeception\Step->getHumanizedArguments()
10 /opt/project/vendor/phpunit/phpunit/src/Framework/TestResult.php(408): PhpStorm_Codeception_ReportPrinter->endTest(Object(Codeception\Test\Cest), 0.089478015899658)
11 /opt/project/vendor/codeception/codeception/src/Codeception/Test/Test.php(111): PHPUnit\Framework\TestResult->endTest(Object(Codeception\Test\Cest), 0.089478015899658)
12 /opt/project/vendor/phpunit/phpunit/src/Framework/TestSuite.php(755): Codeception\Test\Test->run(Object(PHPUnit\Framework\TestResult))
13 /opt/project/vendor/codeception/phpunit-wrapper/src/Runner.php(106): PHPUnit\Framework\TestSuite->run(Object(PHPUnit\Framework\TestResult))
14 /opt/project/vendor/codeception/codeception/src/Codeception/SuiteManager.php(157): Codeception\PHPUnit\Runner->doEnhancedRun(Object(Codeception\Suite), Object(PHPUnit\Framework\TestResult), Array)
15 /opt/project/vendor/codeception/codeception/src/Codeception/Codecept.php(189): Codeception\SuiteManager->run(Object(Codeception\PHPUnit\Runner), Object(PHPUnit\Framework\TestResult), Array)
16 /opt/project/vendor/codeception/codeception/src/Codeception/Codecept.php(158): Codeception\Codecept->runSuite(Array, 'functional', NULL)
17 /opt/project/vendor/codeception/codeception/src/Codeception/Command/Run.php(466): Codeception\Codecept->run('functional')
18 /opt/project/vendor/codeception/codeception/src/Codeception/Command/Run.php(361): Codeception\Command\Run->runSuites(Array, Array)
19 /opt/project/vendor/symfony/console/Command/Command.php(252): Codeception\Command\Run->execute(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
20 /opt/project/vendor/symfony/console/Application.php(946): Symfony\Component\Console\Command\Command->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
21 /opt/project/vendor/symfony/console/Application.php(248): Symfony\Component\Console\Application->doRunCommand(Object(Codeception\Command\Run), Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
22 /opt/project/vendor/symfony/console/Application.php(148): Symfony\Component\Console\Application->doRun(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
23 /opt/project/vendor/codeception/codeception/src/Codeception/Application.php(108): Symfony\Component\Console\Application->run(Object(Symfony\Component\Console\Input\ArgvInput), Object(Symfony\Component\Console\Output\ConsoleOutput))
24 /opt/.phpstorm_helpers/codeception.php(40): Codeception\Application->run()
25 {main}
$_SERVER = [
'PATH' => '/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin',
'HOSTNAME' => '98cc7ec2f196',
'JETBRAINS_REMOTE_RUN' => '1',
'IDE_SCRIPT_PREFIX' => '',
'IDE_CODECEPTION_EXE' => '/opt/project/vendor/codeception/codeception/codecept',
'DEBIAN_FRONTEND' => 'teletype',
'LANG' => 'en_US.UTF-8',
'LANGUAGE' => 'en_US.UTF-8',
'LC_ALL' => 'en_US.UTF-8',
'LC_CTYPE' => 'UTF-8',
'TERM' => 'xterm',
'HOME' => '/root',
'PHP_SELF' => '/opt/.phpstorm_helpers/codeception.php',
'SCRIPT_NAME' => 'api/web/index-test.php',
'SCRIPT_FILENAME' => 'index-test.php',
'PATH_TRANSLATED' => '/opt/.phpstorm_helpers/codeception.php',
'DOCUMENT_ROOT' => '',
'REQUEST_TIME_FLOAT' => 1523283055.505418,
'REQUEST_TIME' => 1523283055,
'argv' => [
'/opt/.phpstorm_helpers/codeception.php',
'run',
'--report',
'-o',
'reporters: report: PhpStorm_Codeception_ReportPrinter',
'--no-ansi',
'--no-interaction',
'--debug',
'-c',
'/opt/project/codeception.yml',
'functional',
],
'argc' => 11,
'SHELL_VERBOSITY' => 0,
'SERVER_NAME' => null,
'SERVER_PORT' => '80',
'HTTPS' => false,
]
PHP Fatal error: Uncaught RuntimeException: Command Did Not Finish Properly in /opt/project/vendor/codeception/codeception/src/Codeception/Subscriber/ErrorHandler.php:101
Stack trace:
0 [internal function]: Codeception\Subscriber\ErrorHandler->shutdownHandler()
1 {main}
thrown in /opt/project/vendor/codeception/codeception/src/Codeception/Subscriber/ErrorHandler.php on line 101
PHP Stack trace:
PHP 1. yii\web\ErrorHandler->handleException() /opt/project/vendor/yiisoft/yii2/base/ErrorHandler.php:0
PHP 2. yii\web\ErrorHandler->handleFallbackExceptionMessage() /opt/project/vendor/yiisoft/yii2/base/ErrorHandler.php:124
Process finished with exit code 1
PHP STORM *********************************************************************