ValueBinderTest.php 4.7 KB

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