verdephp / verde
为您的PHP测试提供行为驱动开发(BDD)风格的库
0.1.0
2020-07-05 16:30 UTC
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: 2024-09-29 23:25:49 UTC
README
为您的PHP测试提供行为驱动开发(BDD)风格的库
Verde 是一个由 Composer 驱动的库,灵感来源于 Jasmine 和 Jest,使用 BDD 风格语法和更友好的异常信息来编写使用 PHPUnit 和 pest 的测试。
它还提供 mocks 和 spies,归功于 runkit7 的强大功能。
入门
composer require "verdephp/verde" --dev
然后开始编写您的测试
<?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); } }
间谍
灵感来源于 Jest 和 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']); });
注意:间谍需要 runkit7 才能工作!
模拟
用于模拟类的简单易用的语法
<?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); })
注意:间谍需要 runkit7 才能工作!
文档
查看 文档
贡献
请阅读 CONTRIBUTING.md 文件。
为什么选择 Verde?
因为我主要是一个 JavaScript 开发者,我发现 Jest/Jasmine 的语法比 PHPUnit 的 assert 语法更直观。
为什么选择 runkit?
为了让间谍和模拟的体验尽可能简单,就像 JavaScript 世界一样。
注意:Runkit 只由 mock
和 spy
辅助函数需要。