Skip to content

Add compatibility trait for PHPUnit constraint classes #33078

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/Symfony/Bridge/PhpUnit/ConstraintTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit;

use PHPUnit\Framework\Constraint\Constraint;
use ReflectionClass;

$r = new ReflectionClass(Constraint::class);
if (\PHP_VERSION_ID < 70000 || !$r->getMethod('matches')->hasReturnType()) {
trait ConstraintTrait
{
use Legacy\ConstraintTraitForV6;
}
} else {
trait ConstraintTrait
{
use Legacy\ConstraintTraitForV7;
}
}
99 changes: 99 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV6.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

use SebastianBergmann\Exporter\Exporter;

/**
* @internal
*/
trait ConstraintTraitForV6
{
/**
* @return int
*/
public function count()
{
return $this->doCount();
}

/**
* @return string
*/
public function toString()
{
return $this->doToString();
}

/**
* @param mixed $other
*
* @return string
*/
protected function additionalFailureDescription($other)
{
return $this->doAdditionalFailureDescription($other);
}

/**
* @return Exporter
*/
protected function exporter()
{
return $this->exporter;
}

/**
* @param mixed $other
*
* @return string
*/
protected function failureDescription($other)
{
return $this->doFailureDescription($other);
}

/**
* @param mixed $other
*
* @return bool
*/
protected function matches($other)
{
return $this->doMatches($other);
}

private function doAdditionalFailureDescription($other)
{
return '';
}

private function doCount()
{
return 1;
}

private function doFailureDescription($other)
{
return $this->exporter()->export($other).' '.$this->toString();
}

private function doMatches($other)
{
return false;
}

private function doToString()
{
return '';
}
}
68 changes: 68 additions & 0 deletions src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\PhpUnit\Legacy;

/**
* @internal
*/
trait ConstraintTraitForV7
{
public function count(): int
{
return $this->doCount();
}

public function toString(): string
{
return $this->doToString();
}

protected function additionalFailureDescription($other): string
{
return $this->doAdditionalFailureDescription($other);
}

protected function failureDescription($other): string
{
return $this->doFailureDescription($other);
}

protected function matches($other): bool
{
return $this->doMatches($other);
}

private function doAdditionalFailureDescription($other): string
{
return '';
}

private function doCount(): int
{
return 1;
}

private function doFailureDescription($other): string
{
return $this->exporter()->export($other).' '.$this->toString();
}

private function doMatches($other): bool
{
return false;
}

private function doToString(): string
{
return '';
}
}