The package provides data for autotests from php-files with returned arrays, and from any text files (json, xml, txt...). The package works with PHPUnit, Codeception and Pest frameworks.
composer require yuraplohov/test-data-provider
The package contains one class - Yuraplohov\TestDataProvider\Provider. This class has four public methods:
- basePath(string $basePath): self - sets the base path to the directory with data.
- getPHPUnitCases(array $caseDirs): array - retrieves an array of test cases in PHPUnit framework format.
- getCodeceptionCases(array $caseDirs): array - retrieves an array of test cases in Codeception framework format.
- get(string $path): mixed - retrieves data from a directory, file, or array element in a php-file.
Php files with data for all these methods should be like this:
<?php
return [
// some large array
];
Also, all three methods can work with any text files (json, xml, txt...). The content of such files is returned as a string without any transformations.
The 'tests' directory structure for getPHPUnitCases() and getCodeceptionCases() methods could be:
- tests
- Example_Test
- ExampleTets.php
- data
- case_1
- input.json
- expected.php
- case_2
- input.json
- expected.php
- case_1
- Example_Test
If the base path is not specified, the 'data' directory must be at the same level as your test class. Directories with cases are placed in the 'data' directory. You can name case directories whatever you want, but the 'data' directory must always be named like this if you don't specify any base path with basePath().
/**
* @test
* @dataProvider someProvider
*/
public function it_gets_some_result(array $case)
{
$sut = new SomeClass;
$this->assertEquals($case['expected'], $sut->someMethod($case['input']));
}
/**
* @return array
*/
public function someProvider(): array
{
return (new Provider)->getPHPUnitCases([
'case_1',
'case_2',
]);
}
/**
* @param UnitTester $I
* @param \Codeception\Example $example
* @dataProvider someProvider
*/
public function it_gets_some_result(UnitTester $I, \Codeception\Example $example)
{
$sut = new SomeClass;
$I->assertEquals($example['expected'], $sut->someMethod($example['input']));
}
/**
* @return array
*/
protected function someProvider()
{
return (new Provider)->getCodeceptionCases([
'case_1',
'case_2',
]);
}
This method can be used with any framework and with any structure of the 'data' directory.
Tests directory structure example:
- tests
- Example_Test
- ExampleTets.php
- data
- service
- input.json
- settings.php
- expected.php
- service
- Example_Test
Examples of method calling for this structure:
(new Provider)->get('service'); // retrieves array with all content of the 'data/service' directory.
(new Provider)->get('service/input'); // retrieves content of the file 'data/service/input.json'.
(new Provider)->get('expected'); // retrieves array from the 'data/expected.php' file.
(new Provider)->get('service/settings.foo.bar'); // retrieves value of the 'bar' array element.
// foo.bar - array elements hierarchy in the 'settings.php' file
Usage in PHPUnit:
/** @test */
public function it_gets_some_result()
{
$dp = new Provider;
$sut = new SomeClass($dp->get('service/settings'));
$this->assertEquals($dp->get('expected'), $sut->someMethod($dp->get('service/input')));
}
./vendor/bin/phpunit
The MIT License (MIT).