Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Starting a trait for constucting common SQL functions
- Loading branch information
Showing
2 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
lib/Cake/Test/TestCase/Model/Datasource/Database/FunctionsTraitTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |