Skip to content
This repository has been archived by the owner on Dec 3, 2023. It is now read-only.

[READ-ONLY] Testing made easy

License

Notifications You must be signed in to change notification settings

deprecated-packages/easy-testing

Repository files navigation

Testing Made Easy

Downloads total

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.

About

[READ-ONLY] Testing made easy

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 3

  •  
  •  
  •  

Languages