Coder Social home page Coder Social logo

test's Introduction

The Test Package Build Status

Latest Stable Version Total Downloads Latest Unstable Version License

This package is a collection of tools that make some of the jobs of unit testing easier.

TestHelper

Joomla\Test\TestHelper is a static helper class that can be used to take some of the pain out of repetitive tasks whilst unit testing with PHPUnit.

Mocking

There are two methods that help with PHPUnit mock objects.

TestHelper::assignMockCallbacks

This helper method provides an easy way to configure mock callbacks in bulk.

use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
	public function testFoo()
	{
		// Create the mock.
		$mockFoo = $this->getMock(
			'Foo',
			// Methods array.
			array(),
			// Constructor arguments.
			array(),
			// Mock class name.
			'',
			// Call original constructor.
			false
		);

		$mockCallbacks = array(
			// 'Method Name' => <callback>
			'method1' => array('\mockFoo', 'method1'),
			'method2' => array($this, 'mockMethod2'),
		);

		TestHelper::assignMockCallbacks($mockFoo, $this, $mockCallbacks);
	}

	public function mockMethod2($value)
	{
		return strtolower($value);
	}
}

TestHelper::assignMockReturns

This helper method provides an easy way to configure mock returns values in bulk.

use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
	public function testFoo()
	{
		// Create the mock.
		$mockFoo = $this->getMock(
			'Foo',
			// Methods array.
			array(),
			// Constructor arguments.
			array(),
			// Mock class name.
			'',
			// Call original constructor.
			false
		);

		$mockReturns = array(
			// 'Method Name' => 'Canned return value'
			'method1' => 'canned result 1',
			'method2' => 'canned result 2',
			'method3' => 'canned result 3',
		);

		TestHelper::assignMockReturns($mockFoo, $this, $mockReturns);
	}
}

Reflection

There are three methods that help with reflection.

TestHelper::getValue

The TestHelper::getValue method allows you to get the value of any protected or private property.

use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
	public function testFoo()
	{
		$instance = new \Foo;

		// Get the value of a protected `bar` property.
		$value = TestHelper::getValue($instance, 'bar');
	}
}

This method should be used sparingly. It is usually more appropriate to use PHPunit's assertAttribute* methods.

TestHelper::setValue

The TestHelper::setValue method allows you to set the value of any protected or private property.

use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
	public function testFoo()
	{
		$instance = new \Foo;

		// Set the value of a protected `bar` property.
		TestHelper::setValue($instance, 'bar', 'New Value');
	}
}

This method is useful for injecting values into an object for the purpose of testing getter methods.

TestHelper::invoke

The TestHelper::invoke method allow you to invoke any protected or private method. After specifying the object and the method name, any remaining arguments are passed to the method being invoked.

use Joomla\Test\TestHelper;

class FooTest extends \PHPUnit_Framework_TestCase
{
	public function testFoo()
	{
		$instance = new \Foo;

		// Invoke the protected `bar` method.
		$value1 = TestHelper::invoke($instance, 'bar');

		// Invoke the protected `bar` method with arguments.
		$value2 = TestHelper::invoke($instance, 'bar', 'arg1', 'arg2');
	}
}

Installation via Composer

Add "joomla/test": "~2.0" to the require block in your composer.json and then run composer install.

{
	"require": {
		"joomla/test": "~2.0"
	}
}

Alternatively, you can simply run the following from the command line:

composer require joomla/test "~2.0"

test's People

Contributors

achal-aggarwal avatar brianteeman avatar dongilbert avatar eddieajau avatar hackwar avatar ianmacl avatar joomla-jenkins avatar mbabker avatar nibra avatar pborreli avatar philetaylor avatar photodude avatar rdeutz avatar realityking avatar wilsonge avatar

Stargazers

 avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.