Commit cccaf876 authored by François Agneray's avatar François Agneray
Browse files

Add nrows for votable (nb records)

parent accfb59e
Pipeline #5388 passed with stages
in 2 minutes and 17 seconds
......@@ -33,10 +33,11 @@ class VotableResponse implements IResponse
*/
public function getResponse(Response $response, AnisQueryBuilder $anisQueryBuilder): Response
{
$nbRecords = $this->getNbRecords($anisQueryBuilder);
$stmt = $anisQueryBuilder->getDoctrineQueryBuilder()->execute();
$dataset = $anisQueryBuilder->getDatasetSelected();
$attributes = $anisQueryBuilder->getAttributesSelected();
$payload = $this->getVoHeader($dataset, $attributes, 0);
$payload = $this->getVoHeader($dataset, $attributes, $nbRecords);
while ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
$payload .= $this->getVoRow($row, $attributes);
}
......@@ -45,6 +46,18 @@ class VotableResponse implements IResponse
return $response->withHeader('Content-Type', 'text/xml');
}
/**
* @param AnisQueryBuilder $anisQueryBuilder Object used to wrap the Doctrine DBAL Query Builder
*
* @return int
*/
private function getNbRecords(AnisQueryBuilder $anisQueryBuilder): int
{
$doctrineQueryBuilder = $anisQueryBuilder->getDoctrineQueryBuilder();
$sql = 'SELECT count(*) as nb FROM (' . $doctrineQueryBuilder . ') as sub';
return $doctrineQueryBuilder->getConnection()->fetchOne($sql);
}
/**
* @param Dataset $dataset The query dataset selected
* @param Attribute[] $attributes The query attributes selected
......
......@@ -15,6 +15,7 @@ namespace App\Tests\Search\Response;
use PHPUnit\Framework\TestCase;
use Nyholm\Psr7\Response;
use Doctrine\DBAL\Driver\Statement;
use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Query\QueryBuilder as DoctrineQueryBuilder;
use App\Search\Response\VotableResponse;
use App\Search\Query\AnisQueryBuilder;
......@@ -44,8 +45,12 @@ final class VotableResponseTest extends TestCase
$dec = $this->createMock(Attribute::class);
$dec->method('getLabel')->willReturn('dec');
$connection = $this->createMock(Connection::class);
$connection->method('fetchOne')->willReturn(10);
$doctrineQueryBuilder = $this->createMock(DoctrineQueryBuilder::class);
$doctrineQueryBuilder->method('execute')->willReturn($stmt);
$doctrineQueryBuilder->method('getConnection')->willReturn($connection);
$anisQueryBuilder = $this->createMock(AnisQueryBuilder::class);
$anisQueryBuilder->method('getDoctrineQueryBuilder')->willReturn($doctrineQueryBuilder);
$anisQueryBuilder->method('getAttributesSelected')->willReturn(array($id, $ra, $dec));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment