Skip to content
Advertisement

How to assert 2 directories are identical in PHPunit?

I have 2 directories, multiple files, basically text formats.

  • They should be equal.
  • I need to know what files are missing/extra (diff will).

What method should I use?

Advertisement

Answer

2020 Open-Source Package Answer

PHPUnit is still missing this feature, and we needed it to simplify generator tests. So we made a small package symplify/easy-testing that covers it.

The package:

  • compares files by their relivate name
  • compares their content

1. Install

composer require symplify/easy-testing --dev

2. Use

Add DirectoryAssertableTrait trait to your test case and use like this:

<?php

use PHPUnitFrameworkTestCase;
use SymplifyEasyTestingPHPUnitBehaviorDirectoryAssertableTrait;

final class SomeTest extends TestCase
{
    use DirectoryAssertableTrait;

    public function testSuccess(): void
    {
        $this->assertDirectoryEquals(__DIR__ . '/first_directory', __DIR__ . '/second_directory');
    }
}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement