FunctionsBuilderTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. /**
  3. * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
  4. * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
  5. *
  6. * Licensed under The Open Group Test Suite License
  7. * Redistributions of files must retain the above copyright notice.
  8. *
  9. * @copyright Copyright 2005-2013, Cake Software Foundation, Inc. (http://cakefoundation.org)
  10. * @link http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
  11. * @since 3.0.0
  12. * @license http://www.opensource.org/licenses/mit-license.php MIT License
  13. */
  14. namespace Cake\Test\TestCase\Database;
  15. use Cake\Database\FunctionsBuilder;
  16. use Cake\Database\ValueBinder;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests FunctionsBuilder class
  20. *
  21. */
  22. class FunctionsBuilderTest extends TestCase {
  23. /**
  24. * Setups a mock for FunctionsBuilder
  25. *
  26. * @return void
  27. */
  28. public function setUp() {
  29. parent::setUp();
  30. $this->functions = new FunctionsBuilder;
  31. }
  32. /**
  33. * Tests generating a generic function call
  34. *
  35. * @return void
  36. */
  37. public function testArbitrary() {
  38. $function = $this->functions->MyFunc(['b' => 'literal']);
  39. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  40. $this->assertEquals('MyFunc', $function->name());
  41. $this->assertEquals('MyFunc(b)', $function->sql(new ValueBinder));
  42. }
  43. /**
  44. * Tests generating a SUM() function
  45. *
  46. * @return void
  47. */
  48. public function testSum() {
  49. $function = $this->functions->sum('total');
  50. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  51. $this->assertEquals('SUM(total)', $function->sql(new ValueBinder));
  52. }
  53. /**
  54. * Tests generating a AVG() function
  55. *
  56. * @return void
  57. */
  58. public function testAvg() {
  59. $function = $this->functions->avg('salary');
  60. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  61. $this->assertEquals('AVG(salary)', $function->sql(new ValueBinder));
  62. }
  63. /**
  64. * Tests generating a MAX() function
  65. *
  66. * @return void
  67. */
  68. public function testMAX() {
  69. $function = $this->functions->max('created');
  70. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  71. $this->assertEquals('MAX(created)', $function->sql(new ValueBinder));
  72. }
  73. /**
  74. * Tests generating a MIN() function
  75. *
  76. * @return void
  77. */
  78. public function testMin() {
  79. $function = $this->functions->min('created');
  80. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  81. $this->assertEquals('MIN(created)', $function->sql(new ValueBinder));
  82. }
  83. /**
  84. * Tests generating a COUNT() function
  85. *
  86. * @return void
  87. */
  88. public function testCount() {
  89. $function = $this->functions->count('*');
  90. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  91. $this->assertEquals('COUNT(*)', $function->sql(new ValueBinder));
  92. }
  93. /**
  94. * Tests generating a CONCAT() function
  95. *
  96. * @return void
  97. */
  98. public function testConcat() {
  99. $function = $this->functions->concat(['title' => 'literal', ' is a string']);
  100. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  101. $this->assertEquals("CONCAT(title, :c0)", $function->sql(new ValueBinder));
  102. }
  103. /**
  104. * Tests generating a COALESCE() function
  105. *
  106. * @return void
  107. */
  108. public function testCoalesce() {
  109. $function = $this->functions->coalesce(['NULL' => 'literal', '1', '2']);
  110. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  111. $this->assertEquals("COALESCE(NULL, :c0, :c1)", $function->sql(new ValueBinder));
  112. }
  113. /**
  114. * Tests generating a NOW(), CURRENT_TIME() and CURRENT_DATE() function
  115. *
  116. * @return void
  117. */
  118. public function testNow() {
  119. $function = $this->functions->now();
  120. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  121. $this->assertEquals("NOW()", $function->sql(new ValueBinder));
  122. $function = $this->functions->now('date');
  123. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  124. $this->assertEquals("CURRENT_DATE()", $function->sql(new ValueBinder));
  125. $function = $this->functions->now('time');
  126. $this->assertInstanceOf('\Cake\Database\Expression\FunctionExpression', $function);
  127. $this->assertEquals("CURRENT_TIME()", $function->sql(new ValueBinder));
  128. }
  129. }