[TASK] Doctrine: Migrate indexed_search part 2
[Packages/TYPO3.CMS.git] / typo3 / sysext / indexed_search / Tests / Functional / Utility / LikeWildcardTest.php
1 <?php
2 namespace TYPO3\CMS\IndexedSearch\Tests\Unit\Utility;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 use TYPO3\CMS\Core\Database\ConnectionPool;
17 use TYPO3\CMS\Core\Tests\FunctionalTestCase;
18 use TYPO3\CMS\Core\Utility\GeneralUtility;
19 use TYPO3\CMS\IndexedSearch\Utility\LikeWildcard;
20
21 /**
22 * This class contains unit tests for the LikeQueryUtility
23 */
24 class LikeWildcardTest extends FunctionalTestCase
25 {
26 /**
27 * @test
28 * @param string $tableName
29 * @param string $fieldName
30 * @param string $likeValue
31 * @param int $wildcard
32 * @param string $expected
33 * @dataProvider getLikeQueryPartDataProvider
34 */
35 public function getLikeQueryPart($tableName, $fieldName, $likeValue, $wildcard, $expected)
36 {
37 $connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($tableName);
38 $subject = LikeWildcard::cast($wildcard);
39 $expected = $connection->quoteIdentifier($fieldName) . ' ' . $expected;
40 $this->assertSame($expected, $subject->getLikeQueryPart($tableName, $fieldName, $likeValue));
41 }
42
43 /**
44 * Returns data sets for the test getLikeQueryPart
45 * Each dataset is an array with the following elements:
46 * - the table name
47 * - the field name
48 * - the search value
49 * - the wildcard mode
50 * - the expected result
51 *
52 * @return array
53 */
54 public function getLikeQueryPartDataProvider()
55 {
56 return [
57 'no placeholders and no wildcard mode' => [
58 'tt_content',
59 'body',
60 'searchstring',
61 LikeWildcard::NONE,
62 "LIKE 'searchstring'"
63 ],
64 'no placeholders and left wildcard mode' => [
65 'tt_content',
66 'body',
67 'searchstring',
68 LikeWildcard::LEFT,
69 "LIKE '%searchstring'"
70 ],
71 'no placeholders and right wildcard mode' => [
72 'tt_content',
73 'body',
74 'searchstring',
75 LikeWildcard::RIGHT,
76 "LIKE 'searchstring%'"
77 ],
78 'no placeholders and both wildcards mode' => [
79 'tt_content',
80 'body',
81 'searchstring',
82 LikeWildcard::BOTH,
83 "LIKE '%searchstring%'"
84 ],
85 'underscore placeholder and left wildcard mode' => [
86 'tt_content',
87 'body',
88 'search_string',
89 LikeWildcard::LEFT,
90 "LIKE '%search\\\\_string'"
91 ],
92 'percent placeholder and right wildcard mode' => [
93 'tt_content',
94 'body',
95 'search%string',
96 LikeWildcard::RIGHT,
97 "LIKE 'search\\\\%string%'"
98 ],
99 'percent and underscore placeholder and both wildcards mode' => [
100 'tt_content',
101 'body',
102 '_search%string_',
103 LikeWildcard::RIGHT,
104 "LIKE '\\\\_search\\\\%string\\\\_%'"
105 ],
106 ];
107 }
108 }