Skip to content

Commit

Permalink
Starting a trait for constucting common SQL functions
Browse files Browse the repository at this point in the history
  • Loading branch information
lorenzo committed Mar 7, 2013
1 parent dc313fa commit ec73b23
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
Expand Up @@ -48,6 +48,14 @@ public function __construct($name, $params = [], $types = []) {
parent::__construct($params, $types, ',');
}

public function name($name = null) {
if ($name === null) {
return $this->_name;
}
$this->_name = $name;
return $this;
}

/**
* Adds one or more arguments for the function call.
*
Expand Down
@@ -0,0 +1,79 @@
<?php
/**
*
* PHP Version 5.4
*
* CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
* Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The Open Group Test Suite License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
* @package Cake.Test.Case.Model.Datasource.Database
* @since CakePHP(tm) v 3.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace Cake\Test\TestCase\Model\Datasource\Database;

use Cake\Model\Datasource\Database\FunctionsTrait;

/**
* Tests FunctionsTrait class
*
**/
class FunctionsTraitTest extends \Cake\TestSuite\TestCase {

public function setUp() {
parent::setUp();
$this->functions = $this->getObjectForTrait(
'\Cake\Model\Datasource\Database\FunctionsTrait'
);
}

public function testFunc() {
$function = $this->functions->func('MyFunc', ['b' => 'literal']);
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('MyFunc', $function->name());
$this->assertEquals('MyFunc(b)', (string)$function);
}

public function testSum() {
$function = $this->functions->sum('total');
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('SUM(total)', (string)$function);
}

public function testAvg() {
$function = $this->functions->avg('salary');
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('AVG(salary)', (string)$function);
}

public function testMAX() {
$function = $this->functions->max('created');
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('MAX(created)', (string)$function);
}

public function testMin() {
$function = $this->functions->min('created');
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('MIN(created)', (string)$function);
}

public function testCount() {
$function = $this->functions->count('*');
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$this->assertEquals('COUNT(*)', (string)$function);
}

public function concat() {
$function = $this->functions->concat(['title' => 'literal', ' is a string']);
$this->assertInstanceOf('\Cake\Model\Datasource\Database\Expression\FunctionExpression', $function);
$param = $function->bindings()[0]['placeholder'];
$this->assertEquals("CONCAT(title, $param)", (string)$function);
}

}

0 comments on commit ec73b23

Please sign in to comment.