ValueBinderTest.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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\StatementInterface;
  18. use Cake\Database\ValueBinder;
  19. use Cake\TestSuite\TestCase;
  20. /**
  21. * Tests ValueBinder class
  22. */
  23. class ValueBinderTest extends TestCase
  24. {
  25. /**
  26. * test the bind method
  27. */
  28. public function testBind(): void
  29. {
  30. $valueBinder = new ValueBinder();
  31. $valueBinder->bind(':c0', 'value0');
  32. $valueBinder->bind(':c1', 1, 'int');
  33. $valueBinder->bind(':c2', 'value2');
  34. $this->assertCount(3, $valueBinder->bindings());
  35. $expected = [
  36. ':c0' => [
  37. 'value' => 'value0',
  38. 'type' => null,
  39. 'placeholder' => 'c0',
  40. ],
  41. ':c1' => [
  42. 'value' => 1,
  43. 'type' => 'int',
  44. 'placeholder' => 'c1',
  45. ],
  46. ':c2' => [
  47. 'value' => 'value2',
  48. 'type' => null,
  49. 'placeholder' => 'c2',
  50. ],
  51. ];
  52. $bindings = $valueBinder->bindings();
  53. $this->assertEquals($expected, $bindings);
  54. }
  55. /**
  56. * test the placeholder method
  57. */
  58. public function testPlaceholder(): void
  59. {
  60. $valueBinder = new ValueBinder();
  61. $result = $valueBinder->placeholder('?');
  62. $this->assertSame('?', $result);
  63. $valueBinder = new ValueBinder();
  64. $result = $valueBinder->placeholder(':param');
  65. $this->assertSame(':param', $result);
  66. $valueBinder = new ValueBinder();
  67. $result = $valueBinder->placeholder('p');
  68. $this->assertSame(':p0', $result);
  69. $result = $valueBinder->placeholder('p');
  70. $this->assertSame(':p1', $result);
  71. $result = $valueBinder->placeholder('c');
  72. $this->assertSame(':c2', $result);
  73. }
  74. public function testGenerateManyNamed(): void
  75. {
  76. $valueBinder = new ValueBinder();
  77. $values = [
  78. 'value0',
  79. 'value1',
  80. ];
  81. $expected = [
  82. ':c0',
  83. ':c1',
  84. ];
  85. $placeholders = $valueBinder->generateManyNamed($values);
  86. $this->assertEquals($expected, $placeholders);
  87. }
  88. /**
  89. * test the reset method
  90. */
  91. public function testReset(): void
  92. {
  93. $valueBinder = new ValueBinder();
  94. $valueBinder->bind(':c0', 'value0');
  95. $valueBinder->bind(':c1', 'value1');
  96. $this->assertCount(2, $valueBinder->bindings());
  97. $valueBinder->reset();
  98. $this->assertCount(0, $valueBinder->bindings());
  99. $placeholder = $valueBinder->placeholder('c');
  100. $this->assertSame(':c0', $placeholder);
  101. }
  102. /**
  103. * test the resetCount method
  104. */
  105. public function testResetCount(): void
  106. {
  107. $valueBinder = new ValueBinder();
  108. // Ensure the _bindings array IS NOT affected by resetCount
  109. $valueBinder->bind(':c0', 'value0');
  110. $valueBinder->bind(':c1', 'value1');
  111. $this->assertCount(2, $valueBinder->bindings());
  112. // Ensure the placeholder generation IS affected by resetCount
  113. $valueBinder->placeholder('param');
  114. $valueBinder->placeholder('param');
  115. $result = $valueBinder->placeholder('param');
  116. $this->assertSame(':param2', $result);
  117. $valueBinder->resetCount();
  118. $placeholder = $valueBinder->placeholder('param');
  119. $this->assertSame(':param0', $placeholder);
  120. $this->assertCount(2, $valueBinder->bindings());
  121. }
  122. /**
  123. * tests the attachTo method
  124. */
  125. public function testAttachTo(): void
  126. {
  127. $valueBinder = new ValueBinder();
  128. $statementMock = $this->createMock(StatementInterface::class);
  129. $statementMock->expects($this->exactly(2))
  130. ->method('bindValue')
  131. ->with(
  132. ...self::withConsecutive(['c0', 'value0', 'string'], ['c1', 'value1', 'string'])
  133. );
  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. /**
  140. * test the __debugInfo method
  141. */
  142. public function testDebugInfo(): void
  143. {
  144. $valueBinder = new ValueBinder();
  145. $valueBinder->bind(':c0', 'value0');
  146. $valueBinder->bind(':c1', 'value1');
  147. $data = $valueBinder->__debugInfo();
  148. $this->assertArrayHasKey('bindings', $data);
  149. $this->assertArrayHasKey(':c0', $data['bindings']);
  150. }
  151. }