2017 © Pedro Peláez
 

library test

A pluf mock object and test utilities

image

pluf/test

A pluf mock object and test utilities

  • Wednesday, December 27, 2017
  • by mostafabarmshory
  • Repository
  • 2 Watchers
  • 0 Stars
  • 1,652 Installations
  • PHP
  • 23 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 13 Versions
  • 15 % Grown

The README.md

Pluf Test

Build Status codecov Maintainability Test Coverage Coverage Status, (*1)

This tutorial assumes that you use PHP 7.3 or PHP 7.4. You will learn how to write simple unit tests as well as how to download and run PlufTest., (*2)

PlufTest is based on PHPUnit 8., (*3)

Install

You can add Pluf/Test as a local, per-project, development-time dependency to your project using Composer:, (*4)

➜ composer require --dev pluf/test ^6

➜ ./vendor/bin/pluftest --version
Pluf/Test 6.0.0 by pluf.ir and contributors.

The example shown above assumes that composer is on your $PATH., (*5)

Your composer.json should look similar to this:, (*6)

{
    "autoload": {
        "classmap": [
            "src/"
        ]
    },
    "require-dev": {
        "pluf/test": "^9"
    }
}

Test Code

suppose there is a code, (*7)

src/Email.php

with content:, (*8)

<?php
declare(strict_types=1);

final class Email
{
    private $email;

    private function __construct(string $email)
    {
        $this->ensureIsValidEmail($email);

        $this->email = $email;
    }

    public static function fromString(string $email): self
    {
        return new self($email);
    }

    public function __toString(): string
    {
        return $this->email;
    }

    private function ensureIsValidEmail(string $email): void
    {
        if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
            throw new InvalidArgumentException(
                sprintf(
                    '"%s" is not a valid email address',
                    $email
                )
            );
        }
    }
}

Create a file:, (*9)

tests/EmailTest.php

And create the test class, (*10)

<?php
declare(strict_types=1);

use PHPUnit\Framework\TestCase;

final class EmailTest extends TestCase
{
    public function testCanBeCreatedFromValidEmailAddress(): void
    {
        $this->assertInstanceOf(
            Email::class,
            Email::fromString('user@example.com')
        );
    }

    public function testCannotBeCreatedFromInvalidEmailAddress(): void
    {
        $this->expectException(InvalidArgumentException::class);

        Email::fromString('invalid');
    }

    public function testCanBeUsedAsString(): void
    {
        $this->assertEquals(
            'user@example.com',
            Email::fromString('user@example.com')
        );
    }
}

The test is ready, (*11)

Note: Do not put tests in a namespace., (*12)

Run the test

./vendor/bin/phpunit --bootstrap vendor/autoload.php tests/EmailTest
PHPUnit 8.0.0 by Sebastian Bergmann and contributors.

...                                                                 3 / 3 (100%)

Time: 70 ms, Memory: 10.00MB

OK (3 tests, 3 assertions)

--bootstrap vendor/autoload.php instructs the PHPUnit command-line test runner to include vendor/autoload.php before the tests are run., (*13)

tests/EmailTest instructs the PHPUnit command-line test runner to execute the tests of the EmailTest class that is declared in tests/EmailTest.php., (*14)

Using tests instead of tests/EmailTest would instruct the PHPUnit command-line test runner to execute all tests found declared in *Test.php sourcecode files in the tests directory., (*15)

Contributing

If you would like to contribute to Pluf, please read the README and CONTRIBUTING documents., (*16)

The most important guidelines are described as follows:, (*17)

All code contributions - including those of people having commit access - must go through a pull request and approved by a core developer before being merged. This is to ensure proper review of all the code., (*18)

Fork the project, create a feature branch, and send us a pull request., (*19)

To ensure a consistent code base, you should make sure the code follows the PSR-2 Coding Standards., (*20)

The Versions

27/12 2017
27/12 2017
06/12 2017

2.1.10

2.1.10.0

A pluf mock object and test utilities

  Sources   Download

MIT

The Requires

 

test mock phpunit pluf

09/11 2017
14/10 2017