Verified Commit 1057a397 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #3350375 by mondrake, alexpott, smustgrave, larowlan, catch:...

Issue #3350375 by mondrake, alexpott, smustgrave, larowlan, catch: [regression] fetchAllAssoc() and fetchAllKeyed() behave inconsistently
parent b479b25a
Loading
Loading
Loading
Loading
+4 −4
Original line number Diff line number Diff line
@@ -157,10 +157,10 @@ public function fetchAllAssoc($key, $fetch = NULL) {
      return [];
    }

    // Once the foreach loop is completed, the resultset is marked so not to
    // Once the while loop is completed, the resultset is marked so not to
    // allow more fetching.
    $return = [];
    foreach ($this as $record) {
    while ($record = $this->fetch()) {
      $recordKey = is_object($record) ? $record->$key : $record[$key];
      $return[$recordKey] = $record;
    }
@@ -179,10 +179,10 @@ public function fetchAllKeyed($key_index = 0, $value_index = 1) {
      return [];
    }

    // Once the foreach loop is completed, the resultset is marked so not to
    // Once the while loop is completed, the resultset is marked so not to
    // allow more fetching.
    $return = [];
    foreach ($this as $record) {
    while ($record = $this->fetch()) {
      $return[$record[$key_index]] = $record[$value_index];
    }
    return $return;
+93 −0
Original line number Diff line number Diff line
@@ -64,6 +64,99 @@ public function testIteratedStatementFetch(): void {
    $this->assertFalse($statement->fetchField());
  }

  /**
   * Tests statement fetchAll after a partial traversal.
   */
  public function testPartiallyIteratedStatementFetchAll(): void {
    $statement = $this->connection->query('SELECT * FROM {test}');

    for ($i = 0; $i < 2; $i++) {
      $statement->fetch();
    }

    $expected = [
      0 => (object) [
        "id" => "3",
        "name" => "Ringo",
        "age" => "28",
        "job" => "Drummer",
      ],
      1 => (object) [
        "id" => "4",
        "name" => "Paul",
        "age" => "26",
        "job" => "Songwriter",
      ],
    ];

    $this->assertEquals($expected, $statement->fetchAll());
    $this->assertSame([], $statement->fetchAll());
  }

  /**
   * Tests statement fetchAllKeyed after a partial traversal.
   */
  public function testPartiallyIteratedStatementFetchAllKeyed(): void {
    $statement = $this->connection->query('SELECT * FROM {test}');

    for ($i = 0; $i < 2; $i++) {
      $statement->fetch();
    }

    $expected = [
      "3" => "Ringo",
      "4" => "Paul",
    ];

    $this->assertSame($expected, $statement->fetchAllKeyed());
    $this->assertSame([], $statement->fetchAllKeyed());
  }

  /**
   * Tests statement fetchAllAssoc after a partial traversal.
   */
  public function testPartiallyIteratedStatementFetchAllAssoc(): void {
    $statement = $this->connection->query('SELECT * FROM {test}');

    for ($i = 0; $i < 2; $i++) {
      $statement->fetch();
    }

    $expected = [
      "28" => (object) [
        "id" => "3",
        "name" => "Ringo",
        "age" => "28",
        "job" => "Drummer",
      ],
      "26" => (object) [
        "id" => "4",
        "name" => "Paul",
        "age" => "26",
        "job" => "Songwriter",
      ],
    ];

    $this->assertEquals($expected, $statement->fetchAllAssoc('age'));
    $this->assertSame([], $statement->fetchAllAssoc('age'));
  }

  /**
   * Tests statement fetchCol after a partial traversal.
   */
  public function testPartiallyIteratedStatementFetchCol(): void {
    $statement = $this->connection->query('SELECT * FROM {test}');

    for ($i = 0; $i < 2; $i++) {
      $statement->fetch();
    }

    $expected = ["3", "4"];

    $this->assertSame($expected, $statement->fetchCol());
    $this->assertSame([], $statement->fetchCol());
  }

  /**
   * Tests statement rewinding.
   */