Skip to content

Commit

Permalink
Merge branch '3.8.x' into 4.0.x
Browse files Browse the repository at this point in the history
* 3.8.x:
  Run tests on MySQL 8.2 (doctrine#6207)
  MySQLSchemaManager. Check expected database type for json columns only. (doctrine#6189)
  Make the type annotation for CompositeExpression::count more specific (doctrine#6188)
  • Loading branch information
derrabus committed Nov 3, 2023
2 parents fa47aee + ca46057 commit 6889209
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ jobs:
mysql-version:
- "5.7"
- "8.0"
- "8.1"
- "8.2"
extension:
- "mysqli"
- "pdo_mysql"
Expand Down
2 changes: 2 additions & 0 deletions src/Query/Expression/CompositeExpression.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public function with(self|string $part, self|string ...$parts): self

/**
* Retrieves the amount of expressions on composite expression.
*
* @psalm-return int<0, max>
*/
public function count(): int
{
Expand Down
80 changes: 80 additions & 0 deletions tests/Functional/Schema/CustomIntrospectionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

declare(strict_types=1);

namespace Doctrine\DBAL\Tests\Functional\Schema;

use Doctrine\DBAL\Platforms\AbstractMySQLPlatform;
use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Schema\AbstractSchemaManager;
use Doctrine\DBAL\Schema\Comparator;
use Doctrine\DBAL\Schema\Table;
use Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType;
use Doctrine\DBAL\Tests\FunctionalTestCase;
use Doctrine\DBAL\Types\Type;

use function array_map;
use function implode;
use function sprintf;

/**
* Tests introspection of a custom column type with an underlying decimal column
* on MySQL and MariaDb platforms.
*
* See bug #6185
*/
class CustomIntrospectionTest extends FunctionalTestCase
{
private AbstractSchemaManager $schemaManager;

private Comparator $comparator;

private AbstractPlatform $platform;

public static function setUpBeforeClass(): void
{
Type::addType('money', MoneyType::class);
}

protected function setUp(): void
{
$this->platform = $this->connection->getDatabasePlatform();

if (! $this->platform instanceof AbstractMySQLPlatform) {
self::markTestSkipped();
}

$this->schemaManager = $this->connection->createSchemaManager();
$this->comparator = $this->schemaManager->createComparator();
}

public function testCustomColumnIntrospection(): void
{
$tableName = 'test_custom_column_introspection';
$table = new Table($tableName);

$table->addColumn('id', 'integer');
$table->addColumn('quantity', 'decimal');
$table->addColumn('amount', 'money', [
'notnull' => false,
'scale' => 2,
'precision' => 10,
]);

$this->dropAndCreateTable($table);

$onlineTable = $this->schemaManager->introspectTable($tableName);

$diff = $this->comparator->compareTables($table, $onlineTable);
$changedCols = [];

if (! $diff->isEmpty()) {
$changedCols = array_map(static fn ($c) => $c->getOldColumnName()->getName(), $diff->getModifiedColumns());

Check failure on line 72 in tests/Functional/Schema/CustomIntrospectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

MissingClosureReturnType

tests/Functional/Schema/CustomIntrospectionTest.php:72:38: MissingClosureReturnType: Closure does not have a return type, expecting mixed (see https://psalm.dev/068)

Check failure on line 72 in tests/Functional/Schema/CustomIntrospectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UndefinedMethod

tests/Functional/Schema/CustomIntrospectionTest.php:72:60: UndefinedMethod: Method Doctrine\DBAL\Schema\ColumnDiff::getOldColumnName does not exist (see https://psalm.dev/022)

Check failure on line 72 in tests/Functional/Schema/CustomIntrospectionTest.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined method Doctrine\DBAL\Schema\ColumnDiff::getOldColumnName().
}

self::assertTrue($diff->isEmpty(), sprintf(
'Tables should be identical. Differences detected in %s.',
implode(':', $changedCols),
));
}
}
18 changes: 18 additions & 0 deletions tests/Functional/Schema/Types/Money.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

Check failure on line 1 in tests/Functional/Schema/Types/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Missing declare(strict_types=1).

namespace Doctrine\DBAL\Tests\Functional\Schema\Types;

final class Money
{
private string $value;

Check failure on line 7 in tests/Functional/Schema/Types/Money.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Required promotion of property $value.

public function __construct(string $value)
{
$this->value = $value;
}

public function __toString(): string
{
return $this->value;
}
}
72 changes: 72 additions & 0 deletions tests/Functional/Schema/Types/MoneyType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<?php

Check failure on line 1 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Coding Standards / Coding Standards (8.2)

Missing declare(strict_types=1).

namespace Doctrine\DBAL\Tests\Functional\Schema\Types;

use Doctrine\DBAL\Platforms\AbstractPlatform;
use Doctrine\DBAL\Types\ConversionException;
use Doctrine\DBAL\Types\Type;
use InvalidArgumentException;

use function is_string;

class MoneyType extends Type
{
public const NAME = 'money';

/**
* {@inheritDoc}
*/
public function getName()

Check failure on line 19 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

MissingReturnType

tests/Functional/Schema/Types/MoneyType.php:19:21: MissingReturnType: Method Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType::getName does not have a return type, expecting 'money' (see https://psalm.dev/050)

Check failure on line 19 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Method Doctrine\DBAL\Tests\Functional\Schema\Types\MoneyType::getName() has no return type specified.
{
return self::NAME;
}

/**
* {@inheritDoc}
*/
public function getSQLDeclaration(array $column, AbstractPlatform $platform): string
{
return $platform->getDecimalTypeDeclarationSQL($column);
}

/**
* {@inheritDoc}
*/
public function convertToDatabaseValue($value, AbstractPlatform $platform): ?string
{
if ($value === null) {
return $value;
}

if ($value instanceof Money) {
return $value->__toString();
}

throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', Money::class]);

Check failure on line 45 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UndefinedMethod

tests/Functional/Schema/Types/MoneyType.php:45:15: UndefinedMethod: Method Doctrine\DBAL\Types\ConversionException::conversionfailedinvalidtype does not exist (see https://psalm.dev/022)

Check failure on line 45 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailedInvalidType().
}

/**
* {@inheritDoc}
*/
public function convertToPHPValue($value, AbstractPlatform $platform): ?Money
{
if ($value === null || $value instanceof Money) {
return $value;
}

if (! is_string($value)) {
throw ConversionException::conversionFailedInvalidType($value, $this->getName(), ['null', 'string']);

Check failure on line 58 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UndefinedMethod

tests/Functional/Schema/Types/MoneyType.php:58:19: UndefinedMethod: Method Doctrine\DBAL\Types\ConversionException::conversionfailedinvalidtype does not exist (see https://psalm.dev/022)

Check failure on line 58 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailedInvalidType().
}

try {
return new Money($value);
} catch (InvalidArgumentException $e) {
throw ConversionException::conversionFailedFormat($value, $this->getName(), Money::class, $e);

Check failure on line 64 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with Psalm (8.2)

UndefinedMethod

tests/Functional/Schema/Types/MoneyType.php:64:19: UndefinedMethod: Method Doctrine\DBAL\Types\ConversionException::conversionfailedformat does not exist (see https://psalm.dev/022)

Check failure on line 64 in tests/Functional/Schema/Types/MoneyType.php

View workflow job for this annotation

GitHub Actions / Static Analysis with PHPStan (8.2)

Call to an undefined static method Doctrine\DBAL\Types\ConversionException::conversionFailedFormat().
}
}

public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
}

0 comments on commit 6889209

Please sign in to comment.