symfony / panther

A browser testing and web crawling library for PHP and Symfony
MIT License
2.91k stars 213 forks source link

Multi domain app and Panther awareness of environment #533

Open extrablind opened 2 years ago

extrablind commented 2 years ago

Hi !

I'm trying to launch tests with multi domain app. As a good fellow I read the doc and make a lot of searchs ;)

The problem : I've got an app with 4 subdomains so I use docker to launch it. Ok. I resolve hosts on local machine and Docker serves webserver on thoses subdomains. Ok. I can launch test in PANTHER_NO_HEADLESS=1 ./vendor/bin/phpunit -c ./phpunit.xml.dist from my local machine assuming same env as Docker. I can PANTHER_NO_HEADLESS=0 ./vendor/bin/phpunit (default) from inside docker container (firefox and chrome installed on container) Now I want php unit to switch environment : .env.test and APP_ENV with proper env definition in phpunit.xml.dist (ie :<env name="APP_ENV" value="test"/>)

What I understand : Because I ask for a domain that is not launched by PHP builtin server (when Panther starts) it's as if I'm asking for any website, so Panther is not aware of the context : it use the context of my actual app (dev not test .env file). So only workaround is to switch the .env from APP_ENV=dev to APP_ENV=test manually before starting tests. Is there a better solution, so Panther can be aware that subdomains are actually the tested app in test mode ?

# Creation og panther client
protected function appCreateClient($domain = 'domain'){
    $client =  static::createPantherClient(
     [
       'external_base_uri' => 'https://'.$domain.'.dev:4433',
       'browser' => 'firefox',
     ],
     [],
     [
       'capabilities' => [
           'acceptInsecureCerts' => true,
       ],
     ]
   );
   // Force window size on startup
   $size = new WebDriverDimension(1800, 1000);
   $client->manage()->window()->maximize()->setSize($size);
   return $client;
}

Thanks for any light on this.

domis86 commented 2 years ago

Can you try adding another domain like :

'external_base_uri' => 'https://'.$domain.'.dev.test:4433',

which points to another front controller like index_test.php (copied from index.php but with changed APP_ENV to test) ?

Or in server configuration (like https://symfony.com/doc/current/setup/web_server_configuration.html#nginx or https://symfony.com/doc/current/setup/web_server_configuration.html#apache-with-mod-php-php-cgi ) set different APP_ENV for that domain:

        # optionally set the value of the environment variables used in the application
        fastcgi_param APP_ENV test;
extrablind commented 2 years ago

Good point on first solution which seems convenient but requires extra processing to deploy (like deleting the index_test.php file). Basically symfony env the old way. Just adding index_test.php with good conf and call domain.dev:4433/index_test.php instead of domain.dev:4433 is ok as well and uses .env.test file The second solution is interesting too, but I prefere to rely on .env file in order to keep it clean.

# public/index_test.php
use App\Kernel;

require_once dirname(__DIR__).'/vendor/autoload_runtime.php';

return function (array $context) {
    $debug = true;
    return new Kernel('test',  $debug);
};

This will do the trick for the moment... Thanks for your reply domis86

arderyp commented 2 years ago

i too can run PANTHER_NO_HEADLESS=1 ./vendor/bin/phpunit

but when I put the following in phpunit.xml it doesn't work: <env name="PANTHER_NO_HEADLESS" value="1"/>