Skip to content

Commit

Permalink
[BUGFIX] Correct doctrine proxy when used with non-SELECT statements (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
NamelessCoder authored Oct 25, 2024
1 parent baf6013 commit e64c897
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 12 deletions.
4 changes: 2 additions & 2 deletions Classes/Service/RecordService.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ public function update(string $table, array $record)
foreach ($record as $name => $value) {
$builder->set($name, $value);
}
return DoctrineQueryProxy::executeQueryOnQueryBuilder($builder);
return DoctrineQueryProxy::executeStatementOnQueryBuilder($builder);
}

/**
Expand All @@ -98,7 +98,7 @@ public function delete(string $table, $recordOrUid): bool
$queryBuilder = $this->getQueryBuilder($table);
$queryBuilder->delete($table)
->where($queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($clauseUid)));
return (bool) DoctrineQueryProxy::executeQueryOnQueryBuilder($queryBuilder);
return (bool) DoctrineQueryProxy::executeStatementOnQueryBuilder($queryBuilder);
}

public function preparedGet(string $table, string $fields, string $condition, array $values = []): array
Expand Down
17 changes: 11 additions & 6 deletions Classes/Utility/DoctrineQueryProxy.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@

class DoctrineQueryProxy
{
/**
* Returns \Doctrine\DBAL\Result on v11+, \Doctrine\DBAL\Driver\ResultStatement on v10
*
* @return Result
*/
public static function executeQueryOnQueryBuilder(QueryBuilder $queryBuilder)
public static function executeStatementOnQueryBuilder(QueryBuilder $queryBuilder): int
{
if (method_exists($queryBuilder, 'executeStatement')) {
return $queryBuilder->executeStatement();
}
/** @var int $result */
$result = $queryBuilder->execute();
return $result;
}

public static function executeQueryOnQueryBuilder(QueryBuilder $queryBuilder): Result
{
if (method_exists($queryBuilder, 'executeQuery')) {
return $queryBuilder->executeQuery();
Expand Down
11 changes: 7 additions & 4 deletions Tests/Unit/Service/RecordServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,12 @@ public function updateMethodCallsExpectedDatabaseMethod()
$fields = ['foo' => 'bar', 'uid' => $uid];
$mock = $this->getMockServiceInstance();

$this->createAndRegisterMockForQueryBuilder();
$this->createAndRegisterMockForQueryBuilder([['uid' => 123]]);

self::assertFalse(empty($mock->update($table, $fields)));
self::assertSame(
1,
$mock->update($table, $fields)
);
}

/**
Expand All @@ -119,7 +122,7 @@ public function deleteMethodCallsExpectedDatabaseMethodWithUid()
$uid = 123;
$mock = $this->getMockServiceInstance();

$this->createAndRegisterMockForQueryBuilder();
$this->createAndRegisterMockForQueryBuilder([['uid' => 123]]);

$this->assertTrue(
$mock->delete($table, $uid)
Expand All @@ -135,7 +138,7 @@ public function deleteMethodCallsExpectedDatabaseMethodWithRecord()
$record = ['uid' => 123];
$mock = $this->getMockServiceInstance();

$this->createAndRegisterMockForQueryBuilder();
$this->createAndRegisterMockForQueryBuilder([['uid' => 123]]);

$this->assertTrue(
$mock->delete($table, $record)
Expand Down

0 comments on commit e64c897

Please sign in to comment.