We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Codeception functional tests do not load any pages (only blank/empty pages)

Not sure how to accurately describe the situation (please advise if I'm missing something)

All functional tests return a blank page but I believe they hit the correct routes/urls because when I try to go to a route that requires login it is correctly redirected to the login page but the page is stil blank.

Can you help me figuring out what I am doing wrong? Thanks!

The test: func2Cept.php

<?php
$I = new FunctionalTester($scenario);

$I->wantTo('try to make functional tests work');
$I->amOnPage("/");
$x = $I->grabPageSource();
var_dump($x);

I followed the setup here: https://github.com/Codeception/phalcon-demo as well as the setup here: https://codeception.com/docs/modules/Phalcon

Running the test in debug mode gives me the following (note that the output of var_dump() is an empty string)

$ vendor/bin/codecept run functional --debug
Codeception PHP Testing Framework v2.4.0
Powered by PHPUnit 7.0.3 by Sebastian Bergmann and contributors.

Functional Tests (1) 
----------------------------------------------------------------------------------
Modules: \Helper\Functional, Asserts, Phalcon
----------------------------------------------------------------------------------
func2Cept: Try to make functional tests work
Signature: func2Cept
Test: tests/functional/func2Cept.php
Scenario --
  [Database] Transaction started
 I am on page "/"
  [Request Headers] []
  [Page] /
  [Response] 200
  [Request Cookies] []
  [Response Headers] []
 I grab page source 
/var/www/phalcon/kdlab/tests/functional/func2Cept.php:19:
string(0) ""
 PASSED 

  [Database] Transaction cancelled; all changes reverted.
----------------------------------------------------------------------------------

Time: 18 ms, Memory: 12.00MB

OK (1 test, 0 assertions)

Test redirect (try to go to a url which requires login, redirects to the login page)

$ vendor/bin/codecept run functional --debug
Codeception PHP Testing Framework v2.4.0
Powered by PHPUnit 7.0.3 by Sebastian Bergmann and contributors.

Functional Tests (1)
----------------------------------------------------------------------------------
Modules: \Helper\Functional, Asserts, Phalcon
----------------------------------------------------------------------------------
func2Cept: Try to make functional tests work
Signature: func2Cept
Test: tests/functional/func2Cept.php
Scenario --
  [Database] Transaction started
 I am on page "/welcome"
  [Request Headers] []
  [Page] /welcome
  [Response] 302
  [Request Cookies] []
  [Response Headers] {"HTTP/1.1 302 Found":null,"Status":"302 Found","Location":"/login"}
  [Redirecting to] /login
  [Page] /login
  [Response] 200
  [Request Cookies] []
  [Response Headers] []
 I grab page source 
/var/www/phalcon/kdlab/tests/functional/func2Cept.php:19:
string(0) ""
 PASSED 

  [Database] Transaction cancelled; all changes reverted.
----------------------------------------------------------------------------------

Time: 22 ms, Memory: 12.00MB

OK (1 test, 0 assertions)
edited May '18

Interesting. @sergeyklay will be able to help you on this for sure.

Hi, i try to use codeception functional test on phlacon project, and have same problem.

When i use echo, print() or var_dump() in my controller, then i can get output with $I->grabPageSource(). But when i use view to get output from controller ( fill view->content property with controller output), then $I->grabPageSource() return empty string. I try use _request and _loadPage methods to make request, but result was same.

I fiddle around with codeception for a few hours and i figured that handling application ($application->handle()) in Codeception/Lib/Connector/Phalcon dont render view.

I insert some outputs to Codeception/Lib/Connector/Phalcon->doRequest() function and get results in comments:

  public function doRequest($request)
  {
        // ...here is original code...

       // --- I ADD THIS ---
        var_dump($response->getContent());
        // string(0) ""
        $view = $di->get('view');
        var_dump($view->content);
        // array(3) {
        //   ["success"]=>
        //   bool(false)
        //   ["errorCode"]=>
        //   int(1)
        //   ["errorDesc"]=>
        //   string(13) "Invalid input"
        // }
        // -----------------------

        return new Response(
            $response->getContent(),
            $status ? $status : 200,
            $headers
        );
    }   

controller action :

    public function codeceptAction()
    {
        $this->view->layout = 'json';
        $this->view->content = [
            "someKey" => "some value"
        ];
    }

cest function:

     public function testCodeceptAction(\FunctionalTester $I) {
        $response = $I->callControllerAction('/test/codecept', []);
        var_dump($response);
    }

helper for function tester:

    public function callControllerAction($controllerUri, array $params = []) {
        $response = $this->getModule('Phalcon')->_request('POST', $controllerUri, $params);
        return $response;
    }

and finaly output from console:


druid33:~/$ ./codecept run -vvv functional LoadDashboardsConfigCest.php
Codeception PHP Testing Framework v2.3.0
Powered by PHPUnit 5.7.27 by Sebastian Bergmann and contributors.

  Rebuilding FunctionalTester...

Functional Tests (1) -------------------------------------------------------------------------------------------------------------------------------------------------------------------------
Modules: REST, PhpBrowser, \Helper\Functional, Filesystem, Asserts, Phalcon
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
LoadDashboardsConfigCest: Test codecept action
Signature: Tests\Functional\LoadDashboardsConfigCest:testCodeceptAction
Test: tests/functional/LoadDashboardsConfigCest.php:testCodeceptAction
Scenario --
 I call controller action "/test/codecept",[]
  [Request Headers] []
string(0) ""
array(1) {
  ["someKey"]=>
  string(10) "some value"
}
  [Page] /test/codecept
  [Response] 200
  [Request Cookies] []
  [Response Headers] []
string(0) ""
 PASSED 

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

Time: 143 ms, Memory: 10.00MB

OK (1 test, 0 assertions)

Hope, this information help someone to explain, whats is going on :)



