Skip to content

Commit

Permalink
NEXT-22626 - Add new test structure for migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
keulinho committed Jul 28, 2022
1 parent b0ea816 commit 81c30b9
Show file tree
Hide file tree
Showing 16 changed files with 189 additions and 87 deletions.
18 changes: 12 additions & 6 deletions .danger.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use Danger\Rule\DisallowRepeatedCommits;
use Danger\Struct\Gitlab\File as GitlabFile;


return (new Config())
->useThreadOnFails()
->useRule(new DisallowRepeatedCommits)
Expand Down Expand Up @@ -196,14 +195,21 @@ function (Context $context) {
]
))
->useRule(function (Context $context) {
$files = $context->platform->pullRequest->getFiles();
function checkMigrationForBundle(string $bundle, Context $context): void
{
$files = $context->platform->pullRequest->getFiles();

$migrationFiles = $files->filterStatus(File::STATUS_ADDED)->matches('src/Core/Migration/V*/Migration*.php');
$migrationTestFiles = $files->filterStatus(File::STATUS_ADDED)->matches('src/Core/Migration/Test/*.php');
$migrationFiles = $files->filterStatus(File::STATUS_ADDED)->matches('src/Core/Migration/V*/Migration*.php');
$migrationTestFiles = $files->filterStatus(File::STATUS_ADDED)->matches('tests/migration/Core/V*/*.php');

if ($migrationFiles->count() && !$migrationTestFiles->count()) {
$context->failure('Please add tests for your new Migration file');
if ($migrationFiles->count() && !$migrationTestFiles->count()) {
$context->failure('Please add tests for your new Migration file');
}
}

checkMigrationForBundle('Core', $context);
checkMigrationForBundle('Administration', $context);
checkMigrationForBundle('Storefront', $context);
})
->useRule(function (Context $context) {
$files = $context->platform->pullRequest->getFiles();
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/02-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
matrix:
php-version: ["7.4", "8.0", "8.1"]
env:
TEST_SUITES: 'administration storefront checkout content framework profiling migration system elasticsearch docs unit integration'
TEST_SUITES: 'administration storefront checkout content framework profiling migration system elasticsearch docs unit integration migration-tests'
APP_ENV: test
DATABASE_URL: mysql://root:root@database:3306/root
APP_URL: http://localhost:8000
Expand Down
40 changes: 39 additions & 1 deletion .gitlab/stages/20-unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,44 @@ PHP unit coverage:
coverage_format: cobertura
path: cobertura.xml

PHP migration coverage:
extends: .base-no-setup
image: shopware/development:7.4-composer-2
stage: unit
needs: []
timeout: 10m
services:
- name: mariadb:10.4
alias: database
entrypoint: [ "sh", "-c", "docker-entrypoint.sh $MYSQL_CMD" ]
variables:
APP_ENV: "test"
FEATURE_ALL: "false"
rules:
- !reference [.rules, skip]
- !reference [.rules, run]
- when: always
before_script:
- composer install --optimize-autoloader --no-interaction --quiet
script:
- PHP_OPTIONS="-d pcov.enabled=1 -d pcov.directory=$PWD/src -d pcov.exclude='~(vendor|Test|node_modules)~'"
- php $PHP_OPTIONS vendor/bin/phpunit
--configuration tests/migration/phpunit.xml
--log-junit phpunit.junit.xml
--testsuite migration-tests
--coverage-text
--coverage-cobertura cobertura.xml | sed -E -n '1,/^\s*Lines:\s*([0-9]+(\.[0-9]+)?)%/ p' # do not output covered files lines
coverage: '/^\s*Lines:\s*(\d+(?:\.\d+)?%)/'
artifacts:
when: always
paths:
- cobertura.xml
reports:
junit: phpunit.junit.xml
coverage_report:
coverage_format: cobertura
path: cobertura.xml

PHP Full:
extends: .long-running
image: $DEV_IMAGE
Expand All @@ -191,7 +229,7 @@ PHP Full:
- name: redis
alias: redis
variables:
TEST_SUITES: "docs administration storefront checkout content framework profiling system elasticsearch unit integration migration"
TEST_SUITES: "docs administration storefront checkout content framework profiling system elasticsearch unit integration migration migration-tests"
APP_ENV: test
GIT_DEPTH: 0 # we need all commits for composer to detect the version which is then checked in a unit test
RUNNER_INSTANCE_TYPE: m5.large
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,8 @@
"Shopware\\Core\\Test\\": "src/Core/Test/",
"Shopware\\Tests\\Unit\\": "tests/unit/php/",
"Shopware\\Tests\\Integration\\": "tests/integration/php/",
"Shopware\\Tests\\Bench\\": "tests/performance/bench/"
"Shopware\\Tests\\Bench\\": "tests/performance/bench/",
"Shopware\\Tests\\Migration\\": "tests/migration/"
}
}
}
50 changes: 0 additions & 50 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -8209,21 +8209,6 @@ parameters:
count: 1
path: src/Core/Framework/Test/Plugin/KernelPluginIntegrationTest.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\Plugin\\\\KernelPluginIntegrationTest\\:\\:makeKernel\\(\\) should return Shopware\\\\Core\\\\Kernel but returns object\\.$#"
count: 1
path: src/Core/Framework/Test/Plugin/KernelPluginIntegrationTest.php

