APIv4 - Add SQL expression handling and aggregate functions
[civicrm-core.git] / tests / phpunit / api / v4 / Action / SqlExpressionTest.php
CommitLineData
3176b04c
CW
1<?php
2
3/*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
11 */
12
13/**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 *
18 */
19
20
21namespace api\v4\Action;
22
23use api\v4\UnitTestCase;
24use Civi\Api4\Contact;
25
26/**
27 * @group headless
28 */
29class SqlExpressionTest extends UnitTestCase {
30
31 public function testSelectNull() {
32 Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
33 $result = Contact::get()
34 ->addSelect('NULL AS nothing', 'NULL', 'NULL AS b*d char', 'first_name AS firsty')
35 ->addWhere('first_name', '=', 'bob')
36 ->setLimit(1)
37 ->execute()
38 ->first();
39 $this->assertNull($result['nothing']);
40 $this->assertNull($result['NULL']);
41 $this->assertNull($result['b_d_char']);
42 $this->assertEquals('bob', $result['firsty']);
43 $this->assertArrayNotHasKey('b*d char', $result);
44 }
45
46 public function testSelectNumbers() {
47 Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
48 $result = Contact::get()
49 ->addSelect('first_name', 123, 45.678, '-55 AS neg')
50 ->addWhere('first_name', '=', 'bob')
51 ->setLimit(1)
52 ->execute()
53 ->first();
54 $this->assertEquals('bob', $result['first_name']);
55 $this->assertEquals('123', $result['123']);
56 $this->assertEquals('-55', $result['neg']);
57 $this->assertEquals('45.678', $result['45_678']);
58 }
59
60 public function testSelectStrings() {
61 Contact::create()->addValue('first_name', 'bob')->setCheckPermissions(FALSE)->execute();
62 $result = Contact::get()
63 ->addSelect('first_name AS bob')
64 ->addSelect('"hello world" AS hi')
65 ->addSelect('"can\'t \"quote\"" AS quot')
66 ->addWhere('first_name', '=', 'bob')
67 ->setLimit(1)
68 ->execute()
69 ->first();
70 $this->assertEquals('bob', $result['bob']);
71 $this->assertEquals('hello world', $result['hi']);
72 $this->assertEquals('can\'t "quote"', $result['quot']);
73 }
74
75}