3 namespace Drupal\KernelTests\Core\Database;
5 use Drupal\Core\Database\RowCountException;
6 use Drupal\Core\Database\StatementInterface;
7 use Drupal\Tests\system\Functional\Database\FakeRecord;
10 * Tests the Database system's various fetch capabilities.
12 * We get timeout errors if we try to run too many tests at once.
16 class FetchTest extends DatabaseTestBase {
19 * Confirms that we can fetch a record properly in default object mode.
21 public function testQueryFetchDefault() {
23 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25]);
24 $this->assertTrue($result instanceof StatementInterface, 'Result set is a Drupal statement object.');
25 foreach ($result as $record) {
27 $this->assertTrue(is_object($record), 'Record is an object.');
28 $this->assertIdentical($record->name, 'John', '25 year old is John.');
31 $this->assertIdentical(count($records), 1, 'There is only one record.');
35 * Confirms that we can fetch a record to an object explicitly.
37 public function testQueryFetchObject() {
39 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_OBJ]);
40 foreach ($result as $record) {
42 $this->assertTrue(is_object($record), 'Record is an object.');
43 $this->assertIdentical($record->name, 'John', '25 year old is John.');
46 $this->assertIdentical(count($records), 1, 'There is only one record.');
50 * Confirms that we can fetch a record to an associative array explicitly.
52 public function testQueryFetchArray() {
54 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_ASSOC]);
55 foreach ($result as $record) {
57 if ($this->assertTrue(is_array($record), 'Record is an array.')) {
58 $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
62 $this->assertIdentical(count($records), 1, 'There is only one record.');
66 * Confirms that we can fetch a record into a new instance of a custom class.
68 * @see \Drupal\system\Tests\Database\FakeRecord
70 public function testQueryFetchClass() {
72 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => FakeRecord::class]);
73 foreach ($result as $record) {
75 if ($this->assertTrue($record instanceof FakeRecord, 'Record is an object of class FakeRecord.')) {
76 $this->assertIdentical($record->name, 'John', '25 year old is John.');
80 $this->assertIdentical(count($records), 1, 'There is only one record.');
84 * Confirms that we can fetch a record into an indexed array explicitly.
86 public function testQueryFetchNum() {
88 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_NUM]);
89 foreach ($result as $record) {
91 if ($this->assertTrue(is_array($record), 'Record is an array.')) {
92 $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
96 $this->assertIdentical(count($records), 1, 'There is only one record');
100 * Confirms that we can fetch a record into a doubly-keyed array explicitly.
102 public function testQueryFetchBoth() {
104 $result = db_query('SELECT name FROM {test} WHERE age = :age', [':age' => 25], ['fetch' => \PDO::FETCH_BOTH]);
105 foreach ($result as $record) {
106 $records[] = $record;
107 if ($this->assertTrue(is_array($record), 'Record is an array.')) {
108 $this->assertIdentical($record[0], 'John', 'Record can be accessed numerically.');
109 $this->assertIdentical($record['name'], 'John', 'Record can be accessed associatively.');
113 $this->assertIdentical(count($records), 1, 'There is only one record.');
117 * Confirms that we can fetch all records into an array explicitly.
119 public function testQueryFetchAllColumn() {
120 $query = db_select('test');
121 $query->addField('test', 'name');
122 $query->orderBy('name');
123 $query_result = $query->execute()->fetchAll(\PDO::FETCH_COLUMN);
125 $expected_result = ['George', 'John', 'Paul', 'Ringo'];
126 $this->assertEqual($query_result, $expected_result, 'Returned the correct result.');
130 * Confirms that we can fetch an entire column of a result set at once.
132 public function testQueryFetchCol() {
133 $result = db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25]);
134 $column = $result->fetchCol();
135 $this->assertIdentical(count($column), 3, 'fetchCol() returns the right number of records.');
137 $result = db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25]);
139 foreach ($result as $record) {
140 $this->assertIdentical($record->name, $column[$i++], 'Column matches direct access.');
145 * Tests that rowCount() throws exception on SELECT query.
147 public function testRowCount() {
148 $result = db_query('SELECT name FROM {test}');
153 catch (RowCountException $e) {
156 $this->assertTrue($exception, 'Exception was thrown');