verdephp/verde

为您的PHP测试提供行为驱动开发(BDD)风格的库

0.1.0 2020-07-05 16:30 UTC

README

Verde Assertion failed example

GitHub Workflow Status (master)

为您的PHP测试提供行为驱动开发(BDD)风格的库

Verde 是一个由 Composer 驱动的库,灵感来源于 Jasmine 和 Jest,使用 BDD 风格语法和更友好的异常信息来编写使用 PHPUnitpest 的测试。

它还提供 mocksspies,归功于 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 只由 mockspy 辅助函数需要。

Verde 是由 Ceceppa 开源的软件,许可协议为 MIT 许可证