-
message: "#^Parameter \\#1 \\$argument of class ReflectionClass constructor expects class\\-string\\<T of object\\>\\|T of object, string given\\.$#"
count: 1
path: src/Core/Framework/Test/Plugin/KernelPluginIntegrationTest.php

-
message: "#^Property Shopware\\\\Core\\\\Framework\\\\Test\\\\Plugin\\\\KernelPluginIntegrationTest\\:\\:\\$kernel \\(Shopware\\\\Core\\\\Kernel\\|null\\) does not accept object\\.$#"
count: 1
path: src/Core/Framework/Test/Plugin/KernelPluginIntegrationTest.php

-
message: "#^Cannot call method format\\(\\) on DateTimeInterface\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -8924,36 +8909,6 @@ parameters:
count: 5
path: src/Core/Framework/Test/TestCaseBase/DatadogListener.php

-
message: "#^Cannot call method fetchAll\\(\\) on Doctrine\\\\DBAL\\\\Connection\\|null\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\TestCaseBase\\\\KernelLifecycleManager\\:\\:bootKernel\\(\\) should return Shopware\\\\Core\\\\Kernel but returns Symfony\\\\Component\\\\HttpKernel\\\\KernelInterface\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Method Shopware\\\\Core\\\\Framework\\\\Test\\\\TestCaseBase\\\\KernelLifecycleManager\\:\\:createKernel\\(\\) should return Symfony\\\\Component\\\\HttpKernel\\\\KernelInterface but returns object\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Parameter \\#1 \\$classname of function class_exists expects string, bool\\|float\\|int\\|string\\|null given\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Service \"test\\.browser\" is not registered in the container\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Static property Shopware\\\\Core\\\\Framework\\\\Test\\\\TestCaseBase\\\\KernelLifecycleManager\\:\\:\\$kernel \\(Shopware\\\\Core\\\\Kernel\\|null\\) does not accept Symfony\\\\Component\\\\HttpKernel\\\\KernelInterface\\.$#"
count: 1
path: src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php

-
message: "#^Property Shopware\\\\Core\\\\Framework\\\\Test\\\\TestCaseBase\\\\KernelTestBehaviourTest\\:\\:\\$kernelId has no type specified\\.$#"
count: 1
Expand Down Expand Up @@ -9349,11 +9304,6 @@ parameters:
count: 1
path: src/Core/Migration/Migration1614249488ChangeProductSortingsToCheapestPrice.php

-
message: "#^Parameter \\#1 \\$filename of function sha1_file expects string, string\\|false given\\.$#"
count: 1
path: src/Core/Migration/Test/BasicDataUntouchedTest.php

-
message: "#^Parameter \\#1 \\$filename of function file_get_contents expects string, string\\|false given\\.$#"
count: 1
Expand Down
4 changes: 2 additions & 2 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,12 @@ parameters:
message: '#Service ".*" is not registered in the container\.#'
paths:
- src/**/*Test.php
- tests/**/*Test.php
- src/Core/Framework/Test/TestCaseBase/*.php
-
message: '#Service ".*" is private#'
paths:
- src/**/*Test.php
- tests/**/*Test.php
- tests/integration/**/*Test.php

# ignore errors caused by static::markTestSkipped
-
Expand Down
6 changes: 4 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@
<directory suffix=".php">src/Core/Profiling</directory>
<directory suffix=".php">src/Core/System</directory>
<directory suffix=".php">src/Elasticsearch</directory>
<directory suffix=".php">src/Core/Migration</directory>
<directory suffix=".php">src/Core/Migration</directory>

<file>src/Core/Defaults.php</file>
<file>src/Core/HttpKernel.php</file>
Expand Down Expand Up @@ -127,6 +125,10 @@
<directory>tests/integration/php</directory>
</testsuite>

<testsuite name="migration-tests">
<directory>tests/migration</directory>
</testsuite>

<testsuite name="docs">
<directory>src/Docs/Test</directory>
<directory suffix="example.php">src/Docs/Resources/current/</directory>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private function isTestClass(InClassNode $node): bool
{
$namespace = $node->getClassReflection()->getName();

if (!\str_contains($namespace, 'Shopware\\Tests\\Unit\\')) {
if (!\str_contains($namespace, 'Shopware\\Tests\\Unit\\') && !\str_contains($namespace, 'Shopware\\Tests\\Migration\\')) {
return false;
}

Expand All @@ -61,6 +61,6 @@ private function hasCovers(InClassNode $class): bool
return false;
}

return \str_contains($doc->getText(), '@covers');
return \str_contains($doc->getText(), '@covers') || \str_contains($doc->getText(), '@coversNothing');
}
}
35 changes: 29 additions & 6 deletions src/Core/Framework/Test/TestCaseBase/KernelLifecycleManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Composer\Autoload\ClassLoader;
use Doctrine\DBAL\Connection;
use Shopware\Core\DevOps\Environment\EnvironmentHelper;
use Shopware\Core\Framework\Adapter\Database\MySQLFactory;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\DbalKernelPluginLoader;
use Shopware\Core\Framework\Plugin\KernelPluginLoader\StaticKernelPluginLoader;
use Shopware\Core\Framework\Test\Filesystem\Adapter\MemoryAdapterFactory;
Expand All @@ -20,7 +21,7 @@
class KernelLifecycleManager
{
/**
* @var string
* @var class-string<Kernel>
*/
protected static $class;

Expand Down Expand Up @@ -102,7 +103,10 @@ public static function bootKernel(bool $reuseConnection = true, string $cacheId
return static::$kernel;
}

public static function createKernel(?string $kernelClass = null, bool $reuseConnection = true, string $cacheId = 'h8f3f0ee9c61829627676afd6294bb029', ?string $projectDir = null): KernelInterface
/**
* @param class-string<Kernel>|null $kernelClass
*/
public static function createKernel(?string $kernelClass = null, bool $reuseConnection = true, string $cacheId = 'h8f3f0ee9c61829627676afd6294bb029', ?string $projectDir = null): Kernel
{
if ($kernelClass === null) {
if (static::$class === null) {
Expand All @@ -122,7 +126,7 @@ public static function createKernel(?string $kernelClass = null, bool $reuseConn
try {
$existingConnection = null;
if ($reuseConnection) {
$existingConnection = self::$connection;
$existingConnection = self::getConnection();

try {
$existingConnection->fetchAll('SELECT 1');
Expand All @@ -148,12 +152,11 @@ public static function createKernel(?string $kernelClass = null, bool $reuseConn
}

/**
* @throws \RuntimeException
* @throws \LogicException
* @return class-string<Kernel>
*/
public static function getKernelClass(): string
{
if (!class_exists($class = EnvironmentHelper::getVariable('KERNEL_CLASS', Kernel::class))) {
if (!class_exists($class = (string) EnvironmentHelper::getVariable('KERNEL_CLASS', Kernel::class))) {
throw new \RuntimeException(
sprintf(
'Class "%s" doesn\'t exist or cannot be autoloaded. Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.',
Expand All @@ -163,6 +166,17 @@ public static function getKernelClass(): string
);
}

if (!is_a($class, Kernel::class, true)) {
throw new \RuntimeException(
sprintf(
'Class "%s" must extend "%s". Check that the KERNEL_CLASS value in phpunit.xml matches the fully-qualified class name of your Kernel or override the %s::createKernel() method.',
$class,
Kernel::class,
static::class
)
);
}

return $class;
}

Expand All @@ -184,4 +198,13 @@ public static function ensureKernelShutdown(): void

static::$kernel = null;
}

public static function getConnection(): Connection
{
if (!static::$connection) {
static::$connection = MySQLFactory::create();
}

return static::$connection;
}
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,22 +1,21 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\Test;
namespace Shopware\Tests\Migration\Core;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use Shopware\Core\Framework\Test\TestCaseBase\KernelTestBehaviour;
use Shopware\Core\Migration\V6_3\Migration1536233560BasicData;

/**
* @internal
* @coversNothing
*/
class BasicDataUntouchedTest extends TestCase
{
use KernelTestBehaviour;

public function testBasicDataUntouched(): void
{
$loader = KernelLifecycleManager::getClassLoader();
/** @var string $file */
$file = $loader->findFile(Migration1536233560BasicData::class);

static::assertSame(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
<?php declare(strict_types=1);

namespace Shopware\Core\Migration\Test;
namespace Shopware\Tests\Migration\Core\V6_4;

use Doctrine\DBAL\Connection;
use PHPUnit\Framework\TestCase;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Test\TestCaseBase\KernelLifecycleManager;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\Migration\V6_4\Migration1653376989ResetDefaultAlwaysValidConditionValue;
use Shopware\Tests\Migration\MigrationTestTrait;

/**
* @internal
* @covers \Shopware\Core\Migration\V6_4\Migration1653376989ResetDefaultAlwaysValidConditionValue
*/
class Migration1653376989ResetDefaultAlwaysValidConditionValueTest extends TestCase
{
use IntegrationTestBehaviour;
use MigrationTestTrait;

private Connection $connection;

protected function setUp(): void
{
$this->connection = $this->getContainer()->get(Connection::class);
$this->connection = KernelLifecycleManager::getConnection();
}

public function testResetDefaultValwaysValidConditionValue(): void
Expand Down
Loading

0 comments on commit 81c30b9

Please sign in to comment.