Skip to content

Commit

Permalink
Merge branch '2.1.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
ostrolucky committed Oct 18, 2020
2 parents 9e52d89 + 4c767f2 commit 6396475
Show file tree
Hide file tree
Showing 25 changed files with 217 additions and 89 deletions.
2 changes: 1 addition & 1 deletion .doctrine-project.json
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,4 @@
"maintained": false
}
]
}
}
39 changes: 39 additions & 0 deletions .github/workflows/coding-standards.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@

name: "Coding Standards"

on: ["pull_request", "push"]

jobs:
coding-standards:
name: "Coding Standards"
runs-on: "ubuntu-20.04"

strategy:
matrix:
php-version:
- "7.4"

steps:
- name: "Checkout"
uses: "actions/checkout@v2"

- name: "Install PHP"
uses: "shivammathur/setup-php@v2"
with:
coverage: "none"
php-version: "${{ matrix.php-version }}"
tools: "cs2pr"

- name: "Cache dependencies installed with Composer"
uses: "actions/cache@v2"
with:
path: "~/.composer/cache"
key: "php-${{ matrix.php-version }}-composer-locked-${{ hashFiles('composer.lock') }}"
restore-keys: "php-${{ matrix.php-version }}-composer-locked-"

- name: "Install dependencies with Composer"
run: "composer install --no-interaction --no-progress --no-suggest"

# https://github.com/doctrine/.github/issues/3
- name: "Run PHP_CodeSniffer"
run: "vendor/bin/phpcs -q --no-colors --report=checkstyle | cs2pr"
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ vendor
composer.phar
composer.lock
phpunit.xml
/.phpunit.result.cache
/phpcs.xml
/.phpcs-cache
6 changes: 0 additions & 6 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,6 @@ jobs:
install:
- travis_retry composer update -n --prefer-dist

- stage: Code Quality
env: CODING_STANDARDS
php: 7.3
script:
- ./vendor/bin/phpcs

- stage: Coverage
php: 7.3
install:
Expand Down
5 changes: 5 additions & 0 deletions Command/CreateDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params = $params['master'];
}

// Since doctrine/dbal 2.11 master has been replaced by primary
if (isset($params['primary'])) {
$params = $params['primary'];
}

// Cannot inject `shard` option in parent::getDoctrineConnection
// cause it will try to connect to a non-existing database
if (isset($params['shards'])) {
Expand Down
7 changes: 6 additions & 1 deletion Command/DropDatabaseDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ protected function execute(InputInterface $input, OutputInterface $output)
$params = $params['master'];
}

// Since doctrine/dbal 2.11 master has been replaced by primary
if (isset($params['primary'])) {
$params = $params['primary'];
}

if (isset($params['shards'])) {
$shards = $params['shards'];
// Default select global
Expand All @@ -74,7 +79,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
foreach ($shards as $shard) {
if ($shard['id'] === (int) $input->getOption('shard')) {
// Select sharded database
$params = $shard;
$params = array_merge($params, $shard);
unset($params['id']);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Command/Proxy/ImportDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ protected function configure()
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
@trigger_error(sprintf('The "%s" (doctrine:database:import) command is deprecated, use a database client instead.', ImportDoctrineCommand::class), E_USER_DEPRECATED);
@trigger_error(sprintf('The "%s" (doctrine:database:import) command is deprecated, use a database client instead.', self::class), E_USER_DEPRECATED);

DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('connection'));

Expand Down
4 changes: 4 additions & 0 deletions Command/Proxy/RunSqlDoctrineCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ protected function execute(InputInterface $input, OutputInterface $output)
{
DoctrineCommandHelper::setApplicationConnection($this->getApplication(), $input->getOption('connection'));

// compatibility with doctrine/dbal 2.11+
// where this option is also present and unsupported before we are not switching to use a ConnectionProvider
$input->setOption('connection', null);

return parent::execute($input, $output);
}
}
10 changes: 8 additions & 2 deletions ConnectionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver\AbstractMySQLDriver;
use Doctrine\DBAL\DriverManager;
use Doctrine\DBAL\Exception;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\Type;
Expand Down Expand Up @@ -47,7 +48,10 @@ public function createConnection(array $params, Configuration $config = null, Ev
$wrapperClass = null;
if (isset($params['wrapperClass'])) {
if (! is_subclass_of($params['wrapperClass'], Connection::class)) {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
if (class_exists(DBALException::class)) {
throw DBALException::invalidWrapperClass($params['wrapperClass']);
}
throw Exception::invalidWrapperClass($params['wrapperClass']);
}

$wrapperClass = $params['wrapperClass'];
Expand Down Expand Up @@ -97,13 +101,15 @@ public function createConnection(array $params, Configuration $config = null, Ev
* For details have a look at DoctrineBundle issue #673.
*
* @throws DBALException
* @throws Exception
*/
private function getDatabasePlatform(Connection $connection) : AbstractPlatform
{
try {
return $connection->getDatabasePlatform();
} catch (DriverException $driverException) {
throw new DBALException(
$exceptionClass = class_exists(DBALException::class)? DBALException::class : Exception::class;
throw new $exceptionClass(
'An exception occurred while establishing a connection to figure out your platform version.' . PHP_EOL .
"You can circumvent this by setting a 'server_version' configuration value" . PHP_EOL . PHP_EOL .
'For further information have a look at:' . PHP_EOL .
Expand Down
2 changes: 1 addition & 1 deletion DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ private function getOrmEntityManagersNode() : ArrayNodeDefinition
->append($this->getOrmCacheDriverNode('region_cache_driver'))
->scalarNode('region_lock_lifetime')->defaultValue(60)->end()
->booleanNode('log_enabled')->defaultValue($this->debug)->end()
->scalarNode('region_lifetime')->defaultValue(0)->end()
->scalarNode('region_lifetime')->defaultValue(3600)->end()
->booleanNode('enabled')->defaultValue(true)->end()
->scalarNode('factory')->end()
->end()
Expand Down
6 changes: 5 additions & 1 deletion DependencyInjection/DoctrineExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ protected function loadOrmEntityManagerMappingInformation(array $entityManager,
* entity_managers:
* default:
* second_level_cache:
* region_lifetime: 3600
* region_lock_lifetime: 60
* region_cache_driver: apc
* log_enabled: true
* regions:
Expand Down Expand Up @@ -633,7 +635,9 @@ protected function loadOrmSecondLevelCache(array $entityManager, Definition $orm
$regionsId = sprintf('doctrine.orm.%s_second_level_cache.regions_configuration', $entityManager['name']);
$driverId = $driverId ?: sprintf('doctrine.orm.%s_second_level_cache.region_cache_driver', $entityManager['name']);
$configDef = $container->setDefinition($configId, new Definition('%doctrine.orm.second_level_cache.cache_configuration.class%'));
$regionsDef = $container->setDefinition($regionsId, new Definition('%doctrine.orm.second_level_cache.regions_configuration.class%'));
$regionsDef = $container
->setDefinition($regionsId, new Definition('%doctrine.orm.second_level_cache.regions_configuration.class%'))
->setArguments([$entityManager['second_level_cache']['region_lifetime'], $entityManager['second_level_cache']['region_lock_lifetime']]);

$slcFactoryId = sprintf('doctrine.orm.%s_second_level_cache.default_cache_factory', $entityManager['name']);
$factoryClass = isset($entityManager['second_level_cache']['factory']) ? $entityManager['second_level_cache']['factory'] : '%doctrine.orm.second_level_cache.default_cache_factory.class%';
Expand Down
15 changes: 12 additions & 3 deletions Tests/Command/CreateDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,10 @@ public function testExecute() : void
array_merge(['command' => $command->getName()])
);

$this->assertContains('Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName, $commandTester->getDisplay());
$this->assertStringContainsString(
'Created database ' . sys_get_temp_dir() . '/' . $dbName . ' for connection named ' . $connectionName,
$commandTester->getDisplay()
);
}

/**
Expand Down Expand Up @@ -81,12 +84,18 @@ public function testExecuteWithShardAlias(string $shardOption) : void
$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(), $shardOption => 1]);

$this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_1 for connection named ' . $connectionName, $commandTester->getDisplay());
$this->assertStringContainsString(
'Created database ' . sys_get_temp_dir() . '/shard_1 for connection named ' . $connectionName,
$commandTester->getDisplay()
);

$commandTester = new CommandTester($command);
$commandTester->execute(['command' => $command->getName(), $shardOption => 2]);

$this->assertContains('Created database ' . sys_get_temp_dir() . '/shard_2 for connection named ' . $connectionName, $commandTester->getDisplay());
$this->assertStringContainsString(
'Created database ' . sys_get_temp_dir() . '/shard_2 for connection named ' . $connectionName,
$commandTester->getDisplay()
);
}

public function provideShardOption() : Generator
Expand Down
16 changes: 12 additions & 4 deletions Tests/Command/DropDatabaseDoctrineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Bundle\DoctrineBundle\Command\DropDatabaseDoctrineCommand;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Exception;
use Generator;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -37,7 +38,7 @@ public function testExecute(array $options) : void
array_merge(['command' => $command->getName()], $options)
);

$this->assertContains(
$this->assertStringContainsString(
sprintf(
'Dropped database %s for connection named %s',
sys_get_temp_dir() . '/' . $dbName,
Expand All @@ -52,7 +53,11 @@ public function testExecute(array $options) : void
*/
public function testItThrowsWhenUsingIfExistsWithAnIncompatibleDriver(array $options) : void
{
static::expectException(DBALException::class);
if (class_exists(DBALException::class)) {
$this->expectException(DBALException::class);
} else {
$this->expectException(Exception::class);
}
$this->testExecute($options);
}

Expand All @@ -77,15 +82,18 @@ public function testExecuteWithoutOptionForceWillFailWithAttentionMessage() : vo
array_merge(['command' => $command->getName()])
);

$this->assertContains(
$this->assertStringContainsString(
sprintf(
'Would drop the database %s for connection named %s.',
sys_get_temp_dir() . '/' . $dbName,
$connectionName
),
$commandTester->getDisplay()
);
$this->assertContains('Please run the operation with --force to execute', $commandTester->getDisplay());
$this->assertStringContainsString(
'Please run the operation with --force to execute',
$commandTester->getDisplay()
);
}

public function provideForceOption() : Generator
Expand Down
31 changes: 23 additions & 8 deletions Tests/Command/ImportMappingDoctrineCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Doctrine\Bundle\DoctrineBundle\Tests\DependencyInjection\Fixtures\TestKernel;
use Doctrine\ORM\EntityManagerInterface;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
Expand Down Expand Up @@ -70,7 +71,11 @@ public function testExecuteXmlWithBundle() : void

$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Resources/config/doctrine/Product.orm.xml';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
$this->assertStringContainsString(
'"Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity\Product"',
file_get_contents($expectedMetadataPath),
'Metadata contains correct namespace'
);
}

public function testExecuteAnnotationsWithBundle() : void
Expand All @@ -82,15 +87,17 @@ public function testExecuteAnnotationsWithBundle() : void

$expectedMetadataPath = sys_get_temp_dir() . '/import_mapping_bundle/Entity/Product.php';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;', file_get_contents($expectedMetadataPath), 'File contains correct namespace');
$this->assertStringContainsString(
'namespace Doctrine\Bundle\DoctrineBundle\Tests\Command\Entity;',
file_get_contents($expectedMetadataPath),
'File contains correct namespace'
);
}

/**
* @expectedException \InvalidArgumentException
* @expectedExceptionMessageRegExp /The --path option is required/
*/
public function testExecuteThrowsExceptionWithNamespaceAndNoPath() : void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The --path option is required');
$this->commandTester->execute(['name' => 'Some\Namespace']);
}

Expand All @@ -103,7 +110,11 @@ public function testExecuteXmlWithNamespace() : void

$expectedMetadataPath = $this->kernel->getProjectDir() . '/config/doctrine/Product.orm.xml';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('"Some\Namespace\Entity\Product"', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
$this->assertStringContainsString(
'"Some\Namespace\Entity\Product"',
file_get_contents($expectedMetadataPath),
'Metadata contains correct namespace'
);
}

public function testExecuteAnnotationsWithNamespace() : void
Expand All @@ -116,7 +127,11 @@ public function testExecuteAnnotationsWithNamespace() : void

$expectedMetadataPath = $this->kernel->getProjectDir() . '/src/Entity/Product.php';
$this->assertFileExists($expectedMetadataPath);
$this->assertContains('namespace Some\Namespace\Entity;', file_get_contents($expectedMetadataPath), 'Metadata contains correct namespace');
$this->assertStringContainsString(
'namespace Some\Namespace\Entity;',
file_get_contents($expectedMetadataPath),
'Metadata contains correct namespace'
);
}
}

Expand Down
22 changes: 18 additions & 4 deletions Tests/ConnectionFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@

use Doctrine\Bundle\DoctrineBundle\ConnectionFactory;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\DBALException;
use Doctrine\DBAL\Driver;
use Doctrine\DBAL\Driver\API\ExceptionConverter;
use Doctrine\DBAL\Driver\Exception as TheDriverException;
use Doctrine\DBAL\Exception\DriverException;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Platforms\MySqlPlatform;
use Exception;

class ConnectionFactoryTest extends TestCase
{
/**
* @expectedException \Doctrine\DBAL\DBALException
*/
public function testContainer() : void
{
$typesConfig = [];
Expand All @@ -23,11 +23,20 @@ public function testContainer() : void
$config = null;
$eventManager = null;
$mappingTypes = [0];
$exception = new DriverException('', $this->createMock(Driver\AbstractDriverException::class));
$exception = new DriverException('', $this->createMock(
class_exists(Driver\AbstractDriverException::class) ?
Driver\AbstractDriverException::class :
TheDriverException::class
));

// put the mock into the fake driver
FakeDriver::$exception = $exception;

if (class_exists(DBALException::class)) {
$this->expectException(DBALException::class);
} else {
$this->expectException(\Doctrine\DBAL\Exception::class);
}
try {
$factory->createConnection($params, $config, $eventManager, $mappingTypes);
} catch (Exception $e) {
Expand Down Expand Up @@ -125,6 +134,11 @@ public function getDatabase(Connection $conn) : string
{
return 'fake_db';
}

public function getExceptionConverter() : ExceptionConverter
{
throw new Exception('not implemented');
}
}

class FakeConnection extends Connection
Expand Down
Loading

0 comments on commit 6396475

Please sign in to comment.