Skip to content

Commit 121f426

Browse files
committed
feature #33078 Add compatibility trait for PHPUnit constraint classes (alcaeus)
This PR was merged into the 4.4 branch. Discussion ---------- Add compatibility trait for PHPUnit constraint classes | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | - | License | MIT | Doc PR | - Similar to the SetUpTearDownTrait, this trait removes the pain of dealing with method signature changes. The `PHPUnit\Framework\Constraint\Constraint` class added return type hints in PHPUnit 7 which this trait will take care of. Commits ------- 908c8c1 Add compatibility trait for PHPUnit constraint classes
2 parents 32b2a52 + 908c8c1 commit 121f426

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit;
13+
14+
use PHPUnit\Framework\Constraint\Constraint;
15+
use ReflectionClass;
16+
17+
$r = new ReflectionClass(Constraint::class);
18+
if (\PHP_VERSION_ID < 70000 || !$r->getMethod('matches')->hasReturnType()) {
19+
trait ConstraintTrait
20+
{
21+
use Legacy\ConstraintTraitForV6;
22+
}
23+
} else {
24+
trait ConstraintTrait
25+
{
26+
use Legacy\ConstraintTraitForV7;
27+
}
28+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
use SebastianBergmann\Exporter\Exporter;
15+
16+
/**
17+
* @internal
18+
*/
19+
trait ConstraintTraitForV6
20+
{
21+
/**
22+
* @return int
23+
*/
24+
public function count()
25+
{
26+
return $this->doCount();
27+
}
28+
29+
/**
30+
* @return string
31+
*/
32+
public function toString()
33+
{
34+
return $this->doToString();
35+
}
36+
37+
/**
38+
* @param mixed $other
39+
*
40+
* @return string
41+
*/
42+
protected function additionalFailureDescription($other)
43+
{
44+
return $this->doAdditionalFailureDescription($other);
45+
}
46+
47+
/**
48+
* @return Exporter
49+
*/
50+
protected function exporter()
51+
{
52+
return $this->exporter;
53+
}
54+
55+
/**
56+
* @param mixed $other
57+
*
58+
* @return string
59+
*/
60+
protected function failureDescription($other)
61+
{
62+
return $this->doFailureDescription($other);
63+
}
64+
65+
/**
66+
* @param mixed $other
67+
*
68+
* @return bool
69+
*/
70+
protected function matches($other)
71+
{
72+
return $this->doMatches($other);
73+
}
74+
75+
private function doAdditionalFailureDescription($other)
76+
{
77+
return '';
78+
}
79+
80+
private function doCount()
81+
{
82+
return 1;
83+
}
84+
85+
private function doFailureDescription($other)
86+
{
87+
return $this->exporter()->export($other).' '.$this->toString();
88+
}
89+
90+
private function doMatches($other)
91+
{
92+
return false;
93+
}
94+
95+
private function doToString()
96+
{
97+
return '';
98+
}
99+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\PhpUnit\Legacy;
13+
14+
/**
15+
* @internal
16+
*/
17+
trait ConstraintTraitForV7
18+
{
19+
public function count(): int
20+
{
21+
return $this->doCount();
22+
}
23+
24+
public function toString(): string
25+
{
26+
return $this->doToString();
27+
}
28+
29+
protected function additionalFailureDescription($other): string
30+
{
31+
return $this->doAdditionalFailureDescription($other);
32+
}
33+
34+
protected function failureDescription($other): string
35+
{
36+
return $this->doFailureDescription($other);
37+
}
38+
39+
protected function matches($other): bool
40+
{
41+
return $this->doMatches($other);
42+
}
43+
44+
private function doAdditionalFailureDescription($other): string
45+
{
46+
return '';
47+
}
48+
49+
private function doCount(): int
50+
{
51+
return 1;
52+
}
53+
54+
private function doFailureDescription($other): string
55+
{
56+
return $this->exporter()->export($other).' '.$this->toString();
57+
}
58+
59+
private function doMatches($other): bool
60+
{
61+
return false;
62+
}
63+
64+
private function doToString(): string
65+
{
66+
return '';
67+
}
68+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy