Skip to content

Commit

Permalink
Drop support for PHP 7.x (#44)
Browse files Browse the repository at this point in the history
* Drop support for PHP 7.x

Small refactors to use PHP 8.0 features and optimize some code

* Add a test case for when container does not have the extension loaded
  • Loading branch information
hugo-goncalves-kununu authored Feb 24, 2023
1 parent a36bd8a commit 6dd9c68
Showing 63 changed files with 654 additions and 617 deletions.
4 changes: 2 additions & 2 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -8,6 +8,6 @@ updates:
labels:
- "dependencies"
versioning-strategy: auto
# By default Dependabot has a limit of the number of open PR for version updates
# and security updates.
# By default, Dependabot has a limit of the number of open PR for version updates
# and security updates.
# See other defaults in here: https://docs.github.com/en/github/administering-a-repository/configuration-options-for-dependency-updates
5 changes: 1 addition & 4 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
@@ -17,9 +17,6 @@ jobs:
strategy:
matrix:
php-version:
- 7.2
- 7.3
- 7.4
- 8.0
mysql-version:
- 5.7
@@ -107,7 +104,7 @@ jobs:

- uses: actions/download-artifact@v2
with:
name: build-7.4-coverage
name: build-8-coverage
path: tests/.results/

- name: Fix Code Coverage Paths
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
@@ -15,9 +15,9 @@
}
],
"require": {
"php": "^7.2 || ^8.0",
"php": ">=8.0",
"symfony/framework-bundle": "^4.4 || ^5.0",
"kununu/data-fixtures": "^9.1",
"kununu/data-fixtures": "^10.0",
"symfony/config": "^4.4 || ^5.0",
"symfony/dependency-injection": "^4.4 || ^5.0",
"symfony/http-kernel": "^4.4 || ^5.0"
39 changes: 18 additions & 21 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,36 +1,33 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true"
beStrictAboutChangesToGlobalState="true">
<!-- https://phpunit.readthedocs.io/en/9.6/ -->
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.6/phpunit.xsd" bootstrap="tests/bootstrap.php"
colors="true" beStrictAboutChangesToGlobalState="true">
<coverage processUncoveredFiles="false">
<include>
<directory>src</directory>
</include>
<exclude>
<file>src/Traits/ConnectionToolsTrait.php</file>
</exclude>
<report>
<clover outputFile="tests/.results/tests-clover.xml"/>
<html outputDirectory="tests/.results/html/"/>
</report>
</coverage>
<php>
<ini name="error_reporting" value="-1"/>
<server name="APP_ENV" value="test" force="true"/>
</php>
<testsuites>
<testsuite name="TestingBundle Test Suite">
<directory>./tests/</directory>
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="false">
<directory>src</directory>
<exclude>
<file>src/Traits/ConnectionToolsTrait.php</file>
</exclude>
</whitelist>
</filter>

<listeners>
<listener class="Symfony\Bridge\PhpUnit\SymfonyTestsListener"/>
</listeners>

<logging>
<log type="coverage-clover" target="tests/.results/tests-clover.xml"/>
<log type="junit" target="tests/.results/tests-junit.xml"/>
<log type="coverage-html" target="tests/.results/html/"/>
<junit outputFile="tests/.results/tests-junit.xml"/>
</logging>
</phpunit>
49 changes: 32 additions & 17 deletions src/Command/CopyConnectionSchemaCommand.php
Original file line number Diff line number Diff line change
@@ -15,32 +15,41 @@

final class CopyConnectionSchemaCommand extends Command
{
private $schemaCopy;
private $registry;
private const OPTION_FROM = 'from';
private const OPTION_FROM_SHORT = 'f';
private const OPTION_TO = 'to';
private const OPTION_TO_SHORT = 't';

public function __construct(SchemaCopyInterface $schemaCopy, ManagerRegistry $registry)
public function __construct(private SchemaCopyInterface $schemaCopy, private ManagerRegistry $registry)
{
parent::__construct('kununu_testing:connections:schema:copy');

$this->schemaCopy = $schemaCopy;
$this->registry = $registry;
}

protected function configure(): void
{
$this
->setDescription('Copy a schema from one connection to another')
->addOption('from', 'f', InputOption::VALUE_REQUIRED, 'The source connection to use for this command')
->addOption('to', 't', InputOption::VALUE_REQUIRED, 'The destination connection to use for this command');
->addOption(
self::OPTION_FROM,
self::OPTION_FROM_SHORT,
InputOption::VALUE_REQUIRED,
'The source connection to use for this command'
)
->addOption(
self::OPTION_TO,
self::OPTION_TO_SHORT,
InputOption::VALUE_REQUIRED,
'The destination connection to use for this command'
);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
$from = $input->getOption('from') ?? '';
$to = $input->getOption('to') ?? '';
$from = $input->getOption(self::OPTION_FROM) ?? '';
$to = $input->getOption(self::OPTION_TO) ?? '';

if (!$this->checkConnection($source = $this->getConnection($from), 'from', $from, $output) ||
!$this->checkConnection($destination = $this->getConnection($to), 'to', $to, $output)
if (!$this->checkConnection($source = $this->getConnection($from), self::OPTION_FROM, $from, $output) ||
!$this->checkConnection($destination = $this->getConnection($to), self::OPTION_TO, $to, $output)
) {
return 2;
}
@@ -60,8 +69,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
return 1;
}

private function checkConnection(?Connection $connection, string $arg, string $connectionName, OutputInterface $output): bool
{
private function checkConnection(
?Connection $connection,
string $arg,
string $connectionName,
OutputInterface $output
): bool {
$connectionName = trim($connectionName);

if ('' === $connectionName) {
@@ -74,7 +87,9 @@ private function checkConnection(?Connection $connection, string $arg, string $c

if (!$connection instanceof Connection) {
$output->writeln('');
$output->writeln(sprintf('Connection wanted to "--%s" argument: "%s" was not found!', $arg, $connectionName));
$output->writeln(
sprintf('Connection wanted to "--%s" argument: "%s" was not found!', $arg, $connectionName)
);
$output->writeln('');

return false;
@@ -83,12 +98,12 @@ private function checkConnection(?Connection $connection, string $arg, string $c
return true;
}

private function getConnection(string $connectionName): ?Connection
private function getConnection(string $connectionName): Connection|null
{
/* @var Connection|null $connection */
try {
$connection = $this->registry->getConnection($connectionName);
} catch (InvalidArgumentException $e) {
} catch (InvalidArgumentException) {
$connection = null;
}

43 changes: 21 additions & 22 deletions src/Command/LoadFixturesCommand.php
Original file line number Diff line number Diff line change
@@ -3,7 +3,7 @@

namespace Kununu\TestingBundle\Command;

use Kununu\TestingBundle\Service\Orchestrator;
use Kununu\TestingBundle\Service\OrchestratorInterface;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
@@ -12,18 +12,14 @@

abstract class LoadFixturesCommand extends Command
{
private $alias;
private $orchestrator;
private $fixturesClassNames;

public function __construct(string $alias, Orchestrator $orchestrator, array $fixturesClassNames)
{
$this->alias = $alias;
protected const OPTION_APPEND = 'append';

public function __construct(
private string $alias,
private OrchestratorInterface $orchestrator,
private array $fixturesClassNames
) {
parent::__construct(sprintf('kununu_testing:load_fixtures:%s:%s', static::getFixtureType(), $alias));

$this->orchestrator = $orchestrator;
$this->fixturesClassNames = $fixturesClassNames;
}

abstract protected function getFixtureType(): string;
@@ -32,31 +28,34 @@ abstract protected function getAliasWord(): string;

final protected function configure(): void
{
parent::configure();

$this
->setDescription(sprintf('Load default fixtures for %s "%s"', $this->getAliasWord(), $this->alias))
->addOption('append', null, InputOption::VALUE_NONE, 'Append the fixtures instead of purging the storage');
->addOption(
self::OPTION_APPEND,
null,
InputOption::VALUE_NONE,
'Append the fixtures instead of purging the storage'
);
}

final protected function execute(InputInterface $input, OutputInterface $output): int
{
$fixtureType = static::getFixtureType();

$appendOption = $input->getOption('append');

$ui = new SymfonyStyle($input, $output);

if (!$appendOption &&
!$ui->confirm(
sprintf('Careful, Fixture type "%s" named "%s" will be purged. Do you want to continue?', $fixtureType, $this->alias),
if (!($append = (bool) filter_var($input->getOption(self::OPTION_APPEND), FILTER_VALIDATE_BOOLEAN)) &&
!(new SymfonyStyle($input, $output))->confirm(
sprintf(
'Careful, Fixture type "%s" named "%s" will be purged. Do you want to continue?',
$fixtureType,
$this->alias
),
!$input->isInteractive()
)
) {
return 0;
}

$this->orchestrator->execute($this->fixturesClassNames, $appendOption);
$this->orchestrator->execute($this->fixturesClassNames, $append);

$output->writeln(
sprintf(
21 changes: 21 additions & 0 deletions src/DependencyInjection/Compiler/AbstractCompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);

namespace Kununu\TestingBundle\DependencyInjection\Compiler;

use Kununu\TestingBundle\DependencyInjection\ExtensionConfigurationInterface;
use Kununu\TestingBundle\DependencyInjection\KununuTestingExtension;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

abstract class AbstractCompilerPass implements CompilerPassInterface
{
protected function getExtensionConfiguration(ContainerBuilder $containerBuilder): ?array
{
$extension = $containerBuilder->hasExtension(KununuTestingExtension::ALIAS)
? $containerBuilder->getExtension(KununuTestingExtension::ALIAS)
: null;

return $extension instanceof ExtensionConfigurationInterface ? $extension->getConfig() : null;
}
}
39 changes: 24 additions & 15 deletions src/DependencyInjection/Compiler/AbstractConnectionCompilerPass.php
Original file line number Diff line number Diff line change
@@ -5,20 +5,19 @@

use Kununu\DataFixtures\Loader\ConnectionFixturesLoader;
use Kununu\TestingBundle\Service\Orchestrator;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

abstract class AbstractConnectionCompilerPass implements CompilerPassInterface
abstract class AbstractConnectionCompilerPass extends AbstractCompilerPass
{
use LoadFixturesCommandsTrait;

private const EXCLUDED_TABLES_CONFIG = 'excluded_tables';
private const LOAD_COMMAND_FIXTURES_CLASSES_NAMESPACE_CONFIG = 'load_command_fixtures_classes_namespace';
private const ORCHESTRATOR_SERVICE_PREFIX = 'kununu_testing.orchestrator.%s';

private $orchestratorServicePrefix;
private string $orchestratorServicePrefix;

public function __construct()
{
@@ -36,7 +35,7 @@ public function process(ContainerBuilder $container): void
foreach ($connections as $connName => $connId) {
$connConfigsParameterName = sprintf('kununu_testing.%s.%s', $this->getSectionName(), $connName);

// Connection is not configured for kununu\testing-bundle
// Connection is not configured for usage with this bundle
if (!$container->hasParameter($connConfigsParameterName)) {
continue;
}
@@ -82,37 +81,47 @@ private function buildConnectionOrchestrator(
string $connName,
array $connConfig
): string {
$excludedTables = $connConfig[self::EXCLUDED_TABLES_CONFIG] ?? [];

$connection = new Reference($id);

// Purger Definition for the Connection with provided $id
$purgerId = sprintf('%s.%s.purger', $this->orchestratorServicePrefix, $connName);
$purgerDefinition = new Definition($this->getConnectionPurgerClass(), [$connection, $excludedTables]);
$purgerDefinition = new Definition(
$this->getConnectionPurgerClass(),
[
$connection = new Reference($id),
$connConfig[self::EXCLUDED_TABLES_CONFIG] ?? [],
]
);
$container->setDefinition($purgerId, $purgerDefinition);

// Executor Definition for the Connection with provided $id
$executorId = sprintf('%s.%s.executor', $this->orchestratorServicePrefix, $connName);
$executorDefinition = new Definition($this->getConnectionExecutorClass(), [$connection, new Reference($purgerId)]);
$executorDefinition = new Definition(
$this->getConnectionExecutorClass(),
[
$connection,
new Reference($purgerId),
]
);
$container->setDefinition($executorId, $executorDefinition);

// Loader Definition for the Connection with provided $id
$loaderId = sprintf('%s.%s.loader', $this->orchestratorServicePrefix, $connName);
$loaderDefinition = new Definition(ConnectionFixturesLoader::class);
$container->setDefinition($loaderId, $loaderDefinition);

$connectionOrchestratorDefinition = new Definition(
// Orchestrator definition for the Connection with provided $id
$orchestratorDefinition = new Definition(
Orchestrator::class,
[
new Reference($executorId),
new Reference($loaderId),
]
);
$connectionOrchestratorDefinition->setPublic(true);
$orchestratorDefinition->setPublic(true);

$orchestratorId = sprintf('%s.%s', $this->orchestratorServicePrefix, $connName);

$container->setDefinition($orchestratorId, $connectionOrchestratorDefinition);
$container->setDefinition(
$orchestratorId = sprintf('%s.%s', $this->orchestratorServicePrefix, $connName),
$orchestratorDefinition
);

return $orchestratorId;
}
Loading

0 comments on commit 6dd9c68

Please sign in to comment.