dekeysoft/pu-tester

This package is abandoned and no longer maintained. The author suggests using the https://github.com/php-kitchen/code-specs package instead.

BDD extension for PHPUnit that allows to write tests as a specifications in a human-readable format.

v4.0.1 2017-12-01 15:07 UTC

This package is not auto-updated.

Last update: 2019-02-20 18:57:48 UTC


README

logo.png

Build Status Code Coverage Code Quality Latest Stable Version Monthly Downloads Total Downloads License

Code Specs is a PHPUnit plugin with support of Codeception for BDD style Unit tests that allows to writes test in a specification way using human-readable format. Goal of this library is to add a bunch of cool methods for unit testing and show a way of representing unit tests as a behavior specifications of a specific class and a test of specific method as a specification of the method. Tester represents an actor who test you code(yes, like in Codeception - because of Codeception this library exists). See by yourself:

namespace Specs\Unit;

use PHPKitchen\CodeSpecs\Base\Specification;
use PHPKitchen\CodeSpecs\Contract\TestGuy;

/**
 * Specification of {@link IncomeCalculator}
 *
 * @author Dmitry Kolodko <prowwid@gmail.com>
 */
class IncomeCalculatorTest extends Specification {
    private const EXPECTED_TAX_FOR_FIRST_LEVEL_TAX_RULE = 4500;
    private const EXPECTED_TAX_FOR_SECOND_LEVEL_TAX_RULE = 7200;
    private const EXPECTED_TAX_FOR_THIRD_LEVEL_TAX_RULE = 30000;
    private const INCOME_AFTER_APPLYING_FIRST_LEVEL_TAX_RULE = 300000;

    /**
     * @test
     */
    public function calculateTaxBehavior() {
        $clientsPayments = []; // dummy variable, just for example
        $hoursSpentWorking = 160; // dummy variable, just for example
        $service = new IncomeCalculator($clientsPayments, $hoursSpentWorking);
        $I = $this->tester;
        $I->describe('income tax calculations');

        $I->verifyThat('income calculator honors tax rules for different ranges of income', function (TestGuy $I) use ($service) {

            $I->expectThat('for income less that 50 000 calculator use 10% tax rule');
            $I->lookAt('first level income tax');
            $I->seeNumber($service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_FIRST_LEVEL_TAX_RULE);

            $I->expectThat('for income between 50 000 and 100 000 calculator use 12% tax rule');
            $I->lookAt('second level income tax');
            $I->seeNumber($service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_SECOND_LEVEL_TAX_RULE);

            $I->expectThat('for income more than 100 000 calculator use 20% tax rule');
            $I->lookAt('third level income tax');
            $I->seeNumber('income tax', $service->calculateTax())
                ->isNotEmpty()
                ->isEqualTo(self::EXPECTED_TAX_FOR_THIRD_LEVEL_TAX_RULE);
        });
    }

    /**
     * @test
     */
    public function calculateWithTaxBehavior() {
        $clientsPayments = []; // dummy variable, just for example
        $hoursSpentWorking = 160; // dummy variable, just for example
        $service = new IncomeCalculator($clientsPayments, $hoursSpentWorking);

        $I = $this->tester;
        $I->describe('income calculation');

        $I->lookAt('income tax');

        $I->expectThat('calculator calculates income with tax using 10% tax rule for income less that 50 000');

        $I->seeNumber($service->calculateWithTax())
            ->isNotEmpty()
            ->isEqualTo(self::INCOME_AFTER_APPLYING_FIRST_LEVEL_TAX_RULE);
    }
}

CodeSpecs also decorates errors output so, for example, if "IncomeCalculator" service from example above will incorrectly calculate income the error output will include following message(example of output in PHPStorm):

picture alt

Requirements

PHP >= 7.1 is required.

PHPUnit >= 6.0 is required but PHPUnit >= 6.2 is recommended.

Getting Started

Run the following command to add CodeSpecs to your project's composer.json. See Packagist for specific versions.

composer require --dev php-kitchen/code-specs

Or you can copy this library from:

Then you can use CodeSpecs in your test simply extending from Specification class. Example:

use PHPKitchen\CodeSpecs\Base\Specification;

class YourTest extends Specification {

    /**
     * @test
     */
    public function myMethodBehavior() {
        $I = $this->tester;
        ......
        $I->lookAt('my dummy variable');
        $I->seeBool(true)->isFalse();
    }
}

or by using "TesterInitialization" trait in your test case

use PHPUnit\Framework\TestCase;
use PHPKitchen\CodeSpecs\Mixin\TesterInitialization;

class YourTest extends TestCase {
    use TesterInitialization;

    /**
     * @test
     */
    public function myMethodBehavior() {
        $I = $this->tester;
        ......
        $I->lookAt('my dummy variable');
        $I->seeBool(true)->isFalse();
    }
}

Note: If you want to use CodeSpecs with Codeception read Codeception integration guide

For additional information and guides go to the project documentation See changes log for information about changes in releases and update guide for information about upgrading to a next major version.

Contributing

If you want to ask any questions, suggest improvements or just to talk with community and developers, join our server at Discord Read organization contributing rules for additional information.

Spreading the Word

Acknowledging or citing CodeSpecs is as important as direct contributions.

If you are using CodeSpecs as part of an OpenSource project, a way to acknowledge it is to use a special badge in your README: Tested By

If your code is hosted at GitHub, you can place the following in your README.md file to get the badge:

[![CodeSpecs](https://img.shields.io/badge/Tested_By-CodeSpecs-brightgreen.svg?style=flat)](https://github.com/php-kitchen/code-specs)

or use regular HTML:

<a href="https://github.com/php-kitchen/code-specs"><img src="https://img.shields.io/badge/Tested_By-CodeSpecs-brightgreen.svg" alt="Tested By"></a>