All Projects → webmozarts → strict-phpunit

webmozarts / strict-phpunit

Licence: MIT license
Enables type-safe comparisons of objects in PHPUnit.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to strict-phpunit

phpunit-randomizer
A PHPUnit extension that allows you to execute your test cases in a random order
Stars: ✭ 52 (+188.89%)
Mutual labels:  phpunit
tester-phpunit
tester runner for phpunit on atom editor
Stars: ✭ 34 (+88.89%)
Mutual labels:  phpunit
noise-php
A starter-kit for your PHP project.
Stars: ✭ 52 (+188.89%)
Mutual labels:  phpunit
phpunit-mink
Library for using Mink in PHPUnit tests. Supports session sharing between tests in a test case.
Stars: ✭ 71 (+294.44%)
Mutual labels:  phpunit
phpci-installer
PHPCI Easy Installer for Laravel Homestead
Stars: ✭ 19 (+5.56%)
Mutual labels:  phpunit
codeigniter-tettei-apps
『CodeIgniter徹底入門』のサンプルアプリケーション(CodeIgniter v3.1版)
Stars: ✭ 26 (+44.44%)
Mutual labels:  phpunit
laravel-survey
Laravel 6 survey app.
Stars: ✭ 39 (+116.67%)
Mutual labels:  phpunit
test-tools
Improves PHPUnit testing productivity by adding a service container and self-initializing fakes
Stars: ✭ 25 (+38.89%)
Mutual labels:  phpunit
asynit
🌠 Asynchronous HTTP Request Testing Library for API or more...
Stars: ✭ 74 (+311.11%)
Mutual labels:  phpunit
config
Config component, strictly typed
Stars: ✭ 14 (-22.22%)
Mutual labels:  strict-types
framework
A PHP framework for rapidly building web applications.
Stars: ✭ 48 (+166.67%)
Mutual labels:  phpunit
test-util
👓 Provides utilities for tests.
Stars: ✭ 15 (-16.67%)
Mutual labels:  phpunit
annotate-pull-request-from-checkstyle
cs2pr - Annotate a GitHub Pull Request based on a Checkstyle XML-report within your GitHub Action
Stars: ✭ 146 (+711.11%)
Mutual labels:  phpunit
php.autotest
autotest for php written in php
Stars: ✭ 19 (+5.56%)
Mutual labels:  phpunit
phpunit-extensions
Extensions for PHPUnit for Humbug
Stars: ✭ 17 (-5.56%)
Mutual labels:  phpunit
concise
✅ Concise is test framework for using plain English and minimal code, built on PHPUnit.
Stars: ✭ 47 (+161.11%)
Mutual labels:  phpunit
Phexecute
Phexecute - Awesome PHP Code Runner
Stars: ✭ 18 (+0%)
Mutual labels:  phpunit
php-finder refactoring-kata
🐘🔍Incomprehensible Finder Refactoring Kata port for PHP
Stars: ✭ 22 (+22.22%)
Mutual labels:  phpunit
php-test-generator
Generate test cases for existing PHP files
Stars: ✭ 47 (+161.11%)
Mutual labels:  phpunit
dusker
Stand alone Laravel Dusk test suit, which do not require Laravel framework itself
Stars: ✭ 28 (+55.56%)
Mutual labels:  phpunit

Strict PHPUnit

Enables type-safe comparisons of objects in PHPUnit.

Problem

PHPUnit has a very powerful comparison system that helps you comparing objects with expected values:

class ValueObject
{
    public ?string $property;
    
    public function __construct(?string $property)
    {
        $this->property = $property;
    }
}

$actual = new ValueObject('foo!');

self::assertEquals(new ValueObject('foo'), $actual);
// => fails with a very helpful error message

This comparison system will give you a meaningful exception that guides you precisely to the problem that caused the assertion to fail. Strings are furthermore diffed so that you see exactly which character of the string causes a mismatch.

PHPUnit compares each scalar property of an object with relaxed types. It is a little more intelligent than using just == under the hood, but still that will not always provide the results you want:

var_dump('Hi' == true);
// => true

self::assertEquals(new ValueObject('Hi'), new ValueObject(true));
// => fails

var_dump('' == null);
// => true

self::assertEquals(new ValueObject(''), new ValueObject(null));
// => succeeds

Solution

This extension enables a comparator for scalar values that fights this problem. With this extension, whenever PHPUnit finds a scalar value during assertEquals() (even recursively within objects or arrays), it will compare the value with ===.

Objects are still not checked for identity, hence you can still construct example objects to compare against.

Error messages stay meaningful.

self::assertEquals(new ValueObject(''), new ValueObject(null));
// => fails with a meaningful error

self::assertEquals(new ValueObject('foo!'), new ValueObject('foo'));
// => fails with a meaningful error

self::assertEquals(new ValueObject('foo!'), new ValueObject('foo!'));
// => succeeds

Installation

The extension can be installed with Composer:

$ composer require --dev webmozarts/strict-phpunit

Add the extension to your phpunit.xml.dist file to enable it:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="vendor/phpunit/phpunit/phpunit.xsd">
    <!-- ... -->
    
    <extensions>
        <extension class="Webmozarts\StrictPHPUnit\StrictPHPUnitExtension"/>
    </extensions>
    
    <!-- ... -->
</phpunit>

Authors

Contribute

Contributions to the package are always welcome!

Note that this repository is a subtree-split of a monorepo and hence read only. PRs will be ported to the (internal) monorepo.

License

All contents of this package are licensed under the MIT license.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].