Download the PHP package ciareis/bypass without Composer

On this page you can find all versions of the php package ciareis/bypass. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package bypass

Bypass for PHP

About | Installation | Writing Tests | Examples | Credits | Inspired

US flag in base64 BR flag in base64

Tests GitHub tag (latest by date) Packagist Downloads Packagist License Last Updated


About

Bypass for PHP provides a quick way to create a custom HTTP Server to return predefined responses to client requests.

This is useful in tests when your application makes requests to external services, and you need to simulate different situations like returning specific data or unexpected server errors.

Just open a Bypass server and configure your application/service to reach Bypass instead of the real-world API end point.


Installation

📌 Bypass requires PHP 8.0+.

To install via composer, run the following command:


Video demo

Pest and Bypass

Writing Tests

Content

🔥 Check out full code examples here section.

1. Open a Bypass Server

To write a test, first open a Bypass server:

Bypass will always run at http://localhost listening to a random port number.

To specify a custom port, just pass it in the argument (int) $port.

2. Bypass URL and Port

You can retrieve the Bypass server URL using getBaseUrl().

If you need to retrieve only the port number, use the getPort() method:

3. Routes

Bypass provides two types of routes: The Standard Route to return a text body content and the File Route, which returns a binary file.

When running your test suit, you should pass the URL created with Bypass to your service. In this way, you will make the service you are testing reach Bypass instead of reaching the real-world API end point.

3.1 Standard Route

The method addRoute() accepts the following parameters:

Parameter Type Description
HTTP Method string $method HTTP Request Method (GET/POST/PUT/PATCH/DELETE)
URI string $uri URI to be served by Bypass
Status int $status HTTP Status Code to be returned by Bypass (default: 200)
Body string\|array $body Body to be served by Bypass (optional)
Times int $times How many times the route should be called (default: 1)

3.2 File Route

The method addFileRoute() accepts the following parameters:

Parameter Type Description
HTTP Method string $method HTTP Request Method (GET/POST/PUT/PATCH/DELETE)
URI string $uri URI to be served by Bypass
Status int $status HTTP Status Code to be returned by Bypass (default: 200)
File binary $file Binary file to be served by Bypass
Times int $times How many times the route should be called (default: 1)

3.3 Bypass Serve and Route Helpers

Bypass provides you with convenient shortcuts to the most-common-used HTTP requests.

These shortcuts are called "Route Helpers" and are served automatically at a random port using Bypass::serve() without the need to call Bypass::open().

In the next example, Bypasss serves two routes: A URL accessible by method GET returning a JSON body with status 200, and a second route URL accessible by method GET and returning status 404.

Route Helpers

You may find below the list of Route Helpers.

Route Helper Default Method HTTP Status Body Common usage
Route::ok() GET 200 optional (string|array) Request was successful
Route::created() POST 201 optional (string|array) Response to a POST request which resulted in a creation
Route::badRequest() POST 400 optional (string|array) Something can't be parsed (ex: wrong parameter)
Route::unauthorized() GET 401 optional (string|array) Not logged in
Route::forbidden() GET 403 optional (string|array) Logged in but trying to request a restricted resource (without permission)
Route::notFound() GET 404 optional (string|array) URL or resource does not exist
Route::notAllowed() GET 405 optional (string|array) Method not allowed
Route::validationFailed() POST 422 optional (string|array) Data sent does not satisfy validation rules
Route::tooMany() GET 429 optional (string|array) Request rejected due to server limitation
Route::serverError() GET 500 optional (string|array) General indication that something is wrong on the server side

You may also adjust the helpers to your needs by passing arguments:

Parameter Type Description
URI string $uri URI to be served by Bypass
Body string\|array $body Body to be served by Bypass (optional)
HTTP Method string $method HTTP Request Method (GET/POST/PUT/PATCH/DELETE)
Times int $times How many times the route should be called (default: 1)

In the example below, you can see the Helper Route::badRequest using method GET instead of its default method POST.

📝 Note: Custom routes can be created using a Standard Route in case something you need is not covered by the Helpers.

4. Asserting Route Calling

Sometimes you may need to assert that a route was called at least one or multiple times.

The method assertRoutes() will return a RouteNotCalledException if a route was NOT called as many times as defined in the $times parameter.

If you need to assert that a route is NOT being called by your service, set the parameter $times = 0

5. Stop or shut down

Bypass will automatically stop its server once your test is done running.

The Bypass server can be stopped or shut down at any point with the following methods:

To stop: $bypass->stop();

To shut down: $bypass->down();

Examples

Use case

To better illustrate Bypass usage, imagine you have to write a test for a service that calculates the total game score of a given username.

The score is obtained by making an external request to a fictitious API at emtudo-games.com/v1/score/::USERNAME::. The API returns HTTP Status 200 and a JSON body with a list of games:

Quick Test Examples

Click below to see code snippets for Pest PHP and PHPUnit.

Pest PHP
PHPUnit

Test Examples

📚 See Bypass being used in complete tests with Pest PHP and PHPUnit for the GithubRepoService demo service.

Credits

And a special thanks to @DanSysAnalyst

Inspired

Code inspired by Bypass


All versions of bypass with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
illuminate/support Version ^9.0 || ^10.0 || ^11.0
symfony/process Version ^5.2.0 || ^6.0 || ^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package ciareis/bypass contains the following files

Loading the files please wait ....