ercos / cakephp-cypress
Integration of cypress controller to handle authentication and db data
Installs: 582
Dependents: 0
Suggesters: 0
Security: 0
Type:cakephp-plugin
pkg:composer/ercos/cakephp-cypress
Requires
- php: >=8.0.8
- cakephp/cakephp: >=4.2
Requires (Dev)
README
This package allows you to run Cypress tests on a CakePHP project with factories and fixtures and is inspired on the Laravel Cypress package.
Requirements
- CakePHP 4.0+
- Cypress 10.0+
- PHP 8.0+
- https://github.com/josegonzalez/php-dotenv
- https://github.com/vierge-noire/cakephp-fixture-factories is highly recommended to take full advantage of this package
API Setup
- Install the package :
composer require ercos/cakephp-cypress - Load the plugin in the
src/Application.phpfile :bin/cake plugin load Ercos/CakephpCypress - Extract your database structure (all tables + phinxlog structure + data) as an SQL file.
- Add your SQL file to your project, for example in
/dump/my-database.sql. - Copy
config/.envtoconfig/.env.cypress. IMPORTANT : make sure to set your testing database on both DB_DATABASE (default) and DB_TEST_DATABASE (test) env variables. Add the path to the sql file in the
config/.env.cypressfile, asexport SQL_DUMP_FOLDER="/dump/my-database.sql"Add the following code to your
.htaccessfile :
RewriteEngine On
SetEnvIf x-cypress-header true cypress
...
This will allow the plugin to detect if the request is coming from Cypress and automatically switch to the testing database (according to .env.cypress)
- In your
config/bootstrap.phpfile, add the logic to load the.env.cypressfile if the request is coming from Cypress. You should find the code part to update right after the imports in theconfig/bootstrap.phpfile.
if (!env('APP_NAME')) {
$dotenv = '';
if (env('cypress') && file_exists(CONFIG . '.env.cypress')) {
$dotenv = new Loader([CONFIG . '.env.cypress']);
} elseif (file_exists(CONFIG . '.env')) {
$dotenv = new Loader([CONFIG . '.env']);
}
if ($dotenv instanceof Loader) {
$dotenv->parse()
->toEnv();
}
}
- In your
src/Application.php, add the following code to thebootstrap()function :
if (env('cypress')) {
require TESTS . 'bootstrap.php';
}
- In your
tests/bootstrap.phpfile, do not import the bootstrap.php file when Cypress is running as shown :
if (!env('cypress', false)) {
require dirname(__DIR__) . '/config/bootstrap.php';
}
- In your
tests/bootstrap.phpfile, Add the following code to fix the usage of sessions.<br/> Fixate sessionid early on, as php7.2+ does not allow the sessionid to be set after stdout has been written to.if (!env('cypress', false)) { session_id('cli'); }
Front-end setup
npm install cypress --save-dev- Copy the content of the
front-endfolder to the root of the front-end project. - Update the Cypress configuration (depending on your Cypress version, either use the
cypress.jsonorcypress.config.tsfile as an example) - Add your fixtures to enable to database refresh command in
tests/cypress/support/commands.js
body: {
fixtures: [
"app.Users",
...
]
},
Available commands
refreshDatabase: to call in the Cypress before hook. The command drops all the tables, executes the SQL file to create a fresh database, and finally runs the migrations. You can update the file from time to time to speed up the test setup time.create: to create a record in the database using factories. For example,cy.create('Users', { username: 'John' })will create a user with the username "John".login: to login a user. You need to complete theloginfunction intests/cypress/support/commands.jsto make it work with your front-end project.
Usage
- Create tests in
tests/cypress/integrationfolder - Run
npx cypress open