9.5k
Accepted
answer
edited May '18

Thanks, everybody.

After three full days of intense debugging I finally found the error. Turns out it was a trivial but subtle error on my part. However, the process of debugging may still be of some interest.

The error was setting the constant APP_PATH like this:

    define('APP_PATH', realpath('..'));

instead of this:

    define('APP_PATH', realpath(__DIR__.'/..'));

So when running the application normally it just happened to be correct but not when running the functional tests.

In order to debug this issue I found a stackoverflow answer

Instead of running the test from the command line I run then codecept file directly after changing it to a proper php file (I just copied the vendor/bin/codecept file to my root of my project renamed it run.php and removed the first line: #!/usr/bin/env php) like this:

<?php
/**
 * Codeception CLI
 */

require_once __DIR__.'/autoload.php';

use Codeception\Application;

$app = new Application('Codeception', Codeception\Codecept::VERSION);
$app->add(new Codeception\Command\Build('build'));
$app->add(new Codeception\Command\Run('run'));
$app->add(new Codeception\Command\Init('init'));
$app->add(new Codeception\Command\Console('console'));
$app->add(new Codeception\Command\Bootstrap('bootstrap'));
$app->add(new Codeception\Command\GenerateCept('generate:cept'));
$app->add(new Codeception\Command\GenerateCest('generate:cest'));
$app->add(new Codeception\Command\GenerateTest('generate:test'));
$app->add(new Codeception\Command\GenerateSuite('generate:suite'));
$app->add(new Codeception\Command\GenerateHelper('generate:helper'));
$app->add(new Codeception\Command\GenerateScenarios('generate:scenarios'));
$app->add(new Codeception\Command\Clean('clean'));
$app->add(new Codeception\Command\GenerateGroup('generate:groupobject'));
$app->add(new Codeception\Command\GeneratePageObject('generate:pageobject'));
$app->add(new Codeception\Command\GenerateStepObject('generate:stepobject'));
$app->add(new Codeception\Command\GenerateEnvironment('generate:environment'));
$app->add(new Codeception\Command\GenerateFeature('generate:feature'));
$app->add(new Codeception\Command\GherkinSnippets('gherkin:snippets'));
$app->add(new Codeception\Command\GherkinSteps('gherkin:steps'));
$app->add(new Codeception\Command\DryRun('dry-run'));
$app->add(new Codeception\Command\ConfigValidate('config:validate'));

// Suggests package
if (class_exists('Stecman\Component\Symfony\Console\BashCompletion\CompletionCommand')) {
    $app->add(new Codeception\Command\Completion());
} else {
    $app->add(new Codeception\Command\CompletionFallback());
}

$app->registerCustomCommands();
$app->run();

Then I set up a debug entry as a PHP script in phpstorm and added the parameters "run functional"

Now it's possible to debug the tests in xdebug.

edited May '18

Hi, i try to use codeception functional test on phlacon project, and have same problem.

When i use echo, print() or var_dump() in my controller, then i can get output with $I->grabPageSource(). But when i use view to get output from controller ( fill view->content property with controller output), then $I->grabPageSource() return empty string. I try use _request and _loadPage methods to make request, but result was same.

I solved it (see my answer here in the thread). In my case it was how I set the APP_PATH constant. It may be in your case too as I believe I copied it from one of the official docs.

Hey, you're right! My APP_PATH constant was wrong.

Iam angry with myself, becouse there was a problem with Phalcon\Loader in my aplication when i run tests, but i ignore it. And it should be a hint to me, that something is wrong with paths.

As usual, the problem was between the keyboard and the chair. :)

Thank you very much.