Merge pull request #13653 from MegaphoneJon/reporting-8
[civicrm-core.git] / tests / phpunit / CRM / Utils / SQL / SelectTest.php
index 3c16690ee03b5b791cc6f3603cb3b3a72e63e36b..ba4d8d29dd2453f38d2669afe9f9df755b280649 100644 (file)
@@ -2,6 +2,7 @@
 
 /**
  * Class CRM_Utils_SQL_SelectTest
+ * @group headless
  */
 class CRM_Utils_SQL_SelectTest extends CiviUnitTestCase {
   public function testGetDefault() {
@@ -9,6 +10,46 @@ class CRM_Utils_SQL_SelectTest extends CiviUnitTestCase {
     $this->assertLike('SELECT * FROM foo bar', $select->toSQL());
   }
 
+  public function testExecute_OK_fetch() {
+    $select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
+    $this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());
+
+    $select = CRM_Utils_SQL_Select::from('civicrm_contact')
+      ->select('count(*) as cnt');
+    $rows = 0;
+    $dao = $select->execute();
+    while ($dao->fetch()) {
+      $rows++;
+      $this->assertTrue(is_numeric($dao->cnt), "Expect query to execute");
+    }
+    $this->assertEquals(1, $rows);
+  }
+
+  public function testExecute_OK_fetchValue() {
+    $select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
+    $this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());
+    $this->assertTrue(is_numeric($select->execute()->fetchValue()));
+  }
+
+  public function testExecute_OK_fetchAll() {
+    $select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('count(*) as cnt');
+    $this->assertLike('SELECT count(*) as cnt FROM civicrm_contact', $select->toSQL());
+    $records = $select->execute()->fetchAll();
+    $this->assertTrue(is_numeric($records[0]['cnt']));
+  }
+
+  public function testExecute_Error() {
+    $select = CRM_Utils_SQL_Select::from('civicrm_contact')->select('snarb;barg');
+
+    try {
+      $select->execute();
+      $this->fail('Expected an exception');
+    }
+    catch (PEAR_Exception $e) {
+      $this->assertTrue(TRUE, "Received expected exception");
+    }
+  }
+
   public function testGetFields() {
     $select = CRM_Utils_SQL_Select::from('foo')
       ->select('bar')