QueryTest.php

Namespace

Drupal\Tests\Core\Entity\Query\Sql

File

core/tests/Drupal/Tests/Core/Entity/Query/Sql/QueryTest.php

View source
<?php

declare (strict_types=1);
namespace Drupal\Tests\Core\Entity\Query\Sql;

use Drupal\Core\Entity\EntityType;
use Drupal\Core\Entity\Query\QueryException;
use Drupal\Core\Entity\Query\Sql\Query;
use Drupal\Core\Extension\ModuleHandler;
use Drupal\Tests\UnitTestCase;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Group;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Tests Drupal\Core\Entity\Query\Sql\Query.
 */
class QueryTest extends UnitTestCase {
  
  /**
   * The query object.
   *
   * @var \Drupal\Core\Entity\Query\Sql\Query
   */
  protected $query;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $entity_type = new EntityType([
      'id' => 'example_entity_query',
    ]);
    $conjunction = 'AND';
    $connection = $this->getMockBuilder('Drupal\\Core\\Database\\Connection')
      ->disableOriginalConstructor()
      ->getMock();
    $namespaces = [
      'Drupal\\Core\\Entity\\Query\\Sql',
    ];
    $this->query = new Query($entity_type, $conjunction, $connection, $namespaces);
    $container = $this->createMock(ContainerInterface::class);
    $container->expects($this->any())
      ->method('get')
      ->with('module_handler')
      ->willReturn($this->createMock(ModuleHandler::class));
    \Drupal::setContainer($container);
  }
  
  /**
   * Tests entity query for entity type without base table.
   *
   * @legacy-covers ::prepare
   */
  public function testNoBaseTable() : void {
    $this->expectException(QueryException::class);
    $this->expectExceptionMessage('No base table for example_entity_query, invalid query.');
    $this->query
      ->execute();
  }
  
  /**
   * Tests revision entity query for entity type without revision table.
   *
   * @legacy-covers ::prepare
   */
  public function testNoRevisionTable() : void {
    $this->expectException(QueryException::class);
    $this->expectExceptionMessage('No revision table for example_entity_query, invalid query.');
    $this->query
      ->allRevisions()
      ->execute();
  }

}

Classes

Title Deprecated Summary
QueryTest Tests Drupal\Core\Entity\Query\Sql\Query.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.