Skip to content

Commit 1e62c99

Browse files
soyukaGregoireHebertdunglas
authored
Merge 2.5 onto master (#3713)
* fix prophecy trait (#3700) * fix prophecy trait * Support multiple phpunit version ArraySubset (#3702) * fix: restore coverage * Fix phpstan * Remove php-code-coverage dependency * Support multiple phpunit version ArraySubset Co-authored-by: soyuka <[email protected]> Co-authored-by: Grégoire Hébert <[email protected]> Co-authored-by: Kévin Dunglas <[email protected]>
1 parent f1a26db commit 1e62c99

File tree

7 files changed

+181
-85
lines changed

7 files changed

+181
-85
lines changed

.github/workflows/ci.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ jobs:
137137
phpstan-
138138
continue-on-error: true
139139
- name: Run PHPStan analysis
140+
env:
141+
SYMFONY_PHPUNIT_VERSION: '9.2'
140142
run: vendor/bin/phpstan analyse --no-progress --no-interaction --ansi
141143

142144
phpunit:

phpstan.neon.dist

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,20 @@ parameters:
99
container_xml_path: tests/Fixtures/app/var/cache/test/appAppKernelTestDebugContainer.xml
1010
constant_hassers: false
1111
bootstrapFiles:
12-
- vendor/bin/.phpunit/phpunit-9.2-0/vendor/autoload.php
12+
- vendor/bin/.phpunit/phpunit/vendor/autoload.php
13+
# We're aliasing classes for phpunit in this file, it needs to be added here see phpstan/#2194
14+
- src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubset.php
1315
- tests/Fixtures/app/AppKernel.php
1416
excludes_analyse:
1517
- tests/Fixtures/app/var/cache
1618
# The Symfony Configuration API isn't good enough to be analysed
1719
- src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php
20+
# Phpstan runs on phpunit > 9, a signature changed in this file
21+
- src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubsetLegacy.php
1822
# Imported code (temporary)
1923
- src/Bridge/Symfony/Bundle/Test/BrowserKitAssertionsTrait.php
2024
- tests/Bridge/Symfony/Bundle/Test/WebTestCaseTest.php
25+
- tests/ProphecyTrait.php
2126
earlyTerminatingMethodCalls:
2227
PHPUnit\Framework\Constraint\Constraint:
2328
- fail

src/Bridge/Symfony/Bundle/Test/Constraint/ArraySubset.php

Lines changed: 6 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -13,94 +13,16 @@
1313

1414
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;
1515

16-
use PHPUnit\Framework\Constraint\Constraint;
1716
use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure;
1817
use SebastianBergmann\Comparator\ComparisonFailure as LegacyComparisonFailure;
1918

2019
if (!class_exists(ComparisonFailure::class)) {
21-
class_alias(LegacyComparisonFailure::class, ComparisonFailure::class);
20+
class_alias(LegacyComparisonFailure::class, 'PHPUnit\SebastianBergmann\Comparator\ComparisonFailure');
2221
}
2322

24-
/**
25-
* Constraint that asserts that the array it is evaluated for has a specified subset.
26-
*
27-
* Uses array_replace_recursive() to check if a key value subset is part of the
28-
* subject array.
29-
*
30-
* Imported from dms/phpunit-arraysubset-asserts, because the original constraint has been deprecated.
31-
*
32-
* @copyright Sebastian Bergmann <[email protected]>
33-
* @copyright Rafael Dohms <[email protected]>
34-
*
35-
* @see https://github.com/sebastianbergmann/phpunit/issues/3494
36-
*/
37-
final class ArraySubset extends Constraint
38-
{
39-
private $subset;
40-
private $strict;
41-
42-
public function __construct(iterable $subset, bool $strict = false)
43-
{
44-
$this->strict = $strict;
45-
$this->subset = $subset;
46-
}
47-
48-
/**
49-
* {@inheritdoc}
50-
*/
51-
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
52-
{
53-
//type cast $other & $this->subset as an array to allow
54-
//support in standard array functions.
55-
$other = $this->toArray($other);
56-
$this->subset = $this->toArray($this->subset);
57-
$patched = array_replace_recursive($other, $this->subset);
58-
if ($this->strict) {
59-
$result = $other === $patched;
60-
} else {
61-
$result = $other == $patched;
62-
}
63-
if ($returnResult) {
64-
return $result;
65-
}
66-
if ($result) {
67-
return null;
68-
}
69-
70-
$f = new ComparisonFailure(
71-
$patched,
72-
$other,
73-
var_export($patched, true),
74-
var_export($other, true)
75-
);
76-
$this->fail($other, $description, $f);
77-
}
78-
79-
/**
80-
* {@inheritdoc}
81-
*/
82-
public function toString(): string
83-
{
84-
return 'has the subset '.$this->exporter()->export($this->subset);
85-
}
86-
87-
/**
88-
* {@inheritdoc}
89-
*/
90-
protected function failureDescription($other): string
91-
{
92-
return 'an array '.$this->toString();
93-
}
94-
95-
private function toArray(iterable $other): array
96-
{
97-
if (\is_array($other)) {
98-
return $other;
99-
}
100-
if ($other instanceof \ArrayObject) {
101-
return $other->getArrayCopy();
102-
}
103-
104-
return iterator_to_array($other);
105-
}
23+
// Aliases as string to avoid loading the class
24+
if (\PHP_VERSION_ID >= 80000 || (float) getenv('SYMFONY_PHPUNIT_VERSION') > 8) {
25+
class_alias('ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\ArraySubsetV9', 'ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\ArraySubset');
26+
} else {
27+
class_alias('ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\ArraySubsetLegacy', 'ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint\ArraySubset');
10628
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;
15+
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
18+
/**
19+
* Is used for phpunit < 8.
20+
*
21+
* @internal
22+
*/
23+
final class ArraySubsetLegacy extends Constraint
24+
{
25+
use ArraySubsetTrait;
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function evaluate($other, $description = '', $returnResult = false)
31+
{
32+
return $this->_evaluate($other, $description, $returnResult);
33+
}
34+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;
15+
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
use PHPUnit\SebastianBergmann\Comparator\ComparisonFailure;
18+
19+
/**
20+
* Constraint that asserts that the array it is evaluated for has a specified subset.
21+
*
22+
* Uses array_replace_recursive() to check if a key value subset is part of the
23+
* subject array.
24+
*
25+
* Imported from dms/phpunit-arraysubset-asserts, because the original constraint has been deprecated.
26+
*
27+
* @copyright Sebastian Bergmann <[email protected]>
28+
* @copyright Rafael Dohms <[email protected]>
29+
*
30+
* @see https://github.com/sebastianbergmann/phpunit/issues/3494
31+
*/
32+
trait ArraySubsetTrait
33+
{
34+
private $subset;
35+
private $strict;
36+
37+
public function __construct(iterable $subset, bool $strict = false)
38+
{
39+
$this->strict = $strict;
40+
$this->subset = $subset;
41+
}
42+
43+
private function _evaluate($other, string $description = '', bool $returnResult = false): ?bool
44+
{
45+
//type cast $other & $this->subset as an array to allow
46+
//support in standard array functions.
47+
$other = $this->toArray($other);
48+
$this->subset = $this->toArray($this->subset);
49+
$patched = array_replace_recursive($other, $this->subset);
50+
if ($this->strict) {
51+
$result = $other === $patched;
52+
} else {
53+
$result = $other == $patched;
54+
}
55+
if ($returnResult) {
56+
return $result;
57+
}
58+
if ($result) {
59+
return null;
60+
}
61+
62+
$f = new ComparisonFailure(
63+
$patched,
64+
$other,
65+
var_export($patched, true),
66+
var_export($other, true)
67+
);
68+
$this->fail($other, $description, $f);
69+
}
70+
71+
/**
72+
* {@inheritdoc}
73+
*/
74+
public function toString(): string
75+
{
76+
return 'has the subset '.$this->exporter()->export($this->subset);
77+
}
78+
79+
/**
80+
* {@inheritdoc}
81+
*/
82+
protected function failureDescription($other): string
83+
{
84+
return 'an array '.$this->toString();
85+
}
86+
87+
private function toArray(iterable $other): array
88+
{
89+
if (\is_array($other)) {
90+
return $other;
91+
}
92+
if ($other instanceof \ArrayObject) {
93+
return $other->getArrayCopy();
94+
}
95+
96+
return iterator_to_array($other);
97+
}
98+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <[email protected]>
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+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Test\Constraint;
15+
16+
use PHPUnit\Framework\Constraint\Constraint;
17+
18+
/**
19+
* Is used for phpunit >= 9.
20+
*
21+
* @internal
22+
*/
23+
final class ArraySubsetV9 extends Constraint
24+
{
25+
use ArraySubsetTrait;
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function evaluate($other, string $description = '', bool $returnResult = false): ?bool
31+
{
32+
return $this->_evaluate($other, $description, $returnResult);
33+
}
34+
}

tests/ProphecyTrait.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ protected function prophesize($classOrInterface = null): ObjectProphecy
7171

7272
/**
7373
* @postCondition
74+
* @after
7475
*/
7576
protected function verifyProphecyDoubles(): void
7677
{

0 commit comments

Comments
 (0)