Skip to content

Commit d648206

Browse files
committedAug 9, 2019
Add compatibility trait for PHPUnit constraint classes
1 parent 4f7032a commit d648206

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed
 

‎ConstraintTrait.php

+28
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+
}

‎Legacy/ConstraintTraitForV6.php

+99
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+
}

‎Legacy/ConstraintTraitForV7.php

+68
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)