enableEvents example

parent::setUp();

    $this->connection = new StubConnection($this->createMock(StubPDO::class)[]);
  }

  /** * @covers ::isEventEnabled * @covers ::enableEvents * @covers ::disableEvents */
  public function testEventEnablingAndDisabling(): void {
    $this->connection->enableEvents([
      StatementExecutionStartEvent::class,
      StatementExecutionEndEvent::class,
    ]);
    $this->assertTrue($this->connection->isEventEnabled(StatementExecutionStartEvent::class));
    $this->assertTrue($this->connection->isEventEnabled(StatementExecutionEndEvent::class));
    $this->connection->disableEvents([
      StatementExecutionEndEvent::class,
    ]);
    $this->assertTrue($this->connection->isEventEnabled(StatementExecutionStartEvent::class));
    $this->assertFalse($this->connection->isEventEnabled(StatementExecutionEndEvent::class));
  }

  

  final public static function startLog($logging_key$key = 'default') {
    if (empty(self::$logs[$key])) {
      self::$logs[$key] = new Log($key);

      // Every target already active for this connection key needs to have the       // logging object associated with it.       if (!empty(self::$connections[$key])) {
        foreach (self::$connections[$key] as $connection) {
          $connection->enableEvents([
            StatementExecutionStartEvent::class,
            StatementExecutionEndEvent::class,
          ]);
          $connection->setLogger(self::$logs[$key]);
        }
      }
    }

    self::$logs[$key]->start($logging_key);
    return self::$logs[$key];
  }

  
// Execute a query, still no events captured.     $this->connection->query('SELECT * FROM {test}');
    $this->assertSame(0, $subscriber->countStatementStarts);
    $this->assertSame(0, $subscriber->countStatementEnds);
    $this->assertEmpty($subscriber->statementIdsInExecution);
    $this->assertFalse($this->connection->isEventEnabled(StatementExecutionStartEvent::class));
    $this->assertFalse($this->connection->isEventEnabled(StatementExecutionEndEvent::class));

    // Enable the statement execution start event and execute a query, start     // event captured but no end one.     $this->connection->enableEvents([StatementExecutionStartEvent::class]);
    $this->connection->query('SELECT * FROM {test}');
    $this->assertSame(1, $subscriber->countStatementStarts);
    $this->assertSame(0, $subscriber->countStatementEnds);
    $this->assertCount(1, $subscriber->statementIdsInExecution);
    $this->assertTrue($this->connection->isEventEnabled(StatementExecutionStartEvent::class));
    $this->assertFalse($this->connection->isEventEnabled(StatementExecutionEndEvent::class));
    // Reset the statements in execution map to cleanup for following tests.     $subscriber->statementIdsInExecution = [];

    // Enable the statement execution end event and execute a query, both     // events captured.
Home | Imprint | This part of the site doesn't use cookies.