symplify/easy-testing
Utilities for easier PHP unit testing, focused on writing cleaner tests with less boilerplate. Provides helpers and base test cases commonly used in Symplify tools to streamline assertions, fixture handling, and test setup across projects.
composer require symplify/easy-testing --dev
Do you use unit fixture file format?
echo 'content before';
?>
-----
<?php
echo 'content after';
?>
Or in case of no change at all:
echo 'just this content';
The code is separated by -----. Top half of the file is input, the 2nd half is expected output.
It is common to organize test fixture in the test directory:
/tests/SomeTest/Fixture/added_comma.php.inc
/tests/SomeTest/Fixture/skip_alreay_added_comma.php.inc
How this package makes it easy to work with them? 2 classes:
Symplify\EasyTesting\DataProvider\StaticFixtureFinderSymplify\EasyTesting\StaticFixtureSplitter// tests/SomeTest/SomeTest.php
namespace App\Tests\SomeTest;
use Iterator;
use PHPUnit\Framework\TestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SomeTest extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fileInfo): void
{
$inputAndExpected = StaticFixtureSplitter::splitFileInfoToInputAndExpected($fileInfo);
// test before content
$someService = new SomeService();
$changedContent = $someService->process($inputAndExpected->getInput());
$this->assertSame($inputAndExpected->getExpected(), $changedContent);
}
public function provideData(): Iterator
{
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
}
}
Do you need the input code to be in separated files? E.g. to test the file was moved?
Instead of splitFileInfoToInputAndExpected() use splitFileInfoToLocalInputAndExpectedFileInfos():
-$inputAndExpected = StaticFixtureSplitter::splitFileInfoToInputAndExpected(
+$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
$fileInfo
);
Compared to formated method, splitFileInfoToLocalInputAndExpectedFileInfos() will:
use Symplify\EasyTesting\StaticFixtureSplitter;
$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
$fileInfo,
true
);
By default, the StaticFixtureFinder finds only *.php.inc files.
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture');
In case you use different files, e.g. *.twig or *.md, change it in 2nd argument:
use Symplify\EasyTesting\DataProvider\StaticFixtureFinder;
return StaticFixtureFinder::yieldDirectory(__DIR__ . '/Fixture', '*.md');
How to Update Hundreds of Test Fixtures with Single PHPUnit run?
If you change an output of your software on purpose, you might want to update your fixture. Manually? No, from command line:
UPDATE_TESTS=1 vendor/bin/phpunit
UT=1 vendor/bin/phpunit
To make this work, we have to add StaticFixtureUpdater::updateFixtureContent() call to our test case:
use PHPUnit\Framework\TestCase;
use Symplify\EasyTesting\DataProvider\StaticFixtureUpdater;
use Symplify\EasyTesting\StaticFixtureSplitter;
use Symplify\SmartFileSystem\SmartFileInfo;
final class SomeTestCase extends TestCase
{
/**
* @dataProvider provideData()
*/
public function test(SmartFileInfo $fixtureFileInfo): void
{
$inputFileInfoAndExpectedFileInfo = StaticFixtureSplitter::splitFileInfoToLocalInputAndExpectedFileInfos(
$fixtureFileInfo
);
// process content
$currentContent = '...';
// here we update test fixture if the content changed
StaticFixtureUpdater::updateFixtureContent(
$inputFileInfoAndExpectedFileInfo->getInputFileInfo(),
$currentContent,
$fixtureFileInfo
);
}
// data provider...
}
Do you generate large portion of files? Do you want to skip nitpicking tests file by file?
Use assertDirectoryEquals() method to validate the files and their content is as expected.
use PHPUnit\Framework\TestCase;
use Symplify\EasyTesting\PHPUnit\Behavior\DirectoryAssertableTrait;
final class DirectoryAssertableTraitTest extends TestCase
{
use DirectoryAssertableTrait;
public function testSuccess(): void
{
$this->assertDirectoryEquals(__DIR__ . '/Fixture/first_directory', __DIR__ . '/Fixture/second_directory');
}
}
In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.
How can I help you explore Laravel packages today?