Update ProductCommentRepository.php#227
Conversation
Fixes PrestaShop#226 issue $productIds parameter array causes an error because it is not sorted.
|
Hello @metineren! This is your first pull request on productcomments repository of the PrestaShop project. Thank you, and welcome to this Open Source community! |
QA — fix confirmed ✅Verified the root cause and the fix at the SQL level (PHP 8.1). The comma between products is emitted by
The Suggested regression testThe bug is purely in the SQL-string construction, so it can be pinned with a fast unit test using a captured connection (no DB). It fails on the base (missing comma) and passes with this PR: <?php
namespace PrestaShop\Module\ProductComment\Tests\Unit\Repository;
use PHPUnit\Framework\TestCase;
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
use ReflectionClass;
class ProductCommentRepositoryTest extends TestCase
{
public function testGetAverageGradesSeparatesProductsWhenKeysAreNotSequential(): void
{
$sql = $this->captureGeneratedSql([5 => 223, 9 => 224]);
// Without the fix the comma is dropped -> `AS "223" SUM(...)` -> SQL syntax error.
$this->assertStringContainsString('AS "223", SUM', $sql);
}
private function captureGeneratedSql(array $productIds): string
{
$connection = new class {
public string $sql = '';
public function executeQuery(string $sql)
{
$this->sql = $sql;
return new class {
public function fetch()
{
return [];
}
};
}
};
$reflection = new ReflectionClass(ProductCommentRepository::class);
/** @var ProductCommentRepository $repository */
$repository = $reflection->newInstanceWithoutConstructor();
foreach (['connection' => $connection, 'databasePrefix' => 'ps_'] as $property => $value) {
$prop = $reflection->getProperty($property);
$prop->setAccessible(true);
$prop->setValue($repository, $value);
}
$repository->getAverageGrades($productIds, false);
return $connection->sql;
}
}(The same applies to |
|
@boo-code Audrius thank you for your PR & confirmation |
|
@jolelievre Hi Jonathan, can you complete the merge request? |
Fixes #226 issue
$productIds parameter array causes an error because it is not sorted.