Skip to content
This is the documentation for pieceofcake2/panther, a CakePHP 2.x plugin for pieceofcake2/cakephp. About this fork.

Configuration ​

The End-to-End tests are configured like Symfony Panther: with PANTHER_* environment variables and the options of createPantherClient().

phpunit.xml.dist ​

Register the extension and add an EndToEnd suite (docs/examples/phpunit.xml.dist):

xml
<phpunit bootstrap="./tests/bootstrap.php" defaultTestSuite="App">
    <extensions>
        <extension class="PieceOfCake2\Panther\ServerExtension"/>
    </extensions>
    <listeners>
        <listener class="CakeFixtureInjector" file="./vendor/pieceofcake2/cakephp/src/Cake/TestSuite/Fixture/CakeFixtureInjector.php"/>
    </listeners>
    <testsuites>
        <testsuite name="App">
            <directory>./tests/TestCase/</directory>
            <exclude>./tests/TestCase/EndToEnd/</exclude>
        </testsuite>
        <testsuite name="EndToEnd">
            <directory>./tests/TestCase/EndToEnd/</directory>
        </testsuite>
    </testsuites>
</phpunit>

The tests of pieceofcake2/app run with APP_ENV=test by default (tests/bootstrap.php sets it before CakePHP loads the .env files when it is not set), so the tests load config/.env.test, and the web server inherits it and loads the same file (Test Database).

Like Panther, the package reads its settings from $_SERVER, so they are <server> elements, as in the Symfony documentation. A variable of your shell is in $_SERVER too, but a <server> element replaces it: put per-machine values such as PANTHER_NO_HEADLESS=1 in your shell, or in a phpunit.xml copy of phpunit.xml.dist.

The extension keeps the web server and the browser running from the first End-to-End test to the last one of the run. Without it, they stop after each test class, as in Panther. Nothing starts until a test calls createPantherClient(), so a run of the App suite starts no browser.

Local Overrides ​

Put per-machine values in your shell, or in a phpunit.xml copy of phpunit.xml.dist (PHPUnit reads phpunit.xml first; keep it out of Git):

bash
PANTHER_NO_HEADLESS=1 vendor/bin/phpunit --testsuite EndToEnd     # watch the browser
PANTHER_WEB_SERVER_PORT=9180 vendor/bin/phpunit --testsuite EndToEnd

Environment variables ​

These are Panther's variables, with Panther's meaning. Set them in the shell, or with <server> in phpunit.xml.dist.

VariableMeaning
PANTHER_WEB_SERVER_DIRDocument root. Default: webroot/ or app/webroot/ of the project. Relative paths must start with ./
PANTHER_WEB_SERVER_PORTPort of the web server. Default 9080
PANTHER_WEB_SERVER_ROUTERRouter script, relative to the document root. Default: the CakePHP router script; empty for none
PANTHER_READINESS_PATHPath requested to check that the web server has started. Default /
PANTHER_EXTERNAL_BASE_URITest an application that is already running at this URL; no web server is started
PANTHER_APP_ENVPassed as APP_ENV to the web server. Only needed to give the web server a different APP_ENV than the tests; without it, the web server inherits APP_ENV of the tests
PANTHER_ERROR_SCREENSHOT_DIRDirectory for the screenshots of failed tests, e.g. ./tmp/error-screenshots
PANTHER_ERROR_SCREENSHOT_ATTACHPrint the screenshots in the JUnit attachment format
PANTHER_NO_HEADLESSShow the browser window
PANTHER_DEVTOOLSOpen the DevTools of the browser. Closed by default, as in Panther 2.3+, also with an older Panther (installed on PHP 8.0), which opens them docked in the window: the page is narrower, and the screenshots of the report used the scale of a phone
PANTHER_NO_REDUCED_MOTIONDo not ask for reduced motion (Panther 2.2+)
PANTHER_NO_SANDBOXDisable the sandbox of Chrome (containers, CI)
PANTHER_CHROME_ARGUMENTSMore Chrome arguments, separated by spaces, e.g. --lang=ja --window-size=1600,1200
PANTHER_CHROME_BINARYChrome binary to use
PANTHER_FIREFOX_ARGUMENTS, PANTHER_FIREFOX_BINARYThe same for Firefox

The PANTHER_REPORT_* variables of this package write a PDF report of the tests: see Test Report.

createPantherClient() options ​

createPantherClient(array $options = [], array $kernelOptions = [], array $managerOptions = []) is Panther's:

  • $options: webServerDir, hostname (default 127.0.0.1), port, router, readinessPath, external_base_uri, env (more environment variables for the web server), browser (static::CHROME, static::FIREFOX or static::SELENIUM) and browser_arguments
  • $kernelOptions: not used, there is no Symfony kernel
  • $managerOptions: options of the browser manager, e.g. port of chromedriver (default 9515), capabilities and chromedriver_arguments

The options apply when the browser starts, at the first call of the run; the following calls return the same browser.

The web server ​

createPantherClient() starts PHP's built-in web server with Panther's WebServerManager, with the options and variables above, and with these defaults for CakePHP.

The CakePHP router script ​

The default router script, vendor/pieceofcake2/panther/resources/router.php:

  • serves the files of the document root (CSS, JavaScript, images) as they are
  • hands every other request to index.php of the document root, with the script name CakePHP uses to detect the base path, also for URLs that look like files, such as /articles.json
  • keeps the cache of the web server (CACHE) in tmp/cache/panther/: the web server uses the test database as default, and must not use the model cache of your development server for it
  • changes to the working directory of the tests, which PantherTestCase passes to the web server as PANTHER_WORKING_DIR: the built-in web server runs PHP in the document root, so a relative path would point to another file than in the tests (Relative Paths)

For more, copy it into your application and set PANTHER_WEB_SERVER_ROUTER to the copy, relative to the document root, e.g. ../tests/router.php. PANTHER_WORKING_DIR is passed to your router script too, which can change to it the same way:

php
$workingDir = getenv('PANTHER_WORKING_DIR');
if (is_string($workingDir) && is_dir($workingDir)) {
    chdir($workingDir);
}

To run the web server in another directory, pass it with the env option, which wins over the working directory of the tests: static::createPantherClient(['env' => ['PANTHER_WORKING_DIR' => $dir]]).

Modern and legacy layouts ​

The document root is webroot/ of the project (pieceofcake2/app), or app/webroot/ when only that exists (the classic CakePHP 2 skeleton). The project is the Composer root package. Set PANTHER_WEB_SERVER_DIR for another directory.

See LICENSE for copyright and license information. Not affiliated with the Cake Software Foundation.