calgamo/test

This package is abandoned and no longer maintained. No replacement package was suggested.

Utility class/trait for test. Supports PHPUnit

0.5.1 2019-11-18 23:56 UTC

This package is auto-updated.

Last update: 2019-11-19 03:03:39 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Code Climate Total Downloads

Description

Calgamo/Test is a library of test.

Feature

Demo

Exsample 1: get private/protected property

use Calgamo\Test\TestCase\PhpUnitTestCase;

class TestTarget
{
    private $foo;

    public function __construct()
    {
        $this->foo = 123;
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testGetPrivateProperty()
    {
        $object = new TestTarget();

        // get private property from object
        $value = $this->getPrivatePropertyWithoutException($object, 'foo');

        // assert
        $this->assertSame(123, $value);
    }
}

Exsample 2: call private/protected method

class TestTarget
{
    /** @noinspection PhpUnusedPrivateMethodInspection */
    private function sayHello()
    {
        return 'Hello';
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallPrivateMethod()
    {
        $object = new TestTarget();

        // call private method
        $ret = $this->callPrivateMethodWithoutException($object, 'sayHello');

        // assert if result is not right
        $this->assertSame('Hello', $ret);
    }
}

Exsample 3: call private/protected static method

class TestTarget
{
    /** @noinspection PhpUnusedPrivateMethodInspection */
    private static function sayBye()
    {
        return 'Bye';
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallPrivateMethod()
    {
        // call private static method
        $ret = $this->callPrivateStaticMethodWithoutException(TestTarget::class, 'sayBye');

        // assert if result is not right
        $this->assertSame('Bye', $ret);
    }
}

Exsample 4: callback if exception is thrown in specified callback

class TestTarget
{
    /**
     * @throws \Exception
     */
    public function doSomething()
    {
        throw new \Exception('Emergency');
    }
}

class MyTest extends PhpUnitTestCase
{
    public function testCallbackIfExceptionIsThrown()
    {
        $this->callbackIfExceptionIsThrown(
            function(){
                (new TestTarget)->doSomething();
            },
            function(\Throwable $e){
                $this->fail('something wrong: ' . $e->getMessage());
            });
    }
}

Usage

Requirement

PHP 7.1 or later

Installing calgamo/test

The recommended way to install calgamo/test is through Composer.

composer require calgamo/test

After installing, you need to require Composer's autoloader:

require 'vendor/autoload.php';

License

MIT

Author

stk2k

Disclaimer

This software is no warranty.

We are not responsible for any results caused by the use of this software.

Please use the responsibility of the your self.