verdephp / verde
a BDD Style library for your PHP tests
Requires
- ext-runkit7: *
Requires (Dev)
- ergebnis/phpstan-rules: ^0.15.0
- friendsofphp/php-cs-fixer: ^2.16
- nunomaduro/phpinsights: ^1.14
- pestphp/pest: ^0.2.0
- phpstan/phpstan: ^0.12.32
- phpstan/phpstan-strict-rules: ^0.12.2
- phpunit/phpunit: ^9.0
- rector/rector: ^0.7.41
- slevomat/coding-standard: ^6.0@dev
- thecodingmachine/phpstan-strict-rules: ^0.12.0
This package is auto-updated.
Last update: 2025-03-29 01:10:22 UTC
README
a BDD Style Library for your PHP Tests
Verde is a composer library, inspired to Jasmine and Jest, for writing tests with PHPUnit and pest using BDD style syntax, and nicer exceptions messages.
It also offers mocks and spies thanks to the power of runkit7.
Getting Started
composer require "verdephp/verde" --dev
Then start writing your tests:
<?php use function Verde\expect; test('the Answer to Everything', function() { expect(getTheAnswer())->toBe(42); }); // or with PHPUnit: class SimpleTest extends TestCase { public function test_the_answer_to_everything() { expect(getTheAnswer())->toBe(42); } }
Spy
Easy and clear syntax inspired to Jest and Jasmine
<?php use function Verde\expect; test('retrieves the ingredients first and then bake the pizza', function () { $spyGetPizzaIngredients = spyOn('getPizzaIngredients'); $spyBakePizza = spyOn('bakePizza'); // We don't want to make the HTTP request $spyGetPizzaIngredients->mockReturnValue(['Mozzarella', 'Pomodoro']); makePizza('Margherita'); // Here we make sure that the functions are called in the right order expect($spyGetPizzaIngredients)->toHaveBeenCalledBefore($spyBakePizza); // We can also check the arguments passed expect($spyGetPizzaIngredients)->toHaveBeenCalledWith('Margherita'); expect($spyBakePizza)->toHaveBeenCalledWith(['Mozzarella', 'Pomodoro']); });
NOTE: Spies require runkit7 to work!
Mocks
Easy to use syntax for mocking class
<?php use \Verde\expect; use \Verde\func; function theAnswerToEverything(callable $callback) { $callback(42); } test('the mock function is called with the correct argument', function() { $mockFunction = func(); theAnswerToEverything($mockFunction->getCallable()); // We can spy on the mock to see if it has been called and with which argument expect($mockFunction)->toHaveBeenCalledWith(42); })
NOTE: Spies require runkit7 to work!
Documentation
Checkout the documentation
Contributing
Please read the CONTRIBUTING.md file.
Why Verde?
Because being mainly a JavaScript developer, I find more straightforward the Jest/Jasmine syntax over the assert one of PHPUnit.
Why runkit?
To make the spying and mocking experience as simple as the JavaScript world one.
Note: Runkit is needed by the mock
and spy
helpers only.
Verde is open-sourced software by Ceceppa licensed under the MIT License.