AbstractHelperTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * \Magento\Framework\DB\Helper\AbstractHelper test case
  4. *
  5. * Copyright © Magento, Inc. All rights reserved.
  6. * See COPYING.txt for license details.
  7. */
  8. namespace Magento\Framework\DB\Test\Unit\Helper;
  9. class AbstractHelperTest extends \PHPUnit\Framework\TestCase
  10. {
  11. /**
  12. * @var \Magento\Framework\DB\Helper\AbstractHelper|\PHPUnit_Framework_MockObject_MockObject
  13. */
  14. protected $_model;
  15. /**
  16. * @var \Magento\Framework\App\ResourceConnection|\PHPUnit_Framework_MockObject_MockObject
  17. */
  18. protected $_resourceMock;
  19. /**
  20. * @var \Magento\Framework\DB\Adapter\AdapterInterface|\PHPUnit_Framework_MockObject_MockObject
  21. */
  22. protected $_adapterMock;
  23. protected function setUp()
  24. {
  25. $this->_adapterMock = $this->createMock(\Magento\Framework\DB\Adapter\AdapterInterface::class);
  26. $this->_resourceMock = $this->createMock(\Magento\Framework\App\ResourceConnection::class);
  27. $this->_resourceMock->expects($this->any())
  28. ->method('getConnection')
  29. ->with('prefix')
  30. ->will($this->returnValue($this->_adapterMock));
  31. $this->_model = $this->getMockForAbstractClass(
  32. \Magento\Framework\DB\Helper\AbstractHelper::class,
  33. [$this->_resourceMock, 'prefix'],
  34. '',
  35. true,
  36. true,
  37. true,
  38. ['addLikeEscape']
  39. );
  40. }
  41. /**
  42. * @param string $expected
  43. * @param array $data
  44. * @dataProvider escapeLikeValueDataProvider
  45. */
  46. public function testEscapeLikeValue($expected, array $data)
  47. {
  48. $this->assertEquals($expected, $this->_model->escapeLikeValue($data['value'], $data['options']));
  49. }
  50. public function testGetCILike()
  51. {
  52. $field = 'field';
  53. $value = 'value';
  54. $options = [];
  55. $this->_adapterMock->expects($this->once())
  56. ->method('quoteIdentifier')
  57. ->with($field)
  58. ->will($this->returnArgument(0));
  59. $this->_model->expects($this->once())
  60. ->method('addLikeEscape')
  61. ->with($value, $options)
  62. ->will($this->returnArgument(0));
  63. $result = $this->_model->getCILike($field, $value, $options);
  64. $this->assertInstanceOf('Zend_Db_Expr', $result);
  65. $this->assertEquals($field . ' LIKE ' . $value, (string)$result);
  66. }
  67. /**
  68. * @return array
  69. */
  70. public function escapeLikeValueDataProvider()
  71. {
  72. return [
  73. [
  74. '',
  75. [
  76. 'value' => '',
  77. 'options' => []
  78. ],
  79. ],
  80. [
  81. 'LIKE \%string\_end',
  82. [
  83. 'value' => 'LIKE %string_end',
  84. 'options' => []
  85. ]
  86. ],
  87. [
  88. 'LIKE \%string_end',
  89. [
  90. 'value' => 'LIKE %string_end',
  91. 'options' => [
  92. 'allow_symbol_mask' => true,
  93. ]
  94. ]
  95. ],
  96. [
  97. 'LIKE %string\_end',
  98. [
  99. 'value' => 'LIKE %string_end',
  100. 'options' => [
  101. 'allow_string_mask' => true,
  102. ]
  103. ]
  104. ],
  105. [
  106. 'LIKE %string_end',
  107. [
  108. 'value' => 'LIKE %string_end',
  109. 'options' => [
  110. 'allow_symbol_mask' => true,
  111. 'allow_string_mask' => true,
  112. ]
  113. ]
  114. ],
  115. [
  116. '%string%',
  117. [
  118. 'value' => 'string',
  119. 'options' => [
  120. 'position' => 'any',
  121. ]
  122. ]
  123. ],
  124. [
  125. 'string%',
  126. [
  127. 'value' => 'string',
  128. 'options' => [
  129. 'position' => 'start',
  130. ]
  131. ]
  132. ],
  133. [
  134. '%string',
  135. [
  136. 'value' => 'string',
  137. 'options' => [
  138. 'position' => 'end',
  139. ]
  140. ]
  141. ]
  142. ];
  143. }
  144. }