From 908c8c1f154c198cf2e17d06dae64165f17d9d35 Mon Sep 17 00:00:00 2001 From: Andreas Braun Date: Fri, 9 Aug 2019 09:23:13 +0200 Subject: [PATCH] Add compatibility trait for PHPUnit constraint classes --- .../Bridge/PhpUnit/ConstraintTrait.php | 28 ++++++ .../PhpUnit/Legacy/ConstraintTraitForV6.php | 99 +++++++++++++++++++ .../PhpUnit/Legacy/ConstraintTraitForV7.php | 68 +++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 src/Symfony/Bridge/PhpUnit/ConstraintTrait.php create mode 100644 src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV6.php create mode 100644 src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV7.php diff --git a/src/Symfony/Bridge/PhpUnit/ConstraintTrait.php b/src/Symfony/Bridge/PhpUnit/ConstraintTrait.php new file mode 100644 index 0000000000..64b24ee166 --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/ConstraintTrait.php @@ -0,0 +1,28 @@ + + * + * 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; + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV6.php b/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV6.php new file mode 100644 index 0000000000..64942d8dfe --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV6.php @@ -0,0 +1,99 @@ + + * + * 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 ''; + } +} diff --git a/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV7.php b/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV7.php new file mode 100644 index 0000000000..48c79a76dd --- /dev/null +++ b/src/Symfony/Bridge/PhpUnit/Legacy/ConstraintTraitForV7.php @@ -0,0 +1,68 @@ + + * + * 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 ''; + } +}