Download the PHP package antonioprimera/phpunit-custom-assertions without Composer

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

Antonio Primera's Custom PHPUnit Assertions

If you are a serious TDD Enthusiast, you probably got into situations where you needed special assertion methods.

This is a package where I tried to gather a few useful custom assertions for my PHPUnit Tests. This package relies on Laravel's Illuminate\Support and Illuminate\Contracts and is primarily written for Laravel tests, but should work fine also with a bare-bone PHPUnit environment.

For the moment, the following assertion categories are covered:

Installation

Import via Composer:

composer require --dev antonioprimera/phpunit-custom-assertions

Then just use the trait AntonioPrimera\Testing\CustomAssertions in your test cases.

Usage

After including the trait AntonioPrimera\Testing\CustomAssertions in your test case, you can just call any of the assertion methods included in this package, in your tests.

Here's a short example covering most of the assertions in this package:

Because the assertions are written based on basic PHPUnit Framework Constraints, all assertions can be called as static methods:

as instance methods:

or as functions:

Available assertions

Files and folders

assertFileContains(string | array $subString, string $filePath, [callable $processor])

This assertion checks whether a file at the given path $filePath contains a string $subString.

For ease of use, you can check that the file contains several strings, by providing an array of strings as the first argument ($substring).

If you want to process the file contents and the strings, before making the checks, you can provide a callable string processor (a function or a method) as the third argument. The callable receives a string as its argument (the file contents and each of the string to compare) and returns a processed strings, which will be used to make the actual comparison.

For example, if you want to remove all whitespaces (space, tabs, new lines) before comparing, you could use a function like in the example below. This can be very useful if you don't care about the formatting of the file contents.

Because this function is so useful, I added it into a public static method removeWhiteSpaces, so you don't have to write it yourself evey time. This method returns the processor function. You can use it like this:

assertFileContentsEquals(string $expected, string $path, [callable $processor])

This assertion works much like assertFileContains(...) but can only receive a single string as its first argument and will check that the given string is identical with the entire contents of the file. A processor function / callable can also be provided, exactly like in the previous assertion.

assertFilesExist(string | array $filePaths)

This works similar to the original file existence assertion, but it allows you to provide one or more file paths (string | array) and in case of failure it will tell you which of the files don't exist.

assertFoldersExist(string | array $folderPaths)

This works similar to assertDirectoryExists but the assertion has a bette name Folders instead of Directories (seriously, who calls them Directories?) enabling you to provide one or more folder paths (string | array) and in case of failure it will tell you which of the folders don't exist.

Lists / Arrays / Collections

assertListsEqual($expected, $actual, [bool $strict = false])

This assertion will do its best to check whether the actual list is the same as the expected list.

The $expected and $actual parameters can be any iterable type - hence the generic term list:

This will try to compare associative lists (with string keys), indexed lists (with numeric keys), as well as mixed lists (having both string and int keys), ignoring the order in which the indexed items are present.

This assertion will compare also deep / nested lists - this is particularly challenging for nested indexed lists.

To do a strict comparison, set the $strict flag to true. Please be aware that NULL values are compared separately, so they must match another NULL value, regardless of $strict, but anything else, like booleans and their numeric counterparts 0 and 1 will be inter-matched in non-strict mode and can provide false positive assertions.

There are currently a few caveats (they can be solved, but does anybody need this? is it worth the effort?):

  1. Objects, other than lists (Collections / iterable list instances) and Eloquent Models (it's a Laravel thingy), can not be compared properly:
    1. in strict mode the objects must be references to the same instance (spl_ids are compared)
    2. in non-strict mode only the object classes are compared
  2. Callables can't be compared - only that the actual list contains the same number of callables
  3. Resources can't be compared - only that the actual list contains the same number of resources

assertArraysEqual($expected, $actual, [bool $strict = false])

This uses the previous (assertListEqual) assertion, but checks that the 2 lists are actual arrays.

Helpers

expectAssertionToFail(...$messages)

This helper works like an assertion, and it expects that the next (or one of the next) assertion fails. If a message or a list of messages is provided, it also checks that the failure messages contain the given strings.

Because of the way PHPUnit works, a test ends at the first failed assertion, so whatever assertions you write after a failing assertion, they will be ignored (there are ways around it but this is a good challenge for your creativity or StackOverflow searching skills). Having said this, it is recommended that this method is used just before the failing assertion, at the end of a test.

e.g.

Tests

At the moment: 18 tests, 65 assertions.

This package is thoroughly tested and running smoothly.

Future development / Contribution / Support

I plan to add here new assertions as soon as I develop them for my own projects. If you like this package, you can also check out my other open source packages (mostly for Laravel).

This package is open source, so it welcomes any contribution. If you want to contribute with enhancements, new functionality or major changes, before putting any work in, please contact me and let me know what your needs are and what changes you plan to make.


All versions of phpunit-custom-assertions with dependencies

PHP Build Version
Package Version
No informations.
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 antonioprimera/phpunit-custom-assertions contains the following files

Loading the files please wait ....