diff --git a/src/UserData/Infrastructure/GeneralTableDataSelector.php b/src/UserData/Infrastructure/GeneralTableDataSelector.php index 371934f..528c22d 100644 --- a/src/UserData/Infrastructure/GeneralTableDataSelector.php +++ b/src/UserData/Infrastructure/GeneralTableDataSelector.php @@ -14,11 +14,15 @@ class GeneralTableDataSelector implements DataSelectorInterface { + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) Not different behaviour, just protection from explosion + */ public function __construct( private string $collection, private string $selectionTable, private string $filterColumn, private QueryBuilderFactoryInterface $queryBuilderFactory, + private bool $optional = false, ) { } @@ -35,14 +39,23 @@ public function getSelectionTable(): string public function getDataForColumnValue(string $columnValue): array { $queryBuilder = $this->queryBuilderFactory->create(); - $queryBuilder->select('*') - ->from($this->selectionTable) - ->where($this->filterColumn . ' = :filterValue') - ->setParameter('filterValue', $columnValue); - /** @var Result $result */ - $result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */ + try { + $queryBuilder->select('*') + ->from($this->selectionTable) + ->where($this->filterColumn . ' = :filterValue') + ->setParameter('filterValue', $columnValue); - return $result->fetchAllAssociative(); + /** @var Result $result */ + $result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */ + + return $result->fetchAllAssociative(); + } catch (\Exception $e) { + if (!$this->optional) { + throw $e; + } + } + + return []; } } diff --git a/src/UserData/Infrastructure/RelatedTableDataSelector.php b/src/UserData/Infrastructure/RelatedTableDataSelector.php index 06ea121..44391b4 100644 --- a/src/UserData/Infrastructure/RelatedTableDataSelector.php +++ b/src/UserData/Infrastructure/RelatedTableDataSelector.php @@ -14,6 +14,9 @@ class RelatedTableDataSelector implements DataSelectorInterface { + /** + * @SuppressWarnings(PHPMD.BooleanArgumentFlag) Not different behaviour, just protection from explosion + */ public function __construct( private string $collection, private string $primaryTable, @@ -21,6 +24,7 @@ public function __construct( private string $relationCondition, private string $filterColumn, private QueryBuilderFactoryInterface $queryBuilderFactory, + private bool $optional = false, ) { } @@ -37,15 +41,24 @@ public function getSelectionTable(): string public function getDataForColumnValue(string $columnValue): array { $queryBuilder = $this->queryBuilderFactory->create(); - $queryBuilder->select($this->selectionTable . '.*') - ->from($this->primaryTable) - ->innerJoin($this->primaryTable, $this->selectionTable, $this->selectionTable, $this->relationCondition) - ->where($this->filterColumn . ' = :filterValue') - ->setParameter('filterValue', $columnValue); - /** @var Result $result */ - $result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */ + try { + $queryBuilder->select($this->selectionTable . '.*') + ->from($this->primaryTable) + ->innerJoin($this->primaryTable, $this->selectionTable, $this->selectionTable, $this->relationCondition) + ->where($this->filterColumn . ' = :filterValue') + ->setParameter('filterValue', $columnValue); - return $result->fetchAllAssociative(); + /** @var Result $result */ + $result = $queryBuilder->execute(); /** @phpstan-ignore missingType.iterableValue */ + + return $result->fetchAllAssociative(); + } catch (\Exception $e) { + if (!$this->optional) { + throw $e; + } + } + + return []; } } diff --git a/src/UserData/services.yaml b/src/UserData/services.yaml index 1c91870..17da597 100644 --- a/src/UserData/services.yaml +++ b/src/UserData/services.yaml @@ -178,6 +178,7 @@ services: $selectionTable: 'oxobject2role' $relationCondition: 'oxuser.OXID = oxobject2role.OXOBJECTID' $filterColumn: 'oxuser.OXID' + $optional: true tags: - { name: 'oe.gdpr.user_data' } diff --git a/tests/Integration/UserData/Infrastructure/GeneralTableDataSelectorTest.php b/tests/Integration/UserData/Infrastructure/GeneralTableDataSelectorTest.php index ebdee9d..0850130 100644 --- a/tests/Integration/UserData/Infrastructure/GeneralTableDataSelectorTest.php +++ b/tests/Integration/UserData/Infrastructure/GeneralTableDataSelectorTest.php @@ -48,4 +48,30 @@ public function testSelectorSelectsDataFromTableFilteredByColumnValue(): void $this->assertSame($collectionName, $sut->getCollection()); $this->assertSame('oxuser', $sut->getSelectionTable()); } + + public function testSelectorExplodesOnWrongQuery(): void + { + $sut = new GeneralTableDataSelector( + collection: uniqid(), + selectionTable: uniqid(), + filterColumn: uniqid(), + queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class), + ); + + $this->expectException(\Exception::class); + $sut->getDataForColumnValue(self::USER_ID); + } + + public function testOptionalFlagDoesNotExplodeOnQueryError(): void + { + $sut = new GeneralTableDataSelector( + collection: uniqid(), + selectionTable: uniqid(), + filterColumn: uniqid(), + queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class), + optional: true, + ); + + $this->assertSame([], $sut->getDataForColumnValue(self::USER_ID)); + } } diff --git a/tests/Integration/UserData/Infrastructure/RelatedTableDataSelectorTest.php b/tests/Integration/UserData/Infrastructure/RelatedTableDataSelectorTest.php index e2d9131..a2bda02 100644 --- a/tests/Integration/UserData/Infrastructure/RelatedTableDataSelectorTest.php +++ b/tests/Integration/UserData/Infrastructure/RelatedTableDataSelectorTest.php @@ -61,4 +61,34 @@ public function testSelectorSelectsDataFromTableFilteredByColumnValue(): void $this->assertSame($collectionName, $sut->getCollection()); $this->assertSame('oxorderfiles', $sut->getSelectionTable()); } + + public function testSelectorExplodesOnWrongQuery(): void + { + $sut = new RelatedTableDataSelector( + collection: $collectionName = uniqid(), + primaryTable: uniqid(), + selectionTable: uniqid(), + relationCondition: 'oxorderfiles.OXORDERID = oxorder.OXID', + filterColumn: 'oxorder.OXUSERID', + queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class) + ); + + $this->expectException(\Exception::class); + $sut->getDataForColumnValue(self::USER_ID); + } + + public function testOptionalFlagDoesNotExplodeOnQueryError(): void + { + $sut = new RelatedTableDataSelector( + collection: uniqid(), + primaryTable: uniqid(), + selectionTable: uniqid(), + relationCondition: 'oxorderfiles.OXORDERID = oxorder.OXID', + filterColumn: 'oxorder.OXUSERID', + queryBuilderFactory: $this->get(QueryBuilderFactoryInterface::class), + optional: true + ); + + $this->assertSame([], $sut->getDataForColumnValue(self::USER_ID)); + } } diff --git a/tests/Integration/UserData/Infrastructure/SelectorTest.php b/tests/Integration/UserData/Infrastructure/SelectorTest.php new file mode 100644 index 0000000..879d356 --- /dev/null +++ b/tests/Integration/UserData/Infrastructure/SelectorTest.php @@ -0,0 +1,24 @@ +get(CollectionAggregationServiceInterface::class); + $aggregate->collectUserData(uniqid()); + + $this->addToAssertionCount(1); + } +}