| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The Open Group Test Suite License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 3.0.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Database;
- use Cake\Database\Expression\AggregateExpression;
- use Cake\Database\Expression\FunctionExpression;
- use Cake\Database\Expression\IdentifierExpression;
- use Cake\Database\FunctionsBuilder;
- use Cake\Database\ValueBinder;
- use Cake\TestSuite\TestCase;
- /**
- * Tests FunctionsBuilder class
- */
- class FunctionsBuilderTest extends TestCase
- {
- /**
- * @var \Cake\Database\FunctionsBuilder
- */
- protected $functions;
- /**
- * Setups a mock for FunctionsBuilder
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->functions = new FunctionsBuilder();
- }
- /**
- * Tests generating a generic function call
- */
- public function testArbitrary(): void
- {
- $function = $this->functions->MyFunc(['b' => 'literal']);
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('MyFunc', $function->getName());
- $this->assertSame('MyFunc(b)', $function->sql(new ValueBinder()));
- $function = $this->functions->MyFunc(['b'], ['string'], 'integer');
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a generic aggregate call
- */
- public function testArbitraryAggregate(): void
- {
- $function = $this->functions->aggregate('MyFunc', ['b' => 'literal']);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('MyFunc', $function->getName());
- $this->assertSame('MyFunc(b)', $function->sql(new ValueBinder()));
- $function = $this->functions->aggregate('MyFunc', ['b'], ['string'], 'integer');
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a SUM() function
- */
- public function testSum(): void
- {
- $function = $this->functions->sum('total');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- $function = $this->functions->sum('total', ['integer']);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('SUM(total)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a AVG() function
- */
- public function testAvg(): void
- {
- $function = $this->functions->avg('salary');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('AVG(salary)', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- }
- /**
- * Tests generating a MAX() function
- */
- public function testMax(): void
- {
- $function = $this->functions->max('total');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('MAX(total)', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- $function = $this->functions->max('created', ['datetime']);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('MAX(created)', $function->sql(new ValueBinder()));
- $this->assertSame('datetime', $function->getReturnType());
- }
- /**
- * Tests generating a MIN() function
- */
- public function testMin(): void
- {
- $function = $this->functions->min('created', ['date']);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('MIN(created)', $function->sql(new ValueBinder()));
- $this->assertSame('date', $function->getReturnType());
- }
- /**
- * Tests generating a COUNT() function
- */
- public function testCount(): void
- {
- $function = $this->functions->count('*');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('COUNT(*)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a CONCAT() function
- */
- public function testConcat(): void
- {
- $function = $this->functions->concat(['title' => 'literal', ' is a string']);
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('CONCAT(title, :param0)', $function->sql(new ValueBinder()));
- $this->assertSame('string', $function->getReturnType());
- }
- /**
- * Tests generating a COALESCE() function
- */
- public function testCoalesce(): void
- {
- $function = $this->functions->coalesce(['NULL' => 'literal', '1', 'a'], ['a' => 'date']);
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('COALESCE(NULL, :param0, :param1)', $function->sql(new ValueBinder()));
- $this->assertSame('date', $function->getReturnType());
- }
- /**
- * Tests generating a CAST() function
- */
- public function testCast(): void
- {
- $function = $this->functions->cast('field', 'varchar');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('CAST(field AS varchar)', $function->sql(new ValueBinder()));
- $this->assertSame('string', $function->getReturnType());
- $function = $this->functions->cast($this->functions->now(), 'varchar');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('CAST(NOW() AS varchar)', $function->sql(new ValueBinder()));
- $this->assertSame('string', $function->getReturnType());
- }
- /**
- * Tests generating a NOW(), CURRENT_TIME() and CURRENT_DATE() function
- */
- public function testNow(): void
- {
- $function = $this->functions->now();
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('NOW()', $function->sql(new ValueBinder()));
- $this->assertSame('datetime', $function->getReturnType());
- $function = $this->functions->now('date');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('CURRENT_DATE()', $function->sql(new ValueBinder()));
- $this->assertSame('date', $function->getReturnType());
- $function = $this->functions->now('time');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('CURRENT_TIME()', $function->sql(new ValueBinder()));
- $this->assertSame('time', $function->getReturnType());
- }
- /**
- * Tests generating a EXTRACT() function
- */
- public function testExtract(): void
- {
- $function = $this->functions->extract('day', 'created');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('EXTRACT(day FROM created)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- $function = $this->functions->datePart('year', 'modified');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('EXTRACT(year FROM modified)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a DATE_ADD() function
- */
- public function testDateAdd(): void
- {
- $function = $this->functions->dateAdd('created', -3, 'day');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder()));
- $this->assertSame('datetime', $function->getReturnType());
- $function = $this->functions->dateAdd(new IdentifierExpression('created'), -3, 'day');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('DATE_ADD(created, INTERVAL -3 day)', $function->sql(new ValueBinder()));
- }
- /**
- * Tests generating a DAYOFWEEK() function
- */
- public function testDayOfWeek(): void
- {
- $function = $this->functions->dayOfWeek('created');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- $function = $this->functions->weekday('created');
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('DAYOFWEEK(created)', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a RAND() function
- */
- public function testRand(): void
- {
- $function = $this->functions->rand();
- $this->assertInstanceOf(FunctionExpression::class, $function);
- $this->assertSame('RAND()', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- }
- /**
- * Tests generating a ROW_NUMBER() window function
- */
- public function testRowNumber(): void
- {
- $function = $this->functions->rowNumber();
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('ROW_NUMBER() OVER ()', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a LAG() window function
- */
- public function testLag(): void
- {
- $function = $this->functions->lag('field', 1);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('LAG(field, 1) OVER ()', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- $function = $this->functions->lag('field', 1, 10, 'integer');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('LAG(field, 1, :param0) OVER ()', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- /**
- * Tests generating a LAG() window function
- */
- public function testLead(): void
- {
- $function = $this->functions->lead('field', 1);
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('LEAD(field, 1) OVER ()', $function->sql(new ValueBinder()));
- $this->assertSame('float', $function->getReturnType());
- $function = $this->functions->lead('field', 1, 10, 'integer');
- $this->assertInstanceOf(AggregateExpression::class, $function);
- $this->assertSame('LEAD(field, 1, :param0) OVER ()', $function->sql(new ValueBinder()));
- $this->assertSame('integer', $function->getReturnType());
- }
- }
|