phpunit-autoprovide icon indicating copy to clipboard operation
phpunit-autoprovide copied to clipboard

Magic helper to autoload CSV, JSON, PHP, XML and YAML data provider in PHPUnit

PHPUnit AutoProvide

Magic helper to autoload CSV, JSON, PHP, XML and YAML data provider in PHPUnit.

Build Status Packagist Version License

Installation

composer require henryruhs/phpunit-autoprovide

Setup

Create the TestCaseAbstract for your testing suite:

<?php
namespace ExampleProject\Tests;

use PHPUnitAutoProvide;

/**
 * TestCaseAbstract
 *
 * @package ExampleProject
 * @category Tests
 */

abstract class TestCaseAbstract extends PHPUnitAutoProvide\TestCaseAbstract
{
	/**
	 * directory of the provider
	 */

	protected $_providerDirectory = 'tests' . DIRECTORY_SEPARATOR . 'provider';

	/**
	 * namespace of the testing suite
	 */

	protected $_testNamespace = __NAMESPACE__;
}

Usage

Extend ExampleTest from TestCaseAbstract and set @dataProvider as needed:

<?php
namespace ExampleProject\Tests;

/**
 * ExampleTest
 *
 * @package ExampleProject
 * @category Tests
 */

class ExampleTest extends TestCaseAbstract
{
	/**
	 * testMethod
	 *
	 * @param string $expect
	 *
	 * @dataProvider autoProvide
	 */

	public function testMethod(string $expect) : void
	{
		$this->assertEquals($expect, 'test');
	}
}

Create the ExampleTest{_testMethod}.{csv|json|php|xml|yml} file:

[
	[
		"test"
	]
]