symplify / easy-testing
Testing made easy
Fund package maintenance!
tomasvotruba
www.paypal.me/rectorphp
Installs: 7 183 458
Dependents: 19
Suggesters: 0
Security: 0
Stars: 42
Watchers: 2
Forks: 2
Type:symfony-bundle
Requires
- php: >=8.1
- nette/utils: ^3.2
- symfony/console: ^6.2
- symfony/dependency-injection: 6.1.*
- symfony/finder: ^6.2
- symplify/package-builder: ^11.2
- symplify/smart-file-system: ^11.2
- symplify/symplify-kernel: ^11.1
Requires (Dev)
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/extension-installer: ^1.2
- phpunit/phpunit: ^9.5.26
- rector/rector: ^0.15.10
- symplify/easy-ci: ^11.1
- symplify/easy-coding-standard: ^11.1
- symplify/package-builder: ^11.2
- symplify/phpstan-extensions: ^11.1
- symplify/symplify-kernel: ^11.2
- tomasvotruba/unused-public: ^0.0.34
- dev-main / 11.2.x-dev
- 11.1.26
- 11.1.25
- 11.1.24
- 11.1.23
- 11.1.22
- 11.1.21
- 11.1.20
- 11.1.19
- 11.1.18
- 11.1.17
- 11.1.16
- 11.1.15
- 11.1.14
- 11.1.13
- 11.1.12
- 11.1.11
- 11.1.10
- 11.1.9
- 11.1.8
- 11.1.7
- 11.1.6
- 11.1.5
- 11.1.4
- 11.1.3
- 11.1.2
- 11.1.1
- 11.1.0
- 11.0.9
- 11.0.8
- 11.0.7
- 11.0.6
- 11.0.5
- 11.0.4
- 11.0.3
- 11.0.2
- 11.0.1
- 11.0.0
- 10.3.3
- 10.3.2
- 10.3.1
- 10.3.0
- 10.2.11
- 10.2.10
- 10.2.9
- 10.2.8
- 10.2.7
- 10.2.6
- 10.2.5
- 10.2.4
- 10.2.3
- 10.2.2
- 10.2.1
- 10.2.0
- 10.1.4
- 10.1.3
- 10.1.2
- 10.1.1
- 10.1.0
- 10.0.25
- 10.0.24
- 10.0.23
- 10.0.22
- 10.0.21
- 10.0.20
- 10.0.19
- 10.0.18
- 10.0.17
- 10.0.16
- 10.0.15
- 10.0.14
- 10.0.13
- 10.0.12
- 10.0.11
- 10.0.10
- 10.0.9
- 10.0.8
- 10.0.7
- 10.0.6
- 10.0.5
- 10.0.4
- 10.0.3
- 10.0.2
- 10.0.1
- 10.0.0
- 10.0.0-beta17
- 10.0.0-beta16
- 10.0.0-beta15
- 10.0.0-beta14
- 10.0.0-beta13
- 10.0.0-beta12
- 10.0.0-beta11
- 10.0.0-beta10
- 10.0.0-beta9
- 10.0.0-beta8
- 10.0.0-beta7
- 10.0.0-beta6
- 10.0.0-beta5
- 10.0.0-beta4
- 10.0.0-beta3
- 10.0.0-beta2
- 10.0.0-beta1
- 9.4.70
- 9.4.69
- 9.4.68
- 9.4.67
- 9.4.66
- 9.4.65
- 9.4.64
- 9.4.63
- 9.4.62
- 9.4.61
- 9.4.60
- 9.4.59
- 9.4.58
- 9.4.57
- 9.4.56
- 9.4.55
- 9.4.54
- 9.4.53
- 9.4.52
- 9.4.51
- 9.4.50
- 9.4.49
- 9.4.48
- 9.4.47
- 9.4.46
- 9.4.45
- 9.4.44
- 9.4.43
- 9.4.42
- 9.4.41
- 9.4.40
- 9.4.39
- 9.4.38
- 9.4.37
- 9.4.36
- 9.4.35
- 9.4.34
- 9.4.33
- 9.4.32
- 9.4.31
- 9.4.30
- 9.4.29
- 9.4.28
- 9.4.27
- 9.4.26
- 9.4.25
- 9.4.24
- 9.4.23
- 9.4.22
- 9.4.21
- 9.4.20
- 9.4.19
- 9.4.18
- 9.4.17
- 9.4.16
- 9.4.15
- 9.4.14
- 9.4.13
- 9.4.12
- 9.4.11
- 9.4.10
- 9.4.9
- 9.4.8
- 9.4.7
- 9.4.6
- 9.4.5
- 9.4.4
- 9.4.3
- 9.4.2
- v9.4.1
- v9.4.0
- v9.3.27
- v9.3.26
- v9.3.25
- v9.3.24
- v9.3.23
- v9.3.22
- v9.3.21
- v9.3.20
- v9.3.19
- v9.3.18
- v9.3.17
- v9.3.16
- v9.3.15
- v9.3.14
- v9.3.13
- v9.3.12
- v9.3.11
- v9.3.10
- v9.3.8
- v9.3.6
- v9.3.5
- v9.3.4
- v9.3.3
- v9.3.1
- v9.3.0
- v9.2.24
- v9.2.23
- v9.2.22
- v9.2.21
- v9.2.20
- v9.2.19
- v9.2.18
- v9.2.17
- v9.2.16
- v9.2.15
- v9.2.14
- v9.2.13
- v9.2.12
- v9.2.11
- v9.2.10
- v9.2.9
- v9.2.8
- v9.2.7
- v9.2.6
- v9.2.5
- v9.2.4
- v9.2.3
- v9.2.2
- 9.2.1
- 9.2.0
- 9.1.9
- 9.1.8
- 9.1.7
- 9.1.6
- 9.1.5
- 9.1.4
- 9.1.3
- 9.1.1
- 9.1.0
- 9.0.50
- 9.0.49
- 9.0.48
- 9.0.47
- 9.0.46
- 9.0.45
- 9.0.44
- 9.0.43
- 9.0.42
- 9.0.41
- 9.0.40
- 9.0.39
- 9.0.38
- 9.0.37
- 9.0.36
- 9.0.35
- 9.0.34
- 9.0.33
- 9.0.32
- 9.0.31
- 9.0.30
- 9.0.29
- 9.0.28
- 9.0.27
- 9.0.26
- 9.0.25
- 9.0.24
- 9.0.23
- 9.0.22
- 9.0.21
- 9.0.20
- 9.0.19
- 9.0.18
- 9.0.17
- 9.0.16
- 9.0.15
- 9.0.14
- 9.0.13
- 9.0.12
- 9.0.11
- 9.0.10
- 9.0.9
- 9.0.8
- 9.0.7
- 9.0.6
- 9.0.5
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 9.0.0-rc1
- 9.0.0-BETA9
- 9.0.0-BETA8
- 9.0.0-BETA7
- 9.0.0-BETA6
- 9.0.0-BETA5
- 9.0.0-BETA4
- 9.0.0-BETA3
- 9.0.0-BETA2
- 9.0.0-BETA1
- 8.3.48
- 8.3.47
- 8.3.46
- 8.3.45
- 8.3.44
- 8.3.43
- 8.3.42
- 8.3.41
- 8.3.40
- 8.3.39
- 8.3.38
- 8.3.37
- 8.3.36
- 8.3.35
- 8.3.34
- 8.3.33
- 8.3.32
- 8.3.31
- 8.3.30
- 8.3.29
- 8.3.28
- 8.3.27
- 8.3.26
- 8.3.25
- 8.3.24
- 8.3.23
- 8.3.22
- 8.3.21
- 8.3.20
- 8.3.19
- 8.3.18
- 8.3.17
- 8.3.16
- 8.3.15
- 8.3.14
- 8.3.13
- 8.3.12
- 8.3.11
- 8.3.10
- 8.3.7
- 8.3.6
- 8.3.5
- 8.3.4
- 8.3.3
- 8.3.2
- 8.3.1
- 8.3.0
- 8.2.27
- 8.2.26
- 8.2.25
- 8.2.24
- 8.2.22
- 8.2.21
- 8.2.20
- 8.2.19
- 8.2.18
- 8.2.17
- v8.2.16
- v8.2.15
- v8.2.14
- v8.2.13
- v8.2.12
- v8.2.11
- v8.2.10
- v8.2.9
- v8.2.8
- v8.2.7
- v8.2.6
- v8.2.5
- v8.2.4
- v8.2.3
- v8.2.2
- v8.2.1
- v8.2.0
- v8.1.20
- v8.1.19
- v8.1.18
- v8.1.17
- v8.1.16
- v8.1.15
- v8.1.14
- v8.1.13
- v8.1.12
- v8.1.11
- v8.1.10
- v8.1.9
- v8.1.8
- v8.1.7
- v8.1.6
- v8.1.4
- v8.1.3
- v8.1.2
- v8.1.1
- v8.1.0
This package is auto-updated.
Last update: 2023-12-03 20:18:28 UTC
README
Install
composer require symplify/easy-testing --dev
Usage
Easier working with Fixtures
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\StaticFixtureFinder
Symplify\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'); } }
Features
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:
- separate fixture to input and expected content
- save them both as separated files to temporary path
- optionally autoload the first one, e.g. if you need it for Reflection
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');
Updating Fixture
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... }
Assert 2 Directories by Files and Content
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'); } }
Report Issues
In case you are experiencing a bug or want to request a new feature head over to the Symplify monorepo issue tracker
Contribute
The sources of this package are contained in the Symplify monorepo. We welcome contributions for this package on symplify/symplify.