Skip to content

Commit 751082b

Browse files
committed
Fix config, tests and QA
1 parent af576dd commit 751082b

File tree

6 files changed

+152
-59
lines changed

6 files changed

+152
-59
lines changed

phpstan-baseline.neon

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,42 @@ parameters:
120120
count: 1
121121
path: src/Command/UpdateSchemaDoctrineODMCommand.php
122122

123+
-
124+
message: '#^Expression on left side of \?\? is not nullable\.$#'
125+
identifier: nullCoalesce.expr
126+
count: 1
127+
path: src/DataCollector/ConnectionDiagnostic.php
128+
129+
-
130+
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DataCollector\\ConnectionDiagnostic\:\:__construct\(\) has parameter \$driverOptions with no value type specified in iterable type array\.$#'
131+
identifier: missingType.iterableValue
132+
count: 1
133+
path: src/DataCollector/ConnectionDiagnostic.php
134+
135+
-
136+
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DataCollector\\ConnectionDiagnostic\:\:getAutoEncryptionInfo\(\) return type has no value type specified in iterable type array\.$#'
137+
identifier: missingType.iterableValue
138+
count: 1
139+
path: src/DataCollector/ConnectionDiagnostic.php
140+
141+
-
142+
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DataCollector\\ConnectionDiagnostic\:\:getPhpExtensionInfo\(\) return type has no value type specified in iterable type array\.$#'
143+
identifier: missingType.iterableValue
144+
count: 1
145+
path: src/DataCollector/ConnectionDiagnostic.php
146+
147+
-
148+
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DataCollector\\ConnectionDiagnostic\:\:getServerInfo\(\) return type has no value type specified in iterable type array\.$#'
149+
identifier: missingType.iterableValue
150+
count: 1
151+
path: src/DataCollector/ConnectionDiagnostic.php
152+
153+
-
154+
message: '#^Unreachable statement \- code above always terminates\.$#'
155+
identifier: deadCode.unreachable
156+
count: 1
157+
path: src/DataCollector/ConnectionDiagnostic.php
158+
123159
-
124160
message: '#^Cannot cast array\|bool\|float\|int\|string\|UnitEnum\|null to string\.$#'
125161
identifier: cast.string
@@ -240,6 +276,12 @@ parameters:
240276
count: 1
241277
path: src/DependencyInjection/DoctrineMongoDBExtension.php
242278

279+
-
280+
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DependencyInjection\\DoctrineMongoDBExtension\:\:loadConnections\(\) has parameter \$config with no value type specified in iterable type array\.$#'
281+
identifier: missingType.iterableValue
282+
count: 1
283+
path: src/DependencyInjection/DoctrineMongoDBExtension.php
284+
243285
-
244286
message: '#^Method Doctrine\\Bundle\\MongoDBBundle\\DependencyInjection\\DoctrineMongoDBExtension\:\:loadConnections\(\) has parameter \$connections with no value type specified in iterable type array\.$#'
245287
identifier: missingType.iterableValue
@@ -270,12 +312,6 @@ parameters:
270312
count: 1
271313
path: src/DependencyInjection/DoctrineMongoDBExtension.php
272314

273-
-
274-
message: '#^PHPDoc tag @param references unknown parameter\: \$config$#'
275-
identifier: parameter.notFound
276-
count: 1
277-
path: src/DependencyInjection/DoctrineMongoDBExtension.php
278-
279315
-
280316
message: '#^Parameter \#2 \$bundles of method Symfony\\Bridge\\Doctrine\\DependencyInjection\\AbstractDoctrineExtension\:\:fixManagersAutoMappings\(\) expects array, array\|bool\|float\|int\|string\|UnitEnum\|null given\.$#'
281317
identifier: argument.type
@@ -558,12 +594,6 @@ parameters:
558594
count: 1
559595
path: tests/DependencyInjection/ConfigurationTest.php
560596

561-
-
562-
message: '#^PHPDoc tag @param references unknown parameter\: \$configs$#'
563-
identifier: parameter.notFound
564-
count: 1
565-
path: tests/DependencyInjection/ConfigurationTest.php
566-
567597
-
568598
message: '#^Parameter \#1 \$input of static method Symfony\\Component\\Yaml\\Yaml\:\:parse\(\) expects string, string\|false given\.$#'
569599
identifier: argument.type

src/Command/ConnectionDiagnosticCommand.php

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4242
$io = new SymfonyStyle($input, $output);
4343
$io->title('MongoDB Encryption Diagnostics');
4444

45-
if (! $this->diagnostics) {
46-
$io->warning('No MongoDB connections found. Please ensure you have configured your connections correctly.');
47-
48-
return Command::SUCCESS;
49-
}
50-
5145
/** @var string[] $connectionNames */
5246
$connectionNames = $input->getOption('connection');
5347
if ($connectionNames) {
@@ -117,7 +111,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
117111
return Command::SUCCESS;
118112
}
119113

120-
/** @return string[] */
114+
/** @return list<string> */
121115
private function getConnectionNames(): array
122116
{
123117
return array_keys($this->diagnostics->getProvidedServices());

src/DependencyInjection/Configuration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,7 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
459459
// Remove empty arrays for schemaMap, encryptedFieldsMap, extraOptions, tlsOptions
460460
foreach (
461461
[
462+
'masterKey',
462463
'schemaMap',
463464
'encryptedFieldsMap',
464465
'extraOptions',
@@ -472,7 +473,6 @@ private function addConnectionsSection(ArrayNodeDefinition $rootNode): void
472473
unset($v[$key]);
473474
}
474475

475-
// Always keep kmsProviders, even if all providers are empty arrays
476476
return $v;
477477
})
478478
->end()

src/DependencyInjection/DoctrineMongoDBExtension.php

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@
4848
use Throwable;
4949

5050
use function array_diff_key;
51-
use function array_intersect_key;
5251
use function array_key_first;
5352
use function array_merge;
5453
use function class_exists;
@@ -489,7 +488,8 @@ private function loadEntityValueResolverServices(ContainerBuilder $container, Fi
489488
/**
490489
* Normalizes the driver options array
491490
*
492-
* @param array<string, mixed> $connection
491+
* @param array<string, mixed> $connection Connection configuration
492+
* @param array<string, mixed> $config Full configuration
493493
*
494494
* @return array<string, mixed>
495495
*/
@@ -504,15 +504,9 @@ private function normalizeDriverOptions(array $connection, array $config): array
504504

505505
if (isset($connection['autoEncryption'])) {
506506
$kmsProvider = $connection['autoEncryption']['kmsProvider'];
507-
$driverOptions['autoEncryption'] = array_intersect_key($connection['autoEncryption'], [
508-
'bypassAutoEncryption' => true,
509-
'bypassQueryAnalysis' => true,
510-
'encryptedFieldsMap' => true,
511-
'extraOptions' => true,
512-
'keyVaultClient' => true,
513-
'keyVaultNamespace' => true,
514-
'schemaMap' => true,
515-
'tlsOptions' => true,
507+
$driverOptions['autoEncryption'] = array_diff_key($connection['autoEncryption'], [
508+
'kmsProvider' => false,
509+
'masterKey' => false,
516510
]);
517511

518512
$driverOptions['autoEncryption']['keyVaultNamespace'] ??= $config['default_database'] . '.datakeys';
@@ -521,7 +515,7 @@ private function normalizeDriverOptions(array $connection, array $config): array
521515
}
522516

523517
$driverOptions['autoEncryption']['kmsProviders'] = [
524-
$kmsProvider['name'] => array_diff_key($kmsProvider, ['name' => true]),
518+
$kmsProvider['type'] => array_diff_key($kmsProvider, ['type' => true]),
525519
];
526520
}
527521

tests/DependencyInjection/ConfigurationTest.php

Lines changed: 99 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Doctrine\ODM\MongoDB\Configuration as ODMConfiguration;
1414
use Doctrine\ODM\MongoDB\Repository\DefaultGridFSRepository;
1515
use Doctrine\ODM\MongoDB\Repository\DocumentRepository;
16+
use Generator;
1617
use PHPUnit\Framework\TestCase;
1718
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
1819
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
@@ -407,7 +408,7 @@ public static function provideMergeOptions(): array
407408
}
408409

409410
/**
410-
* @param array $configs A configuration array to process
411+
* @param array $config A configuration array to process
411412
* @param array $expected Array of key/value options expected in the processed configuration
412413
*
413414
* @dataProvider provideNormalizeOptions
@@ -423,13 +424,11 @@ public function testNormalizeOptions(array $config, array $expected): void
423424
}
424425
}
425426

426-
/** @return array<mixed[]> */
427-
public static function provideNormalizeOptions(): array
427+
/** @return Generator<array{0: array<string, mixed>, 1: array<string, mixed>}> */
428+
public static function provideNormalizeOptions(): Generator
428429
{
429-
$cases = [];
430-
431430
// connection versus connections (id is the identifier)
432-
$cases[] = [
431+
yield [
433432
[
434433
'connection' => [
435434
['server' => 'mongodb://abc', 'id' => 'foo'],
@@ -445,7 +444,7 @@ public static function provideNormalizeOptions(): array
445444
];
446445

447446
// document_manager versus document_managers (id is the identifier)
448-
$cases[] = [
447+
yield [
449448
[
450449
'document_manager' => [
451450
['connection' => 'conn1', 'id' => 'foo'],
@@ -461,7 +460,7 @@ public static function provideNormalizeOptions(): array
461460
];
462461

463462
// mapping configuration that's beneath a specific document manager
464-
$cases[] = [
463+
yield [
465464
[
466465
'document_manager' => [
467466
[
@@ -494,7 +493,88 @@ public static function provideNormalizeOptions(): array
494493
],
495494
];
496495

497-
return $cases;
496+
// Encrypted Field Map normalization from XML tags
497+
yield [
498+
[
499+
'connection' => [
500+
[
501+
'server' => 'mongodb://abc',
502+
'id' => 'foo',
503+
'autoEncryption' => [
504+
'kmsProvider' => ['type' => 'local', 'key' => '1234567890123456789012345678901234567890123456789012345678901234'],
505+
'encryptedFieldsMap' => [
506+
'encryptedFields' => [
507+
[
508+
'name' => 'encrypted.patients',
509+
'field' => [
510+
[
511+
'path' => 'patientRecord.ssn',
512+
'bsonType' => 'string',
513+
'queries' => ['queryType' => 'equality'],
514+
],
515+
[
516+
'path' => 'patientRecord.billing',
517+
'bsonType' => 'object',
518+
],
519+
[
520+
'path' => 'patientRecord.billingAmount',
521+
'bsonType' => 'int',
522+
'queries' => ['queryType' => 'range', 'min' => 100, 'max' => 2000, 'sparsity' => 1, 'trimFactor' => 4],
523+
],
524+
],
525+
],
526+
[
527+
'name' => 'encrypted.users',
528+
'field' =>
529+
[
530+
'path' => 'email',
531+
'bsonType' => 'string',
532+
'queries' => ['queryType' => 'equality'],
533+
],
534+
],
535+
],
536+
],
537+
],
538+
],
539+
],
540+
],
541+
[
542+
'connections' => [
543+
'foo' => [
544+
'server' => 'mongodb://abc',
545+
'autoEncryption' => [
546+
'kmsProvider' => ['type' => 'local', 'key' => '1234567890123456789012345678901234567890123456789012345678901234'],
547+
'encryptedFieldsMap' => [
548+
'encrypted.patients' => [
549+
[
550+
'path' => 'patientRecord.ssn',
551+
'bsonType' => 'string',
552+
'queries' => ['queryType' => 'equality'],
553+
],
554+
[
555+
'path' => 'patientRecord.billing',
556+
'bsonType' => 'object',
557+
],
558+
[
559+
'path' => 'patientRecord.billingAmount',
560+
'bsonType' => 'int',
561+
'queries' => ['queryType' => 'range', 'min' => 100, 'max' => 2000, 'sparsity' => 1, 'trimFactor' => 4],
562+
],
563+
],
564+
'encrypted.users' => [
565+
[
566+
'path' => 'email',
567+
'bsonType' => 'string',
568+
'queries' => ['queryType' => 'equality'],
569+
],
570+
],
571+
],
572+
],
573+
574+
],
575+
],
576+
],
577+
];
498578
}
499579

500580
public function testPasswordAndUsernameShouldBeUnsetIfNull(): void
@@ -571,6 +651,11 @@ public function testNullReplicaSetValue(): void
571651
$this->assertFalse(array_key_exists('replicaSet', $processedConfig['connections']['conn1']['options']));
572652
}
573653

654+
/**
655+
* @param array<string, mixed> $config
656+
*
657+
* @return array<string, mixed>
658+
*/
574659
protected function processConfiguration(array $config): array
575660
{
576661
$processor = new Processor();
@@ -579,6 +664,11 @@ protected function processConfiguration(array $config): array
579664
return $processor->processConfiguration($configuration, [$this->getMinimalValidConfig($config)]);
580665
}
581666

667+
/**
668+
* @param array<string, mixed> $config
669+
*
670+
* @return array<string, mixed>
671+
*/
582672
protected function getMinimalValidConfig(array $config = []): array
583673
{
584674
$baseConfig = [
@@ -619,19 +709,4 @@ protected function getMinimalValidConfig(array $config = []): array
619709

620710
return array_merge($baseConfig, $config);
621711
}
622-
623-
private function assertProcessedConfigurationEquals(array $expected, array $config): void
624-
{
625-
$this->assertEquals($expected, $this->processConfiguration($config));
626-
}
627-
628-
private function assertConfigurationIsInvalid(array $config, ?string $expectedMessage = null): void
629-
{
630-
$this->expectException(InvalidConfigurationException::class);
631-
if ($expectedMessage !== null) {
632-
$this->expectExceptionMessageMatches($expectedMessage);
633-
}
634-
635-
$this->processConfiguration($config);
636-
}
637712
}

tests/DependencyInjection/DoctrineMongoDBExtensionTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ public function testAutoEncryptionWithKeyVaultClientService(): void
494494
'autoEncryption' => [
495495
'keyVaultNamespace' => 'db.vault',
496496
'keyVaultClient' => $dummyServiceId,
497-
'kmsProvider' => ['name' => 'local', 'key' => 'cGFzc3dvcmQ='],
497+
'kmsProvider' => ['type' => 'local', 'key' => 'cGFzc3dvcmQ='],
498498
],
499499
],
500500
],
@@ -530,7 +530,7 @@ public function testAutoEncryptionWithComplexKmsAndSchemaMap(): void
530530
'default' => [
531531
'autoEncryption' => [
532532
'keyVaultNamespace' => 'db.vault',
533-
'kmsProvider' => ['name' => 'aws', 'accessKeyId' => 'test', 'secretAccessKey' => 'secret'],
533+
'kmsProvider' => ['type' => 'aws', 'accessKeyId' => 'test', 'secretAccessKey' => 'secret'],
534534
'schemaMap' => $schemaMap,
535535
],
536536
],
@@ -559,7 +559,7 @@ public function testAutoEncryptionWithExtraOptions(): void
559559
'default' => [
560560
'autoEncryption' => [
561561
'keyVaultNamespace' => 'db.vault',
562-
'kmsProvider' => ['name' => 'local', 'key' => 'cGFzc3dvcmQ='],
562+
'kmsProvider' => ['type' => 'local', 'key' => 'cGFzc3dvcmQ='],
563563
'extraOptions' => [
564564
'cryptSharedLibPath' => '/another/path.so',
565565
'cryptSharedLibRequired' => false,

0 commit comments

Comments
 (0)