Merge pull request #17122 from colemanw/buildOptions
[civicrm-core.git] / tests / phpunit / api / v4 / Action / SqlExpressionTest.php
index 3145c9d6565797b0c41cf8ca387cdddd25caf145..f99242e03e470c19a794e18b0852bcf84a21a389 100644 (file)
@@ -31,7 +31,7 @@ class SqlExpressionTest extends UnitTestCase {
   public function testSelectNull() {
     Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
     $result = Contact::get()
-      ->addSelect('NULL AS nothing', 'NULL', 'NULL AS b*d char', 'first_name AS firsty')
+      ->addSelect('NULL AS nothing', 'NULL', 'NULL AS b*d char', 'first_name')
       ->addWhere('first_name', '=', 'bob')
       ->setLimit(1)
       ->execute()
@@ -39,7 +39,7 @@ class SqlExpressionTest extends UnitTestCase {
     $this->assertNull($result['nothing']);
     $this->assertNull($result['NULL']);
     $this->assertNull($result['b_d_char']);
-    $this->assertEquals('bob', $result['firsty']);
+    $this->assertEquals('bob', $result['first_name']);
     $this->assertArrayNotHasKey('b*d char', $result);
   }
 
@@ -60,16 +60,40 @@ class SqlExpressionTest extends UnitTestCase {
   public function testSelectStrings() {
     Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
     $result = Contact::get()
-      ->addSelect('first_name AS bob')
+      ->addSelect('first_name')
       ->addSelect('"hello world" AS hi')
       ->addSelect('"can\'t \"quote\"" AS quot')
       ->addWhere('first_name', '=', 'bob')
       ->setLimit(1)
       ->execute()
       ->first();
-    $this->assertEquals('bob', $result['bob']);
+    $this->assertEquals('bob', $result['first_name']);
     $this->assertEquals('hello world', $result['hi']);
     $this->assertEquals('can\'t "quote"', $result['quot']);
   }
 
+  public function testSelectAlias() {
+    try {
+      Contact::get()
+        ->addSelect('first_name AS bob')
+        ->execute();
+    }
+    catch (\API_Exception $e) {
+      $msg = $e->getMessage();
+    }
+    $this->assertContains('alias', $msg);
+    try {
+      Contact::get()
+        ->addSelect('55 AS sort_name')
+        ->execute();
+    }
+    catch (\API_Exception $e) {
+      $msg = $e->getMessage();
+    }
+    $this->assertContains('existing field name', $msg);
+    Contact::get()
+      ->addSelect('55 AS ok_alias')
+      ->execute();
+  }
+
 }