Download the PHP package bovigo/assert without Composer

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

bovigo/assert

Provides assertions for unit tests.

Package status

Build Status Coverage Status

Latest Stable Version

Installation

bovigo/assert is distributed as Composer package. To install it as a development dependency of your package use the following command:

composer require --dev "bovigo/assert": "^8.0"

To install it as a runtime dependency for your package use the following command:

composer require "bovigo/assert=^7.0"

Requirements

bovigo/assert 8.x requires at least PHP 8.2.

Why?

The original idea was to explore how a more functional approach to using assertions in unit tests could look like, and if it would make for a better reading of test code. Personally, I found the results convincing enough that I wanted to use it in my own code, so I made a package of it.

Usage

All assertions are written in the same way using functions:

The first parameter is the value to test, and the second is the predicate that should be used to test the value. Additionally, an optional description can be supplied to enhance clarity in case the assertion fails.

In case the predicate fails an AssertionFailure will be thrown with useful information of why the test failed. In case PHPUnit is used AssertionFailure is an instance of \PHPUnit\Framework\AssertionFailedError so it integrates nicely into PHPUnit, yielding a similar test output as PHPUnit's constraints. Here is an example of the output in case of a test failure:

For the sake of brevity below it is assumed the used functions are imported into the current namespace via

List of predicates

This is the list of predicates that are included in bovigo/assert by default.

isNull()

Tests if value is null.

Alias: bovigo\assert\assertNull($value, $description = null)

isNotNull()

Tests that value is not null.

Alias: bovigo\assert\assertNotNull($value, $description = null)

isEmpty()

Tests that value is empty. Empty is defined as follows:

Aliases:

isNotEmpty()

Tests that value is not empty. See isEmpty() for definition of emptyness.

Alias: bovigo\assert\assertNotEmpty($value, $description = null)

isTrue()

Tests that a value is true. The value must be boolean true, no value conversion is applied.

Alias: bovigo\assert\assertTrue($value, $description = null)

isFalse()

Tests that a value is false. The value must be boolean false, no value conversion is applied.

Alias: bovigo\assert\assertFalse($value, $description = null)

equals($expected)

Tests that a value equals the expected value. The optional parameter $delta can be used when equality of float values should be tested and allows for a certain range in which two floats are considered equal.

In case a delta is needed, e.g. for float values, the required delta can be set:

isNotEqualTo($unexpected)

Tests that a value is not equal to the unexpected value. The optional parameter $delta can be used when equality of float values should be tested and allows for a certain range in which two floats are considered equal.

In case a delta is needed, e.g. for float values, the required delta can be set:

isInstanceOf($expectedType)

Tests that a value is an instance of the expected type.

isNotInstanceOf($unexpectedType)

Tests that a value is not an instance of the unexpected type.

isSameAs($expected)

Tests that a value is identical to the expected value. Both values are compared with ===, the according rules apply.

isNotSameAs($unexpected)

Tests that a value is not identical to the unexpected value. Both values are compared with ===, the according rules apply.

isOfSize($expectedSize)

Tests that a value has the expected size. The rules for the size are as follows:

isNotOfSize($unexpectedSize)

Tests that a value does not have the unexpected size. The rules are the same as for isOfSize($expectedSize).

isOfType($expectedType)

Tests that a value is of the expected internal PHP type.

Aliases

Since release 5.0 some alias functions are provided to prevent typos in usages of that function:

isNotOfType($unexpectedType)

Tests that a value is not of the unexpected internal PHP type.

Aliases

Since release 5.0 some alias functions are provided to prevent typos in usages of that function. Please note that some are specific to ensure the code you write with them forms a grammatically valid sentence.

isGreaterThan($expected)

Tests that a value is greater than the expected value.

isGreaterThanOrEqualTo($expected)

Tests that a value is greater than or equal to the expected value.

isLessThan($expected)

Tests that a value is less than the expected value.

isLessThanOrEqualTo($expected)

Tests that a value is less than or equal to the expected value.

contains($needle)

Tests that $needle is contained in value. The following rules apply:

Sometimes it is necessary to differentiate between arrays, Traversables and strings. If a particular type should be enforced it is recommended to combine predicates:

doesNotContain($needle)

Tests that $needle is not contained in value. The rules of contains($needle) apply.

hasKey($key)

Tests that an array or an instance of \ArrayAccess have a key with given name. The key must be either of type integer or string. Values that are neither an array nor an instance of \ArrayAccess are rejected.

doesNotHaveKey($key)

Tests that an array or an instance of \ArrayAccess does not have a key with given name. The key must be either of type integer or string. Values that are neither an array nor an instance of \ArrayAccess are rejected.

containsSubset($other)

Available since release 6.2.0.

Tests that $other contains the value.

matches($pattern)

Tests that a string matches the given pattern of a regular expression. If the value is not a string it is rejected. The test is successful if the pattern yields at least one match in the value.

doesNotMatch($pattern)

Tests that a string does not match the given pattern of a regular expression. If the value is not a string it is rejected. The test is successful if the pattern yields no match in the value.

matchesFormat($format)

Available since release 3.2.0.

Tests that a string matches the given PHP format expression. If the value is not a string it is rejected. The test is successful if the format yields at least one match in the value. The format string may contain the following placeholders:

doesNotMatchFormat($format)

Available since release 3.2.0.

Tests that a string does not match the given PHP format expression. If the value is not a string it is rejected. The test is successful if the pattern yields no match in the value. See above for a list of possible formats.

isExistingFile($basePath = null)

Tests that the value denotes an existing file. If no $basepath is supplied the value must either be an absolute path or a relative path to the current working directory. When $basepath is given the value must be a relative path to this basepath.

isNonExistingFile($basePath = null)

Tests that the value denotes a file which does not exist. If no $basepath is supplied the value must either be an absolute path or a relative path to the current working directory. When $basepath is given the value must be a relative path to this basepath.

isExistingDirectory($basePath = null)

Tests that the value denotes an existing directory. If no $basepath is supplied the value must either be an absolute path or a relative path to the current working directory. When $basepath is given the value must be a relative path to this basepath.

isNonExistingDirectory($basePath = null)

Tests that the value denotes a non-existing directory. If no $basepath is supplied the value must either be an absolute path or a relative path to the current working directory. When $basepath is given the value must be a relative path to this basepath.

startsWith($prefix)

Available since release 1.1.0.

Tests that the value which must be a string starts with given prefix.

doesNotStartWith($prefix)

Available since release 1.1.0.

Tests that the value which must be a string does not start with given prefix.

endsWith($suffix)

Available since release 1.1.0.

Tests that the value which must be a string ends with given suffix.

doesNotEndWith($suffix)

Available since release 1.1.0.

Tests that the value which must be a string does not end with given suffix.

each($predicate)

Available since release 1.1.0.

Applies a predicate to each value of an array or traversable.

Please note that an empty array or traversable will result in a successful test. If it must not be empty use isNotEmpty()->and(each($predicate)):

It can also be used with any callable:

eachKey($predicate)

Available since release 1.3.0.

Applies a predicate to each key of an array or traversable.

Please note that an empty array or traversable will result in a successful test. If it must not be empty use isNotEmpty()->and(eachKey($predicate)):

It can also be used with any callable:

not($predicate)

Reverses the meaning of a predicate.

It can also be used with any callable:

Combining predicates

Each predicate provides both two methods to combine this predicate with another predicate into a new predicate.

and($predicate)

Creates a predicate where both combined predicate must be true so that the combined predicate is true as well. If one of the predicates fails, the combined predicate will fail as well.

It can also be used with any callable:

or($predicate)

Creates a predicate where one of the combined predicates must be true. Only if all predicates fail the combined predicate will fail as well.

It can also be used with any callable:

User defined predicates

To define a predicate to be used in an assertion there are two possibilities:

Use a callable

You can pass anything that is a callable to the assertThat() function:

This will create a predicate which uses PHP's builtin is_nan() function to test the value.

The callable should accept a single value (the value to test, obviously) and must return true on success and false on failure. It is also allowed to throw any exception.

Here is an example with a closure:

Extend bovigo\assert\predicate\Predicate

The other possibility is to extend the bovigo\assert\predicate\Predicate class. You need to implement at least the following methods:

public function test($value)

This method receives the value to test and should return true on success and false on failure. It is also allowed to throw any exception.

public function __toString()

This method must return a proper description of the predicate which fits into the sentences shown when an asssertion fails. These sentences are composed as follows:

Failed asserting that [description of value] [description of predicate].

Additionally, the predicate can influence [description of value] by overriding the describeValue(Exporter $exporter, $value) method.

Instant failure

Available since release 1.2.0.

In case assertions are not enough and the test needs to fail when it reaches a certain point, bovigo\assert\fail($description) can be used to trigger an instant assertion failure:

phpstan and early terminating function calls

Available since release 5.1.0

In case you are using phpstan bovigo/assert provides a config file you can include in your phpstan config so early terminating function calls with fail() are recognized.

Expectations

Available since release 1.6.0

Expectations can be used to check that a specific piece of code does or does not throw an exception or trigger an error. It can also be used to check that after a specific piece of code ran assertions are still true, despite of whether the code in question succeeded or not.

Expectations on exceptions

Note: since release 2.1.0 it is also possible to use expectations with \Error.

Check that a piece of code, e.g. a function or method, throws an exception:

It is also possible to expect any exception, not just a specific one, by leaving out the class name of the exception:

Since release 2.1.0 it is possible to verify that exactly a given exception was thrown:

This will perform an assertion with isSameAs($exception) for the thrown exception.

Additionally checks on the thrown exception can be performed:

The following checks on the exception are possible:

Of course you can also check that a specific exception did not occur:

By leaving out the exception name you ensure that the code doesn't throw any exception at all:

In case any of these expectations fail an AssertionFailure will be thrown.

Expectations on errors

Available since release 2.1.0

Check that a piece of code, e.g. a function or method, triggers an error:

It is also possible to expect any error, not just a specific one, by leaving out the error level:

Additionally checks on the triggered error can be performed:

The following checks on the exception are possible:

In case any of these expectations fail an AssertionFailure will be thrown.

Expectations on state after a piece of code was executed

Sometimes it may be useful to assert that a certain state exists after some piece of code is executed, regardless of whether this execution succeeds.

It is possible to combine this with expectations on whether an exception is thrown or not:

Verify output of a function or method

Available since release 2.1.0

When a function or method utilizes echo it can be cumbersome to check if it prints the correct output. For this, the outputOf() function was introduced:

The first parameter is a callable which prints some output, the second is any predicate which will than be applied to the output. outputOf() takes care of enabling and disabling output buffering to catch the output.

FAQ

How can I access a property of a class or object for the assertions?

Unlike PHPUnit bovigo/assert does not provide means to assert that a property of a class fullfills a certain constraint. If the property is public you can pass it directly into the assertThat() function as a value. In any other case bovigo/assert does not support accessing protected or private properties. There's a reason why they are protected or private, and a test should only be against the public API of a class, not against their inner workings.


All versions of assert with dependencies

PHP Build Version
Package Version
Requires php Version ^8.2
sebastian/comparator Version ^5.0
sebastian/exporter Version ^5.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 bovigo/assert contains the following files

Loading the files please wait ....