Skip to content

Commit 038bcc3

Browse files
authored
Upgrade to Symfony 7.1 + new features (#240)
1 parent 272f0c7 commit 038bcc3

21 files changed

+230
-150
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ jobs:
105105
- '8.2'
106106
symfony-version:
107107
- '7.0'
108+
- '7.1'
108109
steps:
109110
- name: 'Checkout Code'
110111
uses: actions/checkout@v4
@@ -152,14 +153,15 @@ jobs:
152153
run: ./vendor/bin/phpunit -c phpunit.xml.dist --coverage-clover=coverage.xml
153154

154155
- name: 'Download Coverage Files'
155-
uses: actions/download-artifact@v2
156+
uses: actions/download-artifact@v4
156157
with:
157158
path: reports
158159

159160
- name: 'Upload to Codecov'
160-
uses: codecov/codecov-action@v3
161+
uses: codecov/codecov-action@v4
161162
with:
162163
files: ./coverage.xml
163164
fail_ci_if_error: true
164165
flags: unittests
165166
verbose: true
167+
token: ${{ secrets.CODECOV_TOKEN }}

Command/EnumDropCommentCommand.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,11 @@
1212

1313
namespace Fresh\DoctrineEnumBundle\Command;
1414

15-
use Doctrine\DBAL\Platforms\AbstractPlatform;
1615
use Doctrine\ORM\EntityManagerInterface;
1716
use Doctrine\Persistence\ManagerRegistry;
1817
use Fresh\DoctrineEnumBundle\DBAL\Types\AbstractEnumType;
1918
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException;
2019
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
21-
use Fresh\DoctrineEnumBundle\Exception\RuntimeException;
2220
use Symfony\Component\Console\Attribute\AsCommand;
2321
use Symfony\Component\Console\Command\Command;
2422
use Symfony\Component\Console\Input\InputArgument;
@@ -75,6 +73,16 @@ public function __construct(private readonly ManagerRegistry $registry, array $r
7573
}
7674
}
7775

76+
/**
77+
* @return \Closure
78+
*/
79+
public function getEnumTypesForAutocompletion(): \Closure
80+
{
81+
return function () {
82+
return \array_keys($this->registeredEnumTypes);
83+
};
84+
}
85+
7886
/**
7987
* {@inheritdoc}
8088
*/
@@ -83,7 +91,7 @@ protected function configure(): void
8391
$this
8492
->setDefinition(
8593
new InputDefinition([
86-
new InputArgument('enumType', InputArgument::REQUIRED, 'Registered ENUM type'),
94+
new InputArgument('enumType', InputArgument::REQUIRED, 'Registered ENUM type', null, $this->getEnumTypesForAutocompletion()),
8795
new InputOption('em', null, InputOption::VALUE_OPTIONAL, 'The entity manager to use for this command'),
8896
])
8997
)
@@ -143,9 +151,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
143151
$connection = $this->em->getConnection();
144152

145153
$platform = $connection->getDatabasePlatform();
146-
if (!$platform instanceof AbstractPlatform) {
147-
throw new RuntimeException('Missing database platform for connection.', 3);
148-
}
149154

150155
$io->title(\sprintf('Dropping comments for <info>%s</info> type...', $this->enumType));
151156

@@ -157,13 +162,17 @@ protected function execute(InputInterface $input, OutputInterface $output): int
157162

