2017 © Pedro Peláez
 

library test-helpers

Classes designed to assist with PHPUnit testing

image

chadicus/test-helpers

Classes designed to assist with PHPUnit testing

  • Monday, December 5, 2016
  • by chadicus
  • Repository
  • 0 Watchers
  • 2 Stars
  • 3,589 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 2 Open issues
  • 4 Versions
  • 12 % Grown

The README.md

Chadicus Test Helpers

Latest Stable Version Latest Unstable Version License, (*1)

Total Downloads Daily Downloads Monthly Downloads, (*2)

Requirements

Test Helpers requires PHP 7.3 (or later)., (*3)

Composer

To add the library as a local, per-project dependency use Composer! Simply add a dependency on chadicus/test-helpers to your project's composer.json file such as:, (*4)

composer require --dev chadicus/test-helpers

NOTE: test-helpers should never be used in production. They are meant for testing enviornments only., (*5)

Documentation

PHP docs for the project can be found here., (*6)

Contact

Developers may be contacted at:, (*7)

Project Build

With a checkout of the code get Composer in your PATH and run:, (*8)

composer install
composer run test
composer run lint

\Chadicus\FunctionRegistry

Some internal PHP functions are documented to return certain values on failure. If you're a meticulous programmer you want to account for these return values in your code and respond to them accordingly., (*9)

class MyClass
{
    public function doSomething()
    {
        $curl = curl_init();
        if ($curl === false) {
            throw new \Exception('curl_init() failed');
        }

        //do something with $curl ...
    }
}

A meticulous programmer may also want to ensure their unit test code coverage is 100%., (*10)

One way to accomplish this is to use @codeCoverageIgnore annotations, (*11)

class MyClass
{
    public function doSomething()
    {
        $curl = curl_init();
        if ($curl === false) {
            //@codeCoverageIgnoreStart
            throw new \Exception('curl_init() failed');
            //@codeCoverageIgnoreEnd
        }

        //do something with $curl ...
    }
}

This gets us the code coverage but the code isn't really tested., (*12)

The FunctionRegistry class alows you to mock an internal PHP function, (*13)

class MyClassTest extends \PHPUnit_Framework_TestCase
{
    protected function setUp()
    {
        // prepare the curl functions for mocking
        \Chadicus\FunctionRegistry::reset(__NAMESPACE__, array('curl'));
    }

    /**
     * @expectedExceptionMessage curl_init() failed
     */
    public function testCurlInitFails()
    {
        \Chadicus\FunctionRegistry::set(
            __NAMESPACE__,
            'curl_init',
            function () {
                return false;
            }
        );

        $myClass = new MyClass();

        // this will call our custom curl_init function
        $myClass->doSomething();
    }
}

For functions and constants, PHP will fall back to global functions or constants if a namespaced function or constant does not exist. It is because of this behavior that we can mock internal functions., (*14)

The Versions

05/12 2016

dev-master

9999999-dev

Classes designed to assist with PHPUnit testing

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

test mock phpunit functions

05/12 2016

v2.0.0

2.0.0.0

Classes designed to assist with PHPUnit testing

  Sources   Download

MIT

The Requires

  • php ^5.6 || ^7.0

 

The Development Requires

test mock phpunit functions

15/09 2016

v1.0.1

1.0.1.0

Classes designed to assist with PHPUnit testing

  Sources   Download

MIT

The Requires

  • php >=5.4

 

The Development Requires

test mock phpunit functions

30/04 2015

v1.0.0

1.0.0.0

Classes designed to assist with PHPUnit testing

  Sources   Download

MIT

The Requires

  • php ~5.4

 

The Development Requires

test mock phpunit functions