Skip to content

Commit a7b22e8

Browse files
committed
Support PHPUnit 8 and PHPUnit 9 in constraint compatibility trait
1 parent 83a1178 commit a7b22e8

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
@@ -16,6 +16,16 @@
1616
*/
1717
trait ConstraintTraitForV7
1818
{
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+
1929
public function count(): int
2030
{
2131
return $this->doCount();
@@ -40,29 +50,4 @@ protected function matches($other): bool
4050
{
4151
return $this->doMatches($other);
4252
}
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-
}
6853
}

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)