You are here

public function ReportQueryBuilderTest::testAlterQuery in Commerce Reporting 8

Tests ::alterQuery.

File

tests/src/Kernel/ReportQueryBuilderTest.php, line 67

Class

ReportQueryBuilderTest
Tests ReportQueryBuilder.

Namespace

Drupal\Tests\commerce_reports\Kernel

Code

public function testAlterQuery() {

  /** @var \Drupal\commerce_reports\Plugin\Commerce\ReportType\ReportTypeInterface $report_type_plugin */
  $report_type_plugin = $this->reportTypeManager
    ->createInstance('order_report');

  /** @var \Drupal\Core\Entity\Query\Sql\QueryAggregate $query */
  $query = $this->reportQueryBuilder
    ->getQuery($report_type_plugin);

  /** @var \Drupal\Core\Database\Query\SelectInterface $sqlQuery */
  $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;
  }

  /** @var \Drupal\Core\Entity\Query\Sql\QueryAggregate $query */
  $query = $this->reportQueryBuilder
    ->getQuery($report_type_plugin, 'j F Y');

  /** @var \Drupal\Core\Database\Query\SelectInterface $sqlQuery */
  $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;
  }
}