Essence is a very flexible BDD style assertion framework for PHP that fits into existing PHPUnit projects nicely

Related tags

Testing essence
Overview

Essence 1.5.1 Build Status

Essence is a very flexible BDD style assertion framework for PHP that fits into existing PHPUnit projects nicely.

Installation

composer require --dev bound1ess/essence

The Idea

In most PHP testing frameworks, you are tied to concrete matcher names (e.g., assertEqual, shouldHaveType). I don't like that. That's why I created Essence.

Usage

In order to run a matcher you need to specify it in the query string. So what is a query string? Have a look:

should_have_length_of(10); # => "someValue should have length of 10" expect(123)->toBeAbove(120); # => "expect 123 to be above 120" $elements = [1, 2, 3, 4, 5]; these($elements)->values->should_contain(5); # => "these elements should contain a value '5'" expect(null)->not()->to()->beNull(); => "expect NULL not to be NULL"">
this("someValue")->should_have_length_of(10); # => "someValue should have length of 10"
expect(123)->toBeAbove(120); # => "expect 123 to be above 120"

$elements = [1, 2, 3, 4, 5];
these($elements)->values->should_contain(5); # => "these elements should contain a value '5'"

expect(null)->not()->to()->beNull(); => "expect NULL not to be NULL"

Yes, Essence is smart enough to handle all these cases just as you would expect it to do. So, how do you build a query string (or assertion)?

  1. Decide if you need to configure a matcher you plan to use. As for now there are two only matchers that can be used in configuration mode - ValuesMatcher and KeysMatcher.
  2. Determine what matcher you will need to use to get the job done. Is it ThrowMatcher, or, say, RespondMatcher?
  3. Add some links to make the assertion readable.
  4. Choose an appropriate entry point (expect, this, these etc).
  5. Pass a proper value and arguments.
  6. If you want to, add ->go() to immediately perform validation. I'll tell you more about that later.

Configuration

First of all, Essence leverages the singleton pattern to persist all its important data during the runtime. It means that this expression will always be equal to true:

spl_object_hash(essence()) == spl_object_hash(essence());

You can configure Essence by using configure method:

"Your\Custom\AssertionException", ]); });">
essence()->configure(function($config) {
    return array_merge($config, [
        "exception" => "Your\Custom\AssertionException",
    ]);
});

Available configuration options:

Name Possible value
exception fully qualified class name (as a string)
implicit_validation a boolean value (true or false)
links an array (won't be merged automatically)
matchers an associative array name => aliases (won't be merged automatically)

Explicit and implicit, validateAll and PHPUnit extension

If you don't want to write ->validate() or ->go() every time, you can enable implicit validation:

true, ]; });">
essence()->configure(function() {
    return [
        "implicit_validation" => true,
    ];
});

It will validate the last (previous) assertion when you create a new one. Or, even better, just use the PHPUnit extension as shown below:

class MyTestCase extends Essence\Extensions\PhpunitExtension
{

    // Your assertions here.
}

It'll do the job for you, no need to configure anything or call go/validate.

Verbose mode

This line of code will throw an Essence\Exceptions\AssertionException by default:

expect(10)->to_be_equal_to(15)->validate(); // You can also use "go" instead of "validate".

However, if you pass true to validate/go, Essence will dump all important data and just exit.

expect(10)->to_be_equal_to(15)->validate(true);
vendor/bin/phpunit
# ........
Value: 10
Arguments:
  #1: 15

Cheatsheet

Entry points

  • essence
  • it
  • that
  • this
  • these
  • those

Links

  • of
  • have
  • be
  • at
  • to

Matchers

Name Aliases
TypeMatcher an, a, type
ContainMatcher contain, include
PositiveMatcher ok, fine
TrueMatcher true
FalseMatcher false
NullMatcher null
EmptyMatcher empty
EqualMatcher equal
LikeMatcher like
AboveMatcher above
BelowMatcher below
LeastMatcher least
MostMatcher most
WithinMatcher within
LengthMatcher length
MatchMatcher match
KeysMatcher key, keys
ValuesMatcher value, values
ThrowMatcher throw, raise
RespondMatcher respond
CloseMatcher close

License

The MIT License (MIT).

Development

The Makefile contains all sorts of useful tasks.

Running tests

make run-tests

Creating code coverage report

make coverage-report coverage-report-server

Building documentation

make build-docs docs-server
You might also like...
Add mocking capabilities to Pest or PHPUnit

This repository contains the Pest Plugin Mock. The Mocking API can be used in regular PHPUnit projects. For that, you just have to run the following c

A sample RESTful API in Laravel with PHPunit test.

Laravel PHP Framework URL | URI | Action |

PHPUnit Application Architecture Test

PHPUnit Application Architecture Test Idea: write architecture tests as well as feature and unit tests Installation Install via composer composer requ

PHPUnit to Pest Converter

PestConverter PestConverter is a PHP library for converting PHPUnit tests to Pest tests. Before use Before using this converter, make sure your files

Allows the running of PHPUnit within ExpressionEngine

EE Unit Tests EE Unit Tests is an Add-on for ExpressionEngine that allows developers to execute unit tests from the Command Line. EE Unit Tests uses P

PHPUnit extension for database interaction testing.

This extension is no longer maintained DbUnit PHPUnit extension for database interaction testing. Installation Composer If you use Composer to manage

Mock implementation of the Translation package, for testing with PHPUnit

PoP Translation - Mock Mock implementation of the Translation package, for testing with PHPUnit Install Via Composer composer require getpop/translati

Magento PHPUnit Integration

Magento PHPUnit Integration Magento is a quite complex platform without built in unit test suite, so the code is not oriented on running tests over it

The objective of ParaTest is to support parallel testing in PHPUnit

The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in your project and start using it with no additional bootstrap or configurations!

Event driven BDD test framework for PHP

The highly extensible, highly enjoyable, PHP testing framework. Read more at peridot-php.github.io or head over to the wiki. Building PHAR Peridot's p

Peridot 327 Jan 5, 2023
BDD test framework for PHP

BDD test framework for PHP, inspired by Jasmine and RSpec. Features a familiar syntax, and a watch command to automatically re-run specs during develo

Daniel St. Jules 286 Nov 12, 2022
The most powerful and flexible mocking framework for PHPUnit / Codeception.

AspectMock AspectMock is not an ordinary PHP mocking framework. With the power of Aspect Oriented programming and the awesome Go-AOP library, AspectMo

Codeception Testing Framework 777 Dec 12, 2022
Provides generic data providers for use with phpunit/phpunit.

data-provider Installation Run composer require --dev ergebnis/data-provider Usage This package provides the following generic data providers: Ergebni

null 25 Jan 2, 2023
Qase-phpunit - Qase TMS PHPUnit reporter.

Qase TMS PHPUnit reporter Publish results simple and easy. How to integrate composer require qase/phpunit-reporter Example of usage The PHPUnit report

Qase TMS 6 Nov 24, 2022
Mockery - Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library

Mockery Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its c

Mockery 10.3k Jan 1, 2023
vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with any unit test framework, like PHPUnit or SimpleTest.

vfsStream vfsStream is a stream wrapper for a virtual file system that may be helpful in unit tests to mock the real file system. It can be used with

null 1.4k Dec 23, 2022
PHP libraries that makes Selenium WebDriver + PHPUnit functional testing easy and robust

Steward: easy and robust testing with Selenium WebDriver + PHPUnit Steward is a set of libraries made to simplify writing and running robust functiona

LMC s.r.o. 219 Dec 17, 2022
:computer: Parallel testing for PHPUnit

ParaTest The objective of ParaTest is to support parallel testing in PHPUnit. Provided you have well-written PHPUnit tests, you can drop paratest in y

null 2k Dec 31, 2022
Rector upgrades rules for PHPUnit

Rector Rules for PHPUnit See available PHPUnit rules Install composer require rector/rector-phpunit Use Sets To add a set to your config, use Rector\P

RectorPHP 34 Dec 27, 2022