Make your tests read the same way you speak.
- Write your tests in the same way you speak
- Makes tests easier to understand
- Improves collaboration and communication
Phluent is an assertion library for PHPUnit. It's API design mimics how we speak as humans, this way it's easier for you and your team members to understand what the test is doing.
Compare the two assertions below, the first is using PHPUnit's Assert class and the other example uses Phluent.
self::assertToBeGreaterThan(4, 3)
Expect(4)->toBeGreaterThan(3);
The latter example is much easier to read and understand.
Hello, I'm Nils Haberkamp. I strongly believe that writing better tests makes one a better developer, leads to better communication and collaboration and overall a better software. This package is my take on creating better tests.
use PHPUnit\Framework\TestCase;
use function Phluent\Expect;
class SomeTest extends TestCase {
public function test_one_plus_one_equals_two(): void
{
$result = 1 + 1;
Expect($result)->toBe(2);
}
}
Available assertions
- toBeEmpty
- toBe
- toBeAnInteger
- toBeNegative
- toBeInBetween
- toBePositive
- toBeGreaterThan
- toBeLessThan
- toBeAFloat
- toBeLessThanOrEqual
- toBeGreaterThanOrEqual
- toBeAString
- toBeEmpty
- toStartWith
- toEndWith
- toBeABoolean
- toBeAnArray
- toBeInstanceOf
- toBeNull
- toBeFalse
- toBeTrue
- toContainAllOf
- toContainAnyOf
- toContain
- toHaveALengthOf
- toThrow
- withMessage
- toHaveThrown
- withMessage
First, install the package via composer:
composer require --dev phluent/phluent
Now you need to include phluent in your autoloader. To do this open
the file that bootstraps PHPUnit. (This file is often located under: tests/bootstrap.php
)
Open the file and add the following code:
require dirname(__DIR__) . '/vendor/phluent/phluent/src/Expect.php';
require dirname(__DIR__) . '/vendor/phluent/phluent/src/Act.php';
The installation is now complete, and you're all set.
Requirements:
- PHP 7.4 or higher
- PHPUnit 9.0 or higher
Thank you for taking an interest in contributing to this project. Feel free to open an issue or a pull request if you have any suggestions or improvements.
I want to thank the people who contributed to the following projects. Without them, this project would not exist.
Special thanks to the maintainers and contributors of PHPUnit for creating such an awesome project.