hyvor / laravel-e2e
Helpers for Laravel E2E testing
Installs: 2 958
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 1
Forks: 1
Open Issues: 0
Requires
- php: ^8.2 || ^8.3
- doctrine/dbal: ^3.6
Requires (Dev)
- orchestra/testbench: ^9.0
- pestphp/pest: ^2.6
- phpstan/phpstan: ^1.10
This package is auto-updated.
Last update: 2024-11-03 11:06:59 UTC
README
This package is a collection of helpers routes for Laravel E2E testing with frameworks like Playwright.
It adds the following endpoints to your application in local
and testing
environments:
POST /_testing/artisan
- Run an artisan commandPOST /_testing/truncate
- Truncate all tablesPOST /_testing/factory
- Create a model using factoriesPOST /_testing/query
- Run a database queryPOST /_testing/select
- Run a database select queryPOST /_testing/function
- Call a PHP function (or static class method)
Installation
You can install the package via composer:
composer require --dev hyvor/laravel-e2e
Usage
Example Usage with Javascript
fetch('/_testing/artisan', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(INPUT_DATA), })
Run artisan commands
POST _testing/artisan
endpoint allows you to run artisan commands from your tests. For example, you can run php artisan migrate:fresh
before each test. It accepts two parameters:
command
- The artisan command to runparameters
- The parameters to pass to the command (array/object, optional)
{ "command": "migrate:fresh", "parameters": ["--seed"] }
This endpoint returns the exit code and the output of the command in JSON format:
{ "code": 0, "output": "" }
Truncate all tables
Truncating tables is faster than running migrate:fresh
command in small sized databases. You can use POST /_testing/truncate
endpoint to truncate all tables. It accepts an optional connections
parameter to truncate tables in specific connections. If the connections
parameter is not set, it truncates tables in the default connection.
{ "connections": [] // optional }
Create a model using factories
You can use POST /_testing/factory
endpoint to create a model using factories. It accepts the following parameters:
model
- The model class name (if the model class name starts withApp\Models\
, you can omit it)count
- The number of models to create (optional, default: 1). If count is set (even if it's 1), it returns an array of models. Otherwise, it returns a single model.attributes
- The attributes to set (optional)
The following example creates a single App\Models\User
model with the name
attribute set to John Doe
, and returns it in JSON format:
{ "model": "User", "attributes": { "name": "John Doe" } }
The following example creates 5 App\Database\Models\User
models and returns them as an array in JSON format:
{ "model": "App\\Database\\Models\\User", "count": 5 }
Run a database query
You can use POST /_testing/query
endpoint to run a database query. It accepts the following parameters:
query
- The query to runconnection
- The database connection to use (optional)
{ "query": "UPDATE users SET name = 'John Doe' WHERE id = 1", "connection": "mysql" }
Run a database select query
You can use POST /_testing/select
endpoint to run a database select query. It accepts the following parameters:
query
- The query to runconnection
- The database connection to use (optional)
It returns the result as an array of objects in JSON format.
{ "query": "SELECT * FROM users WHERE id = 1", "connection": "mysql" }
Call a PHP function or static class method
Use the POST /_testing/function
endpoint to call a PHP function or a static class method. It accepts the following parameters:
function
- Function name to callargs
- Array of arguments to send to the function
Function's return value will be returned (JSON-encoded) from this endpoint.
Examples:
Arguments as an array:
{ "function": "fullName", "args": ["Supun", "Wimalasena"] }
This calls a function named fullName
with two arguments.
Named arguments:
{ "function": "fullName", "args": { "first": "Supun", "last": "Wimalasena" } }
Static methods:
{ "function": "namespace\\class::method", "args": [] }