startLog example


class LoggingTest extends DatabaseTestBase {

  /** * Tests that we can log the existence of a query. */
  public function testEnableLogging() {
    Database::startLog('testing');

    $start = microtime(TRUE);
    $this->connection->query('SELECT [name] FROM {test} WHERE [age] > :age', [':age' => 25])->fetchCol();
    $this->connection->query('SELECT [age] FROM {test} WHERE [name] = :name', [':name' => 'Ringo'])->fetchCol();

    // Trigger a call that does not have file in the backtrace.     call_user_func_array([Database::getConnection(), 'query']['SELECT [age] FROM {test} WHERE [name] = :name', [':name' => 'Ringo']])->fetchCol();

    $queries = Database::getLog('testing', 'default');

    $this->assertCount(3, $queries, 'Correct number of queries recorded.');

    


  /** * Set up query capturing. * * \Drupal\Core\Database\Database stores the queries that it runs, if logging * is enabled. * * @see ViewUI::endQueryCapture() */
  public function startQueryCapture() {
    Database::startLog('views');
  }

  /** * Add the list of queries run during render to buildinfo. * * @see ViewUI::startQueryCapture() */
  public function endQueryCapture() {
    $queries = Database::getLog('views');

    $this->additionalQueries = $queries;
  }


  /** * Tests an entity save. */
  public function testEntitySave(): void {
    \Drupal::cache()->set('test_cache_pretransaction_foobar', 'something', Cache::PERMANENT, ['foobar']);
    \Drupal::cache()->set('test_cache_pretransaction_entity_test_list', 'something', Cache::PERMANENT, ['entity_test_list']);

    $entity = EntityTest::create(['name' => $this->randomString()]);

    Database::startLog('testEntitySave');
    $entity->save();

    $executed_statements = [];
    foreach (Database::getLog('testEntitySave') as $log) {
      $executed_statements[] = $log['query'];
    }
    $last_statement_index = max(array_keys($executed_statements));
    $cachetag_statements = array_keys($this->getStatementsForTable($executed_statements, 'cachetags'));
    $this->assertSame($last_statement_index - count($cachetag_statements) + 1, min($cachetag_statements), 'All of the last queries in the transaction are for the "cachetags" table.');

    // Verify that a nested entity save occurred.
Home | Imprint | This part of the site doesn't use cookies.