Skip to content

Registering custom Comparator #80

Description

@tzunghaor

Hi, I've found a problem with using custom Comparator, and made a workaround - I'm writing to make others aware of this issue.

In my tests I registered a custom Comparator using SebastianBergmann\Comparator\Factory (as I read in the phpunit doc), but it didn't work with php-actions/phpunit

As I found out, the phpunit PHAR prefixes the SebastianBergmann namespace with PHPUnitPhar

I did the following workaround in my phpunit bootstrap.php - this way the tests run both using the phpunit in vendor directory, and with the phpunit PHAR. (Though it might be better to drop the phpunit from vendor, and always use phpunit PHAR.)

use SebastianBergmann\Comparator\Factory as ComparatorFactory;
use PHPUnitPHAR\SebastianBergmann\Comparator\Factory as PharComparatorFactory;

if (class_exists(PharComparatorFactory::class)) {
    $comparatorFactory = PharComparatorFactory::class;

    // alias PHPUnit classes used in my custom TzunghaorObjectComparator 
    class_alias(PHPUnitPHAR\SebastianBergmann\Comparator\Comparator::class, 'SebastianBergmann\Comparator\Comparator');
    class_alias(PHPUnitPhar\SebastianBergmann\Comparator\ComparisonFailure::class, 'SebastianBergmann\Comparator\ComparisonFailure');
} elseif (class_exists(ComparatorFactory::class)) {
    $comparatorFactory = ComparatorFactory::class;
} else {
    throw new RuntimeException('SebastianBergmann\Comparator\Factory not found');
}

$comparatorFactory::getInstance()->register(new TzunghaorObjectComparator());

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions