ValueBinderTest.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  4. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  5. *
  6. * Licensed under The MIT License
  7. * For full copyright and license information, please see the LICENSE.txt
  8. * Redistributions of files must retain the above copyright notice.
  9. *
  10. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  11. * @link https://cakephp.org CakePHP(tm) Project
  12. * @since 3.3.12
  13. * @license https://opensource.org/licenses/mit-license.php MIT License
  14. */
  15. namespace Cake\Test\TestCase\Database;
  16. use Cake\Database\ValueBinder;
  17. use Cake\TestSuite\TestCase;
  18. /**
  19. * Tests ValueBinder class
  20. */
  21. class ValueBinderTest extends TestCase
  22. {
  23. /**
  24. * test the bind method
  25. */
  26. public function testBind()
  27. {
  28. $valueBinder = new ValueBinder();
  29. $valueBinder->bind(':c0', 'value0');
  30. $valueBinder->bind(':c1', 1, 'int');
  31. $valueBinder->bind(':c2', 'value2');
  32. $this->assertCount(3, $valueBinder->bindings());
  33. $expected = [
  34. ':c0' => [
  35. 'value' => 'value0',
  36. 'type' => 'string',
  37. 'placeholder' => 'c0'
  38. ],
  39. ':c1' => [
  40. 'value' => 1,
  41. 'type' => 'int',
  42. 'placeholder' => 'c1'
  43. ],
  44. ':c2' => [
  45. 'value' => 'value2',
  46. 'type' => 'string',
  47. 'placeholder' => 'c2'
  48. ]
  49. ];
  50. $bindings = $valueBinder->bindings();
  51. $this->assertEquals($expected, $bindings);
  52. }
  53. /**
  54. * test the placeholder method
  55. */
  56. public function testPlaceholder()
  57. {
  58. $valueBinder = new ValueBinder();
  59. $result = $valueBinder->placeholder('?');
  60. $this->assertEquals('?', $result);
  61. $valueBinder = new ValueBinder();
  62. $result = $valueBinder->placeholder(':param');
  63. $this->assertEquals(':param', $result);
  64. $valueBinder = new ValueBinder();
  65. $result = $valueBinder->placeholder('p');
  66. $this->assertEquals(':p0', $result);
  67. $result = $valueBinder->placeholder('p');
  68. $this->assertEquals(':p1', $result);
  69. $result = $valueBinder->placeholder('c');
  70. $this->assertEquals(':c2', $result);
  71. }
  72. public function testGenerateManyNamed()
  73. {
  74. $valueBinder = new ValueBinder();
  75. $values = [
  76. 'value0',
  77. 'value1'
  78. ];
  79. $expected = [
  80. ':c0',
  81. ':c1'
  82. ];
  83. $placeholders = $valueBinder->generateManyNamed($values);
  84. $this->assertEquals($expected, $placeholders);
  85. }
  86. /**
  87. * test the reset method
  88. */
  89. public function testReset()
  90. {
  91. $valueBinder = new ValueBinder();
  92. $valueBinder->bind(':c0', 'value0');
  93. $valueBinder->bind(':c1', 'value1');
  94. $this->assertCount(2, $valueBinder->bindings());
  95. $valueBinder->reset();
  96. $this->assertCount(0, $valueBinder->bindings());
  97. $placeholder = $valueBinder->placeholder('c');
  98. $this->assertEquals(':c0', $placeholder);
  99. }
  100. /**
  101. * test the resetCount method
  102. */
  103. public function testResetCount()
  104. {
  105. $valueBinder = new ValueBinder();
  106. // Ensure the _bindings array IS NOT affected by resetCount
  107. $valueBinder->bind(':c0', 'value0');
  108. $valueBinder->bind(':c1', 'value1');
  109. $this->assertCount(2, $valueBinder->bindings());
  110. // Ensure the placeholder generation IS affected by resetCount
  111. $valueBinder->placeholder('param');
  112. $valueBinder->placeholder('param');
  113. $result = $valueBinder->placeholder('param');
  114. $this->assertEquals(':param2', $result);
  115. $valueBinder->resetCount();
  116. $placeholder = $valueBinder->placeholder('param');
  117. $this->assertEquals(':param0', $placeholder);
  118. $this->assertCount(2, $valueBinder->bindings());
  119. }
  120. /**
  121. * tests the attachTo method
  122. */
  123. public function testAttachTo()
  124. {
  125. $valueBinder = new ValueBinder();
  126. $statementMock = $this->getMockBuilder('Cake\Database\Statement\StatementDecorator')
  127. ->disableOriginalConstructor()
  128. ->setMethods(['bindValue'])
  129. ->getMock();
  130. $statementMock->expects($this->at(0))->method('bindValue')->with('c0', 'value0', 'string');
  131. $statementMock->expects($this->at(1))->method('bindValue')->with('c1', 'value1', 'string');
  132. $valueBinder->attachTo($statementMock); //empty array shouldn't call statement
  133. $valueBinder->bind(':c0', 'value0', 'string');
  134. $valueBinder->bind(':c1', 'value1', 'string');
  135. $valueBinder->attachTo($statementMock);
  136. }
  137. }