ReportQueryBuilderTest.php in Commerce Reporting 8
File
tests/src/Kernel/ReportQueryBuilderTest.php
View source
<?php
namespace Drupal\Tests\commerce_reports\Kernel;
use Drupal\Core\Database\Query\SelectInterface;
use Drupal\Core\Entity\Query\QueryAggregateInterface;
use Drupal\Core\Entity\Query\Sql\QueryAggregate;
use Drupal\Tests\commerce\Kernel\CommerceKernelTestBase;
class ReportQueryBuilderTest extends CommerceKernelTestBase {
public static $modules = [
'entity_reference_revisions',
'profile',
'state_machine',
'commerce_order',
'commerce_reports',
];
protected $reportQueryBuilder;
protected $reportTypeManager;
protected function setUp() {
parent::setUp();
$this->reportQueryBuilder = $this->container
->get('commerce_reports.query_builder');
$this->reportTypeManager = $this->container
->get('plugin.manager.commerce_report_type');
}
public function testGetQuery() {
$report_type_plugin = $this->reportTypeManager
->createInstance('order_report');
$query = $this->reportQueryBuilder
->getQuery($report_type_plugin);
$this
->assertInstanceOf(QueryAggregateInterface::class, $query);
$this
->assertTrue($query
->hasTag('commerce_reports'));
$this
->assertEquals('F Y', $query
->getMetaData('report_date_format'));
$query = $this->reportQueryBuilder
->getQuery($report_type_plugin, 'j F Y');
$this
->assertEquals('j F Y', $query
->getMetaData('report_date_format'));
}
public function testAlterQuery() {
$report_type_plugin = $this->reportTypeManager
->createInstance('order_report');
$query = $this->reportQueryBuilder
->getQuery($report_type_plugin);
$sqlQuery = $this
->getSqlQueryFromAggregateQuery($query);
$this->reportQueryBuilder
->alterQuery($sqlQuery);
$expressions = $sqlQuery
->getExpressions();
$this
->assertTrue(isset($expressions['formatted_date']));
$formatted_date_expression = $expressions['formatted_date']['expression'];
$db_type = $this->container
->get('database')
->databaseType();
switch ($db_type) {
case 'mysql':
$this
->assertEquals("DATE_FORMAT(FROM_UNIXTIME(base_table.created), '%M %Y')", $formatted_date_expression);
break;
case 'sqlite':
$this
->assertEquals("strftime('%m %Y', base_table.created, 'unixepoch')", $formatted_date_expression);
break;
case 'pgsql':
break;
}
$query = $this->reportQueryBuilder
->getQuery($report_type_plugin, 'j F Y');
$sqlQuery = $this
->getSqlQueryFromAggregateQuery($query);
$this->reportQueryBuilder
->alterQuery($sqlQuery);
$expressions = $sqlQuery
->getExpressions();
$this
->assertTrue(isset($expressions['formatted_date']));
$formatted_date_expression = $expressions['formatted_date']['expression'];
$db_type = $this->container
->get('database')
->databaseType();
switch ($db_type) {
case 'mysql':
$this
->assertEquals("DATE_FORMAT(FROM_UNIXTIME(base_table.created), '%e %M %Y')", $formatted_date_expression);
break;
case 'sqlite':
$this
->assertEquals("strftime('%d %m %Y', base_table.created, 'unixepoch')", $formatted_date_expression);
break;
case 'pgsql':
break;
}
}
protected function getSqlQueryFromAggregateQuery(QueryAggregate $query) {
$query
->prepare();
$reflection = new \ReflectionObject($query);
$property = $reflection
->getProperty('sqlQuery');
$property
->setAccessible(TRUE);
$sqlQuery = $property
->getValue($query);
$this
->assertInstanceOf(SelectInterface::class, $sqlQuery);
return $sqlQuery;
}
}