158163
foreach ($allMetadata as $metadata) {
159164
$entityName = $metadata->getName();
160-
$tableName = $metadata->getTableName();
165+
if (!empty($metadata->getSchemaName())) {
166+
$tableName = $metadata->getSchemaName().'.'.$metadata->getTableName();
167+
} else {
168+
$tableName = $metadata->getTableName();
169+
}
161170

162171
foreach ($metadata->getFieldNames() as $fieldName) {
163172
if ($metadata->getTypeOfField($fieldName) === $this->enumType) {
164173
/** @var array{columnName: string} $fieldMappingDetails */
165174
$fieldMappingDetails = $metadata->getFieldMapping($fieldName);
166-
$sql = $platform->getCommentOnColumnSQL($tableName, $fieldMappingDetails['columnName'], null);
175+
$sql = $platform->getCommentOnColumnSQL($tableName, $fieldMappingDetails['columnName'], 'NULL');
167176
$connection->executeQuery($sql);
168177

169178
$io->text(\sprintf(' * %s::$%s <info>Dropped ✔</info>', $entityName, $fieldName));

DBAL/Types/AbstractEnumType.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use Doctrine\DBAL\Platforms\AbstractPlatform;
1616
use Doctrine\DBAL\Platforms\MySQLPlatform;
1717
use Doctrine\DBAL\Platforms\PostgreSQLPlatform;
18-
use Doctrine\DBAL\Platforms\SqlitePlatform;
18+
use Doctrine\DBAL\Platforms\SQLitePlatform;
1919
use Doctrine\DBAL\Platforms\SQLServerPlatform;
2020
use Doctrine\DBAL\Types\Type;
2121
use Fresh\DoctrineEnumBundle\Exception\InvalidArgumentException;
@@ -51,7 +51,7 @@ abstract class AbstractEnumType extends Type
5151
*
5252
* @return TValue|int|string
5353
*/
54-
public function convertToDatabaseValue($value, AbstractPlatform $platform)
54+
public function convertToDatabaseValue(mixed $value, AbstractPlatform $platform): mixed
5555
{
5656
if (null !== $value && !isset(static::$choices[$value])) {
5757
throw new InvalidArgumentException(\sprintf('Invalid value "%s" for ENUM "%s".', $value, $this->getName()));
@@ -66,7 +66,7 @@ public function convertToDatabaseValue($value, AbstractPlatform $platform)
6666
*
6767
* @return TValue
6868
*/
69-
public function convertToPHPValue($value, AbstractPlatform $platform)
69+
public function convertToPHPValue($value, AbstractPlatform $platform): mixed
7070
{
7171
if (!isset(static::$choices[$value])) {
7272
return $value;
@@ -104,7 +104,7 @@ static function (int|string $value) {
104104
);
105105

106106
$sqlDeclaration = match (true) {
107-
$platform instanceof SqlitePlatform => \sprintf('TEXT CHECK(%s IN (%s))', $column['name'], $values),
107+
$platform instanceof SQLitePlatform => \sprintf('TEXT CHECK(%s IN (%s))', $column['name'], $values),
108108
$platform instanceof PostgreSQLPlatform, $platform instanceof SQLServerPlatform => \sprintf(
109109
'VARCHAR(255) CHECK(%s IN (%s))',
110110
$column['name'],

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222

2323
##### Choose the version you need
2424

25-
| Bundle Version (X.Y.Z) | PHP | Symfony | Doctrine Bundle | Comment |
25+
| Bundle Version (X.Y.Z) | PHP | Symfony | Doctrine Bundle | Comment |
2626
|:----------------------:|:--------:|:----------:|:---------------:|:--------------------|
27-
| `10.1.*` | `>= 8.2` | `>= 7.0` | `>= 2.11` | **Current version** |
28-
| `9.2.*` | `>= 8.1` | `>= 6.1` | `>= 2.9` | Previous version |
27+
| `11.0.*` | `>= 8.2` | `>= 7.0` | `>= 2.11` | **Current version** |
28+
| `10.1.*` | `>= 8.2` | `>= 7.0` | `>= 2.11` | Previous |
2929

3030
#### Check the `config/bundles.php` file
3131

Resources/docs/usage_example.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ Register `BasketballPositionType` for Doctrine in config.yaml:
4141

4242
```yaml
4343
doctrine:
44-
dbal:
45-
types:
46-
BasketballPositionType: App\DBAL\Types\BasketballPositionType
44+
dbal:
45+
types:
46+
BasketballPositionType: App\DBAL\Types\BasketballPositionType
4747
```
4848
4949
Create a `Player` entity that has a `position` field:
@@ -60,9 +60,9 @@ use Fresh\DoctrineEnumBundle\Validator\Constraints as DoctrineAssert;
6060
#[ORM\Table(name: 'players')]
6161
class Player
6262
{
63-
#[ORM\Id]
64-
#[ORM\Column(type: 'integer', name: 'id')]
65-
#[ORM\GeneratedValue(strategy: 'AUTO')]
63+
#[ORM\Id]
64+
#[ORM\Column(type: 'integer', name: 'id')]
65+
#[ORM\GeneratedValue(strategy: 'AUTO')]
6666
private $id;
6767
6868
// Note, that type of field should be same as you set in Doctrine config (in this case it is BasketballPositionType)

Tests/Command/EnumDropCommentCommandTest.php

Lines changed: 46 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
use Fresh\DoctrineEnumBundle\Exception\EnumType\EnumTypeIsRegisteredButClassDoesNotExistException;
2424
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\BasketballPositionType;
2525
use Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\TaskStatusType;
26+
use PHPUnit\Framework\Attributes\DataProvider;
27+
use PHPUnit\Framework\Attributes\Test;
2628
use PHPUnit\Framework\MockObject\MockObject;
2729
use PHPUnit\Framework\TestCase;
2830
use Symfony\Component\Console\Application;
@@ -94,7 +96,8 @@ protected function tearDown(): void
9496
);
9597
}
9698

97-
public function testExceptionInConstructor(): void
99+
#[Test]
100+
public function exceptionInConstructor(): void
98101
{
99102
$this->expectException(EnumTypeIsRegisteredButClassDoesNotExistException::class);
100103
$this->expectExceptionMessage('ENUM type "CustomType" is registered as "Fresh\DoctrineEnumBundle\Tests\Fixtures\DBAL\Types\CustomType", but that class does not exist');
@@ -110,7 +113,8 @@ public function testExceptionInConstructor(): void
110113
$this->commandTester->getDisplay();
111114
}
112115

113-
public function testExceptionOnExecution(): void
116+
#[Test]
117+
public function exceptionOnExecution(): void
114118
{
115119
$this->em
116120
->expects(self::once())
@@ -130,7 +134,8 @@ public function testExceptionOnExecution(): void
130134
self::assertStringContainsString('test', $output);
131135
}
132136

133-
public function testInvalidEnumTypeArgument(): void
137+
#[Test]
138+
public function invalidEnumTypeArgument(): void
134139
{
135140
$result = $this->commandTester->execute(
136141
[
@@ -144,7 +149,8 @@ public function testInvalidEnumTypeArgument(): void
144149
self::assertStringContainsString('Argument "enumType" is not a string.', $output);
145150
}
146151

147-
public function testExceptionNotRegisteredEnumType(): void
152+
#[Test]
153+
public function exceptionNotRegisteredEnumType(): void
148154
{
149155
$result = $this->commandTester->execute(
150156
[
@@ -158,27 +164,8 @@ public function testExceptionNotRegisteredEnumType(): void
158164
self::assertStringContainsString('Argument "enumType" is not a registered ENUM type.', $output);
159165
}
160166

161-
public function testMissingDatabasePlatformForConnection(): void
162-
{
163-
$this->connection
164-
->expects(self::once())
165-
->method('getDatabasePlatform')
166-
->willReturn(null)
167-
;
168-
169-
$result = $this->commandTester->execute(
170-
[
171-
'command' => $this->command->getName(),
172-
'enumType' => 'TaskStatusType',
173-
]
174-
);
175-
self::assertSame(3, $result);
176-
177-
$output = $this->commandTester->getDisplay();
178-
self::assertStringContainsString('Missing database platform for connection.', $output);
179-
}
180-
181-
public function testExecutionWithCaughtException(): void
167+
#[Test]
168+
public function executionWithCaughtException(): void
182169
{
183170
$this->connection
184171
->expects(self::once())
@@ -198,7 +185,8 @@ public function testExecutionWithCaughtException(): void
198185
self::assertStringContainsString('test', $output);
199186
}
200187

201-
public function testSuccessfulExecutionWithNoMetadata(): void
188+
#[Test]
189+
public function successfulExecutionWithNoMetadata(): void
202190
{
203191
$this->connection
204192
->expects(self::once())
@@ -226,7 +214,9 @@ public function testSuccessfulExecutionWithNoMetadata(): void
226214
self::assertStringContainsString('NO METADATA FOUND', $output);
227215
}
228216

229-
public function testSuccessfulExecutionWithMetadata(): void
217+
#[Test]
218+
#[DataProvider('dataProviderForMetadataTest')]
219+
public function successfulExecutionWithMetadata(?string $schemaName, string $sqlColumnComment): void
230220
{
231221
$this->connection
232222
->expects(self::once())
@@ -242,12 +232,15 @@ public function testSuccessfulExecutionWithMetadata(): void
242232
;
243233

244234
$metadata->expects(self::once())->method('getName')->willReturn('Task');
235+
$metadata->expects(self::atLeast(1))->method('getSchemaName')->willReturn($schemaName);
245236
$metadata->expects(self::once())->method('getTableName')->willReturn('tasks');
246237
$metadata->expects(self::once())->method('getFieldNames')->willReturn(['status']);
247238
$metadata->expects(self::once())->method('getTypeOfField')->with('status')->willReturn('TaskStatusType');
248-
$metadata->expects(self::once())->method('getFieldMapping')->with('status')->willReturn(FieldMapping::fromMappingArray(['type'=> 'string', 'columnName' => 'task_column_name', 'fieldName' => 'test']));
239+
$metadata->expects(self::once())->method('getFieldMapping')->with('status')->willReturn(
240+
FieldMapping::fromMappingArray(['type'=> 'string', 'columnName' => 'task_column_name', 'fieldName' => 'test'])
241+
);
249242

250-
$this->platform->expects(self::once())->method('getCommentOnColumnSQL')->with('tasks', 'task_column_name', null)->willReturn('test SQL');
243+
$this->platform->expects(self::once())->method('getCommentOnColumnSQL')->with($sqlColumnComment, 'task_column_name', 'NULL')->willReturn('test SQL');
251244

252245
$this->connection->expects(self::once())->method('executeQuery')->with('test SQL');
253246

@@ -266,4 +259,28 @@ public function testSuccessfulExecutionWithMetadata(): void
266259
self::assertStringContainsString('TOTAL: 1', $output);
267260
self::assertStringContainsString('DONE', $output);
268261
}
262+
263+
public static function dataProviderForMetadataTest(): iterable
264+
{
265+
yield 'no schema' => [
266+
'schemaName' => null,
267+
'sqlColumnComment' => 'tasks',
268+
];
269+
yield 'public schema' => [
270+
'schemaName' => 'public',
271+
'sqlColumnComment' => 'public.tasks',
272+
];
273+
yield 'custom schema' => [
274+
'schemaName' => 'custom',
275+
'sqlColumnComment' => 'custom.tasks',
276+
];
277+
}
278+
279+
#[Test]
280+
public function autocomplete(): void
281+
{
282+
$enumTypes = $this->command->getEnumTypesForAutocompletion()();
283+
284+
self::assertSame(['BasketballPositionType', 'TaskStatusType'], $enumTypes);
285+
}
269286
}

0 commit comments

Comments
 (0)