Skip to content

Commit d5477cd

Browse files
committed
bug #38681 [PHPUnitBridge] Support PHPUnit 8 and PHPUnit 9 in constraint compatibility trait (alcaeus)
This PR was merged into the 4.4 branch. Discussion ---------- [PHPUnitBridge] Support PHPUnit 8 and PHPUnit 9 in constraint compatibility trait | Q | A | ------------- | --- | Branch? | 5.x | Bug fix? | not really | New feature? | yes | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | License | MIT This expands the compatibility layer for PHPUnit constraints by supporting PHPUnit 8 and PHPUnit 9. Support for the latter requires also adding the `evaluate` method to the trait, as this method gets a signature change in PHPUnit 9. Commits ------- f79ad80009 Support PHPUnit 8 and PHPUnit 9 in constraint compatibility trait
2 parents c4eed56 + a7b22e8 commit d5477cd

File tree

6 files changed

+213
-26
lines changed

6 files changed

+213
-26
lines changed

ConstraintTrait.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,19 @@ trait ConstraintTrait
2020
{
2121
use Legacy\ConstraintTraitForV6;
2222
}
23-
} else {
23+
} elseif ($r->getProperty('exporter')->isProtected()) {
2424
trait ConstraintTrait
2525
{
2626
use Legacy\ConstraintTraitForV7;
2727
}
28+
} elseif (\PHP_VERSION < 70100 || !$r->getMethod('evaluate')->hasReturnType()) {
29+
trait ConstraintTrait
30+
{
31+
use Legacy\ConstraintTraitForV8;
32+
}
33+
} else {
34+
trait ConstraintTrait
35+
{
36+
use Legacy\ConstraintTraitForV9;
37+
}
2838
}

Legacy/ConstraintLogicTrait.php

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 ConstraintLogicTrait
18+
{
19+
private function doEvaluate($other, $description, $returnResult)
20+
{
21+
$success = false;
22+
23+
if ($this->matches($other)) {
24+
$success = true;
25+
}
26+
27+
if ($returnResult) {
28+
return $success;
29+
}
30+
31+
if (!$success) {
32+
$this->fail($other, $description);
33+
}
34+
35+
return null;
36+
}
37+
38+
private function doAdditionalFailureDescription($other): string
39+
{
40+
return '';
41+
}
42+
43+
private function doCount(): int
44+
{
45+
return 1;
46+
}
47+
48+
private function doFailureDescription($other): string
49+
{
50+
return $this->exporter()->export($other).' '.$this->toString();
51+
}
52+
53+
private function doMatches($other): bool
54+
{
55+
return false;
56+
}
57+
58+
private function doToString(): string
59+
{
60+
return '';
61+
}
62+
}

Legacy/ConstraintTraitForV6.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@
1818
*/
1919
trait ConstraintTraitForV6
2020
{
21+
/**
22+
* @return bool|null
23+
*/
24+
public function evaluate($other, $description = '', $returnResult = false)
25+
{
26+
return $this->doEvaluate($other, $description, $returnResult);
27+
}
28+
2129
/**
2230
* @return int
2331
*/
@@ -86,6 +94,25 @@ private function doCount()
8694
return 1;
8795
}
8896

97+
private function doEvaluate($other, $description, $returnResult)
98+
{
99+
$success = false;
100+
101+
if ($this->matches($other)) {
102+
$success = true;
103+
}
104+
105+
if ($returnResult) {
106+
return $success;
107+
}
108+
109+
if (!$success) {
110+
$this->fail($other, $description);
111+
}
112+
113+
return null;
114+
}
115+
89116
private function doFailureDescription($other)
90117
{
91118
return $this->exporter()->export($other).' '.$this->toString();

Legacy/ConstraintTraitForV7.php

Lines changed: 10 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,16 @@
1818
*/
1919
trait ConstraintTraitForV7
2020
{
21+
use ConstraintLogicTrait;
22+
23+
/**
24+
* @return bool|null
25+
*/
26+
public function evaluate($other, $description = '', $returnResult = false)
27+
{
28+
return $this->doEvaluate($other, $description, $returnResult);
29+
}
30+
2131
public function count(): int
2232
{
2333
return $this->doCount();
@@ -51,29 +61,4 @@ protected function matches($other): bool
5161
{
5262
return $this->doMatches($other);
5363
}
54-
55-
private function doAdditionalFailureDescription($other): string
56-
{
57-
return '';
58-
}
59-
60-
private function doCount(): int
61-
{
62-
return 1;
63-
}
64-
65-
private function doFailureDescription($other): string
66-
{
67-
return $this->exporter()->export($other).' '.$this->toString();
68-
}
69-
70-
private function doMatches($other): bool
71-
{
72-
return false;
73-
}
74-
75-
private function doToString(): string
76-
{
77-
return '';
78-
}
7964
}

Legacy/ConstraintTraitForV8.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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 ConstraintTraitForV8
18+
{
19+
use ConstraintLogicTrait;
20+
21+
/**
22+
* @return bool|null
23+
*/
24+
public function evaluate($other, $description = '', $returnResult = false)
25+
{
26+
return $this->doEvaluate($other, $description, $returnResult);
27+
}
28+
29+
public function count(): int
30+
{
31+
return $this->doCount();
32+
}
33+
34+
public function toString(): string
35+
{
36+
return $this->doToString();
37+
}
38+
39+
protected function additionalFailureDescription($other): string
40+
{
41+
return $this->doAdditionalFailureDescription($other);
42+
}
43+
44+
protected function failureDescription($other): string
45+
{
46+
return $this->doFailureDescription($other);
47+
}
48+
49+
protected function matches($other): bool
50+
{
51+
return $this->doMatches($other);
52+
}
53+
}

Legacy/ConstraintTraitForV9.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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 ConstraintTraitForV9
18+
{
19+
use ConstraintLogicTrait;
20+
21+
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
22+
{
23+
return $this->doEvaluate($other, $description, $returnResult);
24+
}
25+
26+
public function count(): int
27+
{
28+
return $this->doCount();
29+
}
30+
31+
public function toString(): string
32+
{
33+
return $this->doToString();
34+
}
35+
36+
protected function additionalFailureDescription($other): string
37+
{
38+
return $this->doAdditionalFailureDescription($other);
39+
}
40+
41+
protected function failureDescription($other): string
42+
{
43+
return $this->doFailureDescription($other);
44+
}
45+
46+
protected function matches($other): bool
47+
{
48+
return $this->doMatches($other);
49+
}
50+
}

0 commit comments

Comments
 (0)