|
|
@@ -18,6 +18,7 @@ use Cake\Database\ExpressionInterface;
|
|
|
use Cake\Database\Expression\IdentifierExpression;
|
|
|
use Cake\Database\Query;
|
|
|
use Cake\Database\StatementInterface;
|
|
|
+use Cake\Database\Statement\StatementDecorator;
|
|
|
use Cake\Database\TypeMap;
|
|
|
use Cake\Datasource\ConnectionManager;
|
|
|
use Cake\TestSuite\TestCase;
|
|
|
@@ -4611,34 +4612,6 @@ class QueryTest extends TestCase
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * Tests that fetch returns an anonymous object when the string 'obj'
|
|
|
- * is passed as an argument
|
|
|
- *
|
|
|
- * @return void
|
|
|
- */
|
|
|
- public function testSelectWithObjFetchType()
|
|
|
- {
|
|
|
- $this->loadFixtures('Comments');
|
|
|
- $query = new Query($this->connection);
|
|
|
- $result = $query
|
|
|
- ->select(['id'])
|
|
|
- ->from('comments')
|
|
|
- ->where(['id' => '1'])
|
|
|
- ->execute();
|
|
|
- $obj = (object)['id' => 1];
|
|
|
- $this->assertEquals($obj, $result->fetch('obj'));
|
|
|
-
|
|
|
- $query = new Query($this->connection);
|
|
|
- $result = $query
|
|
|
- ->select(['id'])
|
|
|
- ->from('comments')
|
|
|
- ->where(['id' => '1'])
|
|
|
- ->execute();
|
|
|
- $rows = $result->fetchAll('obj');
|
|
|
- $this->assertEquals($obj, $rows[0]);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
* Test getValueBinder()
|
|
|
*
|
|
|
* @return void
|
|
|
@@ -4777,4 +4750,123 @@ class QueryTest extends TestCase
|
|
|
$pattern = str_replace('>', '[`"\]]' . $optional, $pattern);
|
|
|
$this->assertRegExp('#' . $pattern . '#', $query);
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that calling fetchAssoc return an associated array.
|
|
|
+ * @return void
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function testFetchAssoc()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Profiles');
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $fields = [
|
|
|
+ 'id' => 'integer',
|
|
|
+ 'user_id' => 'integer',
|
|
|
+ 'is_active' => 'boolean'
|
|
|
+ ];
|
|
|
+ $typeMap = new TypeMap($fields);
|
|
|
+ $results = $query
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'user_id',
|
|
|
+ 'is_active'
|
|
|
+ ])
|
|
|
+ ->from('profiles')
|
|
|
+ ->setSelectTypeMap($typeMap)
|
|
|
+ ->limit(1)
|
|
|
+ ->execute()
|
|
|
+ ->fetchAssoc();
|
|
|
+ $this->assertSame(['id' => 1, 'user_id' => 1, 'is_active' => false], $results);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that calling fetch with with FETCH_TYPE_OBJ return stdClass object.
|
|
|
+ * @return void
|
|
|
+ * @throws \Exception
|
|
|
+ */
|
|
|
+ public function testFetchObjects()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Profiles');
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $results = $query
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'user_id',
|
|
|
+ 'is_active'
|
|
|
+ ])
|
|
|
+ ->from('profiles')
|
|
|
+ ->limit(1)
|
|
|
+ ->execute()
|
|
|
+ ->fetch(StatementDecorator::FETCH_TYPE_OBJ);
|
|
|
+ $this->assertInstanceOf(\stdClass::class, $results);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that fetchColumn() will return the correct value at $position.
|
|
|
+ * @throws \Exception
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFetchColumn()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Profiles');
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $fields = [
|
|
|
+ 'integer',
|
|
|
+ 'integer',
|
|
|
+ 'boolean'
|
|
|
+ ];
|
|
|
+ $typeMap = new TypeMap($fields);
|
|
|
+ $query
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'user_id',
|
|
|
+ 'is_active'
|
|
|
+ ])
|
|
|
+ ->from('profiles')
|
|
|
+ ->setSelectTypeMap($typeMap)
|
|
|
+ ->where(['id' => 2])
|
|
|
+ ->limit(1);
|
|
|
+ $statement = $query->execute();
|
|
|
+ $results = $statement->fetchColumn(0);
|
|
|
+ $this->assertSame(2, $results);
|
|
|
+
|
|
|
+ $statement = $query->execute();
|
|
|
+ $results = $statement->fetchColumn(1);
|
|
|
+ $this->assertSame(2, $results);
|
|
|
+
|
|
|
+ $statement = $query->execute();
|
|
|
+ $results = $statement->fetchColumn(2);
|
|
|
+ $this->assertSame(false, $results);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Test that fetchColumn() will return false if $position is not set.
|
|
|
+ * @throws \Exception
|
|
|
+ * @return void
|
|
|
+ */
|
|
|
+ public function testFetchColumnReturnsFalse()
|
|
|
+ {
|
|
|
+ $this->loadFixtures('Profiles');
|
|
|
+ $query = new Query($this->connection);
|
|
|
+ $fields = [
|
|
|
+ 'integer',
|
|
|
+ 'integer',
|
|
|
+ 'boolean'
|
|
|
+ ];
|
|
|
+ $typeMap = new TypeMap($fields);
|
|
|
+ $query
|
|
|
+ ->select([
|
|
|
+ 'id',
|
|
|
+ 'user_id',
|
|
|
+ 'is_active'
|
|
|
+ ])
|
|
|
+ ->from('profiles')
|
|
|
+ ->setSelectTypeMap($typeMap)
|
|
|
+ ->where(['id' => 2])
|
|
|
+ ->limit(1);
|
|
|
+ $statement = $query->execute();
|
|
|
+ $results = $statement->fetchColumn(3);
|
|
|
+ $this->assertFalse($results);
|
|
|
+ }
|
|
|
}
|