Download the PHP package rybakit/phpunit-extras without Composer

On this page you can find all versions of the php package rybakit/phpunit-extras. 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 phpunit-extras

PHPUnit Extras

Quality Assurance

This repository contains functionality that makes it easy to create custom attributes and expectations and use them with the PHPUnit framework. In other words, with this library, your tests may look like this:

Here:

Table of contents

Installation

Depending on which functionality you use, you may also need to install these packages:

To use version-related requirements:

To use the package requirement: Composer 2 is required for the built-in package version lookup used by the package requirement.

To use expression-based requirements and/or expectations:

To install everything in one command, run:

Attributes

PHPUnit supports a variety of attributes, the full list of which can be found in the PHPUnit manual. With this library, you can easily expand this list by using one of the following options:

Inheriting from the base test case class

Using a trait

Registering an extension

You can then use attributes provided by the library or created by yourself.

Requirements

The library comes with the following requirements:

Condition

Format:

where <condition> is an arbitrary expression that should evaluate to true. By default, you can refer to the following superglobal variables in expressions: cookie, env, get, files, post, request and server.

Example:

You can also define your own variables in expressions:

For a custom requirement, define its attribute and a Requirement that handles that attribute class:

Register the requirement in your test case builder with $builder->addRequirement(new FeatureRequirement()), then use #[RequiresFeature('new-checkout')]. The requirement gets the typed attribute, test target, and placeholder resolver. Return null to run the test; return a message to skip it.

Constant

Format: #[RequiresConstant('<constant-name>')] where <constant-name> is the constant name.

Example:

Package

Format: #[RequiresPackage('<package-name> [<version-constraint>]')] where <package-name> is the required package name and <version-constraint> is a Composer-style version constraint. See the Composer documentation for supported constraint formats.

Example:

Placeholders

Placeholders let you include values that depend on the target test in string arguments to custom attributes. A placeholder is any text surrounded by %. An error is thrown for unknown placeholders.

Below is a list of the placeholders available by default:

TargetClass

Example:

In the example above, %target_class% is replaced with FoobarTest, and %target_class_full% is replaced with App\Tests\FoobarTest.

TargetMethod

Example:

In the example above, %target_method% is replaced with Foobar, and %target_method_full% is replaced with testFoobar.

TmpDir

Example:

In the example above, %tmp_dir% is replaced with the result of the sys_get_temp_dir() call.

Creating your own attribute

As an example, let's implement a #[Sql(...)] attribute. First, create a processor class named SqlProcessor:

The processor declares which attribute class it handles, resolves placeholders in its SQL, and calls PDO::exec(). An attribute such as #[Sql('TRUNCATE TABLE foo')] is equivalent to $this->conn->exec('TRUNCATE TABLE foo').

Next, create the attribute class. Its class is the key the processor registers for:

The processor can use the placeholder resolver it receives to replace %table_name% with a unique table name for a specific test method or class. This lets you use dynamic table names instead of hard-coded ones:

The only thing left is to register our new processor:

After that, all classes that extend App\Tests\TestCase can use #[Sql(...)].

If no processor is registered for an attribute class, the library throws an InvalidAttributeException.

As mentioned earlier, you can also register attributes through PHPUnit extensions. To do this, override the createAttributeProcessorBuilder() method in your AttributeExtension class:

Then register your extension:

To change the default connection settings, pass the new DSN value as a parameter:

For more information on configuring extensions, please refer to the PHPUnit manual.

Expectations

PHPUnit provides several methods for setting expectations about code under test. The most commonly used are the expectException* and expectOutput* methods. This library also lets you create custom expectations.

Usage example

As an example, let's create an expectation that verifies the code under test creates a file. Let's call it FileCreatedExpectation:

To use this expectation, extend PHPUnitExtras\TestCase (recommended) or include the PHPUnitExtras\Expectation\Expectations trait in your test case:

Then use the expectation as shown below:

For convenience, you can put this statement in a separate method and group your expectations into a trait:

Advanced example

The Symfony ExpressionLanguage component lets you create expectations with more complex verification rules.

As an example, let's implement the expectSelectStatementToBeExecutedOnce() method mentioned above. First, create an expression context that collects statistics on SELECT statement calls:

Now create a trait which holds all our statement expectations:

And finally, include that trait in your test case class:

For inspiration and more examples of expectations take a look at the tarantool/phpunit-extras package.

Testing

Before running tests, the development dependencies must be installed:

Then, to run all the tests:

License

The library is released under the MIT License. See the bundled LICENSE file for details.


All versions of phpunit-extras with dependencies

PHP Build Version
Package Version
Requires composer-runtime-api Version ^2.0
php Version ^8.2
phpunit/phpunit Version ^10.5
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 rybakit/phpunit-extras contains the following files

Loading the files please wait ...