Skip to content

Commit

Permalink
NEXT-25276 - phpunit v10 update remaining tasks
Browse files Browse the repository at this point in the history
- Migrated extension
- Fixed and refactored unit tests
- Fixed phpstan errors
  • Loading branch information
jozsefdamokos committed Dec 13, 2023
1 parent b96697d commit 4572a90
Show file tree
Hide file tree
Showing 197 changed files with 1,282 additions and 879 deletions.
4 changes: 4 additions & 0 deletions .gitlab/stages/20-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ PHP integration:
FEATURE_ALL: "major"
GIT_DEPTH: 0 # we need all commits for composer to detect the version which is then checked in a unit test
REDIS_URL: redis://redis:6379
SYMFONY_DEPRECATIONS_HELPER: "disabled" # remove line when phpunit-bridge supports phpunit 10
rules:
# do not run in merge trains - PHP Full should cover all tests
- if: $CI_MERGE_REQUEST_EVENT_TYPE == "merge_train"
Expand Down Expand Up @@ -158,6 +159,7 @@ PHP unit coverage:
services: []
variables:
APP_ENV: "test"
SYMFONY_DEPRECATIONS_HELPER: "disabled" # remove line when phpunit-bridge supports phpunit 10
rules:
- !reference [.rules, skip]
- !reference [.rules, run]
Expand Down Expand Up @@ -201,6 +203,7 @@ PHP migration coverage:
variables:
APP_ENV: "test"
FEATURE_ALL: "false"
SYMFONY_DEPRECATIONS_HELPER: "disabled" # remove line when phpunit-bridge supports phpunit 10
rules:
- !reference [.rules, skip]
- !reference [.rules, run]
Expand Down Expand Up @@ -245,6 +248,7 @@ PHP Full:
REDIS_URL: redis://redis:6379
SWAGGER_VALIDATOR_URL: http://swagger:8080/validator/debug
FEATURE_ALL: 'false'
SYMFONY_DEPRECATIONS_HELPER: "disabled" # remove line when phpunit-bridge supports phpunit 10
rules:
- !reference [.rules, skip]
- if: !reference [.rules, run-allow-failure, if ]
Expand Down
2 changes: 1 addition & 1 deletion UPGRADE-6.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ Core
// or with a static plugin list
$plugins = [
[
'baseClass' => 'SwagTest\\SwagTest',
'baseClass' => 'SwagTest\\SwagTestPlugin',
'active' => true,
'path' => 'platform/src/Core/Framework/Test/Plugin/_fixture/plugins/SwagTest',
'autoload' => ['psr-4' => ['SwagTest\\' => 'src/']],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
title: Updated PHPUnit to version 10+
issue: NEXT-25276
author: Jozsef Damokos
author_email: [email protected]
author_github: jozsefdamokos
---
# Core
* Added support for PHPUnit v10+. Changed all `@covers` annotations to attributes. Migrated extensions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@
"ezyang/htmlpurifier": "^4.16",
"guzzlehttp/guzzle": "^7.5.0",
"guzzlehttp/psr7": "^2.4",
"lcobucci/clock": "^3.1.0",
"lcobucci/jwt": "^5.0",
"lcobucci/clock": "^3.1.0",
"league/flysystem": "^3.10.3",
"league/flysystem-memory": "^3.10.3",
"league/mime-type-detection": "^1.13.0",
Expand Down
10 changes: 0 additions & 10 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -2660,11 +2660,6 @@ parameters:
count: 2
path: src/Core/Content/Test/Media/Aggregate/MediaFolderConfigurationThumbnailSize/MediaFolderConfigurationMediaThumbnailSizeTest.php

-
message: "#^Cannot call method all\\(\\) on Shopware\\\\Core\\\\Content\\\\Cms\\\\DataResolver\\\\CriteriaCollection\\|null\\.$#"
count: 1
path: src/Core/Content/Test/Media/Cms/Type/ImageTypeDataResolverTest.php

-
message: "#^Cannot call method getFileExtension\\(\\) on Shopware\\\\Core\\\\Content\\\\Media\\\\MediaEntity\\|null\\.$#"
count: 1
Expand All @@ -2690,11 +2685,6 @@ parameters:
count: 1
path: src/Core/Content/Test/Media/Cms/Type/ImageTypeDataResolverTest.php

-
message: "#^Parameter \\#2 \\$haystack of static method PHPUnit\\\\Framework\\\\Assert\\:\\:assertCount\\(\\) expects Countable\\|iterable, Shopware\\\\Core\\\\Content\\\\Cms\\\\DataResolver\\\\CriteriaCollection\\|null given\\.$#"
count: 1
path: src/Core/Content/Test/Media/Cms/Type/ImageTypeDataResolverTest.php

-
message: "#^Cannot call method getConfigurationId\\(\\) on Shopware\\\\Core\\\\Framework\\\\DataAbstractionLayer\\\\Entity\\|null\\.$#"
count: 6
Expand Down
4 changes: 0 additions & 4 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,6 @@ parameters:
- src/Core/Checkout/Payment/PaymentMethodDefinition.php
- src/Core/System/NumberRange/NumberRangeDefinition.php

- # NEXT-25276 - Needs to be fixed with the update ot PHPUnit 10
message: '#deprecated interface PHPUnit\\Framework\\TestListener#'
path: src/Core/Test/PHPUnit/Extension/DatadogListener.php

# Internal deprecations of Shopware are handled in other places
- '#deprecated.*class Shopware\\#'

Expand Down
7 changes: 4 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,10 @@
</testsuites>
<extensions>
<bootstrap class="Ergebnis\PHPUnit\SlowTestDetector\Extension"/>
<!-- <extension class="Shopware\Core\Test\PHPUnit\Extension\FeatureFlagExtension"/>-->
<!--Enable to see the db sideeffects of the tests. -->
<!--<extension class="Shopware\Core\Framework\Test\TestCaseBase\DatabaseCleanTestExtension" />-->
<bootstrap class="Shopware\Core\Test\PHPUnit\Extension\FeatureFlag\FeatureFlagExtension"/>
<bootstrap class="Shopware\Core\Test\PHPUnit\Extension\Datadog\DatadogExtension"/>
<!-- Enable to see the db side effects of the tests. -->
<!-- <bootstrap class="Shopware\Core\Test\PHPUnit\Extension\DatabaseDiff\DatabaseDiffExtension"/>-->
</extensions>
<source>
<include>
Expand Down
6 changes: 6 additions & 0 deletions rector.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@
]
);

// PHPUnit v10 update
// $rectorConfig->rule(\Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\CoversAnnotationWithValueToAttributeRector::class);
// $rectorConfig->rule(\Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DataProviderAnnotationToAttributeRector::class);
// $rectorConfig->rule(\Rector\PHPUnit\AnnotationsToAttributes\Rector\ClassMethod\DependsAnnotationWithValueToAttributeRector::class);
// $rectorConfig->rule(\Rector\PHPUnit\AnnotationsToAttributes\Rector\Class_\AnnotationWithValueToAttributeRector::class);

// $rectorConfig->rule(ClassPackageRector::class);
// $rectorConfig->ruleWithConfiguration(
// ChangeMethodVisibilityRector::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function testAddSingleCriteria(): void
static::assertCount(1, $collection->all());

// test iterator
static::assertCount(1, $collection);
static::assertCount(1, iterator_to_array($collection));
}

public function testAddMultipleCriteriaOfDifferentDefinition(): void
Expand All @@ -38,7 +38,7 @@ public function testAddMultipleCriteriaOfDifferentDefinition(): void
static::assertCount(3, $collection->all());

// test iterator
static::assertCount(3, $collection);
static::assertCount(3, iterator_to_array($collection));
}

public function testAddMultipleCriteriaOfSameDefinition(): void
Expand All @@ -52,7 +52,7 @@ public function testAddMultipleCriteriaOfSameDefinition(): void
static::assertCount(1, $collection->all());

// test iterator
static::assertCount(1, $collection);
static::assertCount(1, iterator_to_array($collection));

// test indexed by definition
static::assertCount(3, $collection->all()[ProductDefinition::class]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Media\Api\MediaUploadController;
use Shopware\Core\Content\Media\Event\MediaUploadedEvent;
use Shopware\Core\Content\Media\MediaEntity;
use Shopware\Core\Content\Media\MediaType\ImageType;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public function testCollectWithStaticConfig(): void

$criteriaCollection = $this->imageSliderResolver->collect($slot, $resolverContext);
static::assertNotNull($criteriaCollection);
static::assertCount(1, $criteriaCollection);
static::assertCount(1, iterator_to_array($criteriaCollection));

$expectedCriteria = new Criteria(['media123', 'media456']);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function testCollectWithMediaId(): void

$criteriaCollection = $this->imageResolver->collect($slot, $resolverContext);

static::assertCount(1, $criteriaCollection);
static::assertNotNull($criteriaCollection);
static::assertCount(1, iterator_to_array($criteriaCollection));

$expectedCriteria = new Criteria(['media123']);

Expand Down
1 change: 1 addition & 0 deletions src/Core/Content/Test/Media/File/FileSaverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ public function testPersistFileToMediaDoesNotAddSuffixOnReplacement(): void
$this->getPublicFilesystem()->writeStream($pathName, $resource);

static::assertIsString($png->getFileName());
static::assertNotEmpty($png->getFileName());

try {
$this->fileSaver->persistFileToMedia(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Content\Newsletter\Event\NewsletterUnsubscribeEvent;
use Shopware\Core\Content\Newsletter\SalesChannel\NewsletterUnsubscribeRoute;
use Shopware\Core\Framework\Log\Package;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Test\TestCaseBase\SalesChannelApiTestBehaviour;
Expand Down
3 changes: 2 additions & 1 deletion src/Core/Content/Test/Seo/SeoUrlUpdaterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ protected function setUp(): void
* Checks whether the seo url updater is using the correct language for translations.
*
* @param list<string> $translations
* @param non-empty-string $pathInfo
*/
#[DataProvider('seoLanguageDataProvider')]
public function testSeoLanguageInheritance(array $translations, string $pathInfo): void
Expand Down Expand Up @@ -165,7 +166,7 @@ public function testSeoLanguageInheritance(array $translations, string $pathInfo
}

/**
* @return list<array{translations: list<string>, pathInfo: string}>
* @return list<array{translations: list<string>, pathInfo: non-empty-string}>
*/
public static function seoLanguageDataProvider(): array
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,23 +111,23 @@ protected function execute(InputInterface $input, OutputInterface $output): int
if ($unitDocument === null) {
return 1;
}
$coverage = $unitDocument->getElementsByTagName('coverage')->item(0);
if ($coverage === null) {
$source = $unitDocument->getElementsByTagName('source')->item(0);
if ($source === null) {
return 1;
}
$includeChildElement = $coverage->getElementsByTagName('include')->item(0);
$includeChildElement = $source->getElementsByTagName('include')->item(0);
if ($includeChildElement === null) {
return 1;
}
// Remove include from coverage to create our own includes
$coverage->removeChild($includeChildElement);
// Remove include from source to create our own includes
$source->removeChild($includeChildElement);
$includeElement = $unitFile->createElement('include');

foreach ($classToFile as $class => $file) {
$fileElement = $unitFile->createElement('file', $file);
$includeElement->appendChild($fileElement);
}
$coverage->appendChild($includeElement);
$source->appendChild($includeElement);

// Create phpunit file per area
file_put_contents("phpunit.$area.xml", $unitFile->saveXML());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
use PHPStan\Node\InClassNode;
use PHPStan\Rules\Rule;
use PHPStan\Rules\RuleError;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversFunction;
use PHPUnit\Framework\Attributes\CoversNothing;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Log\Package;

Expand Down Expand Up @@ -35,7 +38,7 @@ public function processNode(Node $node, Scope $scope): array
}

if ($this->isTestClass($node)) {
return ['Test classes must have @covers annotation'];
return ['Test classes must have CoversClass, CoversFunction or CoversNothing attribute'];
}

return [];
Expand Down Expand Up @@ -64,17 +67,11 @@ private function hasCovers(InClassNode $class): bool
/** @var Node\Name\FullyQualified $name */
$name = $attribute->name;

if ($name->toString() === 'PHPUnit\Metadata\CoversClass\CoversClass') {
if (\in_array($name->toString(), [CoversClass::class, CoversFunction::class, CoversNothing::class], true)) {
return true;
}
}

$doc = $class->getDocComment();

if ($doc === null) {
return false;
}

return \str_contains($doc->getText(), '@covers') || \str_contains($doc->getText(), '@coversNothing');
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function testSortByScoreIfScoreQueryIsSet(): void
$productDefinition = $this->getContainer()->get(ProductDefinition::class);
$criteria = new Criteria();
$criteria->addQuery(new ScoreQuery(new ContainsFilter('name', 'test matching'), 1000));
$queryMock = $this->createTestProxy(QueryBuilder::class, [$this->createMock(Connection::class)]);
$queryMock = $this->createMock(QueryBuilder::class);
$queryMock
->expects(static::once())
->method('addOrderBy')
Expand All @@ -83,7 +83,7 @@ public function testSortByScoreIfSearchTermIsSet(): void
$productDefinition = $this->getContainer()->get(ProductDefinition::class);
$criteria = new Criteria();
$criteria->setTerm('searchTerm');
$queryMock = $this->createTestProxy(QueryBuilder::class, [$this->createMock(Connection::class)]);
$queryMock = $this->createMock(QueryBuilder::class);
$queryMock
->expects(static::once())
->method('addOrderBy')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
use Shopware\Core\Framework\Test\Plugin\_fixture\bundles\FooBarBundle;
use Shopware\Core\Framework\Test\Plugin\_fixture\bundles\GizmoBundle;
use Shopware\Core\Framework\Test\Plugin\PluginIntegrationTestBehaviour;
use SwagTest\SwagTest;
use SwagTest\SwagTestFake;
use SwagTestPlugin\SwagTestFake;
use SwagTestPlugin\SwagTestPlugin;
use SwagTestWithBundle\SwagTestWithBundle;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
Expand Down Expand Up @@ -91,7 +91,7 @@ public function testExpectExceptionOnMissingAutoload(): void
$loader = new StaticKernelPluginLoader($this->classLoader, null, $plugins);

$this->expectException(KernelPluginLoaderException::class);
$this->expectExceptionMessage('Failed to load plugin "SwagTest". Reason: Unable to register plugin "SwagTest\SwagTest" in autoload. Required property `autoload` missing.');
$this->expectExceptionMessage('Failed to load plugin "SwagTestPlugin". Reason: Unable to register plugin "SwagTestPlugin\SwagTestPlugin" in autoload. Required property `autoload` missing.');
$loader->initializePlugins(TEST_PROJECT_DIR);
}

Expand All @@ -104,7 +104,7 @@ public function testExpectExceptionOnMissingAutoloadPsr(): void
$loader = new StaticKernelPluginLoader($this->classLoader, null, $plugins);

$this->expectException(KernelPluginLoaderException::class);
$this->expectExceptionMessage('Failed to load plugin "SwagTest". Reason: Unable to register plugin "SwagTest\SwagTest" in autoload. Required property `psr-4` or `psr-0` missing in property autoload.');
$this->expectExceptionMessage('Failed to load plugin "SwagTestPlugin". Reason: Unable to register plugin "SwagTestPlugin\SwagTestPlugin" in autoload. Required property `psr-4` or `psr-0` missing in property autoload.');
$loader->initializePlugins(TEST_PROJECT_DIR);
}

Expand Down Expand Up @@ -195,7 +195,7 @@ public function testGetBundles(): void

static::assertCount(4, $bundles);
static::assertInstanceOf(GizmoBundle::class, $bundles[0]);
static::assertInstanceOf(SwagTest::class, $bundles[1]);
static::assertInstanceOf(SwagTestPlugin::class, $bundles[1]);
static::assertInstanceOf(FooBarBundle::class, $bundles[2]);
static::assertSame($loader, $bundles[3]);
}
Expand All @@ -213,7 +213,7 @@ public function testGetBundlesWithAdditionalBundlesThatAreDuplicatesButKeepOrder

static::assertCount(5, $bundles);
static::assertInstanceOf(GizmoBundle::class, $bundles[0]);
static::assertInstanceOf(SwagTest::class, $bundles[1]);
static::assertInstanceOf(SwagTestPlugin::class, $bundles[1]);
static::assertInstanceOf(FooBarBundle::class, $bundles[2]);
static::assertInstanceOf(SwagTestWithBundle::class, $bundles[3]);
static::assertSame($loader, $bundles[4]);
Expand All @@ -237,7 +237,7 @@ public function testExpectExceptionWithFakePlugin(): void
$loader = new StaticKernelPluginLoader($this->classLoader, null, [$fakePluginData]);

$this->expectException(KernelPluginLoaderException::class);
$this->expectExceptionMessage('Failed to load plugin "SwagTest". Reason: Plugin class "SwagTest\SwagTestFake" must extend "Shopware\Core\Framework\Plugin"');
$this->expectExceptionMessage('Failed to load plugin "SwagTestPlugin". Reason: Plugin class "SwagTestPlugin\SwagTestFake" must extend "Shopware\Core\Framework\Plugin"');
$loader->initializePlugins(TEST_PROJECT_DIR);
}

Expand Down Expand Up @@ -278,7 +278,7 @@ public function testBuild(): void
$container = new ContainerBuilder();
$loader->build($container);

$definition = $container->getDefinition(SwagTest::class);
$definition = $container->getDefinition(SwagTestPlugin::class);
static::assertNotNull($definition);
static::assertTrue($definition->isAutowired());
static::assertTrue($definition->isPublic());
Expand All @@ -295,11 +295,11 @@ public function testBuildWithExistingDefinition(): void
$definition = new Definition();
$definition->setAutowired(false);
$definition->setPublic(false);
$container->setDefinition(SwagTest::class, $definition);
$container->setDefinition(SwagTestPlugin::class, $definition);

$loader->build($container);

$actualDefinition = $container->getDefinition(SwagTest::class);
$actualDefinition = $container->getDefinition(SwagTestPlugin::class);
static::assertSame($definition, $actualDefinition);
static::assertTrue($actualDefinition->isAutowired());
static::assertTrue($actualDefinition->isPublic());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Kernel;
use SwagTest\SwagTest;
use SwagTestPlugin\SwagTestPlugin;
use SwagTestSkipRebuild\SwagTestSkipRebuild;
use SwagTestWithBundle\SwagTestWithBundle;

Expand Down Expand Up @@ -78,7 +78,7 @@ protected function getNotInstalledPlugin(): PluginEntity
$plugin->assign([
'id' => Uuid::randomHex(),
'name' => 'SwagTest',
'baseClass' => SwagTest::class,
'baseClass' => SwagTestPlugin::class,
'version' => '1.0.1',
'active' => false,
'path' => __DIR__ . '/_fixture/plugins/SwagTest',
Expand Down
Loading

0 comments on commit 4572a90

Please sign in to comment.