1+ <?php
2+ /**
3+ * WordPress Coding Standard.
4+ *
5+ * @package WPCS\WordPressCodingStandards
6+ * @link https://github.com/WordPress/WordPress-Coding-Standards
7+ * @license https://opensource.org/licenses/MIT MIT
8+ */
9+
10+ namespace WordPressCS \WordPress \Util \Tests \Helpers \WPDBTrait ;
11+
12+ use PHP_CodeSniffer \Files \File ;
13+ use WordPressCS \WordPress \Helpers \WPDBTrait ;
14+ use PHPCSUtils \TestUtils \UtilityMethodTestCase ;
15+
16+ /**
17+ * Tests for the `WPDBTrait::is_wpdb_method_call()` utility method.
18+ *
19+ * @since 3.3.0
20+ *
21+ * @covers \WordPressCS\WordPress\Helpers\WPDBTrait::is_wpdb_method_call()
22+ */
23+ final class IsWpdbMethodCallUnitTest extends UtilityMethodTestCase {
24+
25+ /**
26+ * Test class using the WPDBTrait for testing purposes.
27+ */
28+ private static $ testClass ;
29+
30+ /**
31+ * Set up the test class.
32+ *
33+ * @beforeClass
34+ *
35+ * @return void
36+ */
37+ public static function setUpBeforeClass (): void {
38+ parent ::setUpBeforeClass ();
39+
40+ self ::$ testClass = new class () {
41+ use WPDBTrait;
42+
43+ public $ methodPtr ;
44+ public $ i ;
45+ public $ end ;
46+
47+ public function testIsWpdbMethodCall ( File $ phpcsFile , $ stackPtr , array $ target_methods ) {
48+ return $ this ->is_wpdb_method_call ( $ phpcsFile , $ stackPtr , $ target_methods );
49+ }
50+ };
51+ }
52+
53+ /**
54+ * Test is_wpdb_method_call() returns false if given token is not a WPDB method call.
55+ *
56+ * @dataProvider dataIsWpdbMethodCallShouldReturnFalse
57+ *
58+ * @param string $commentString The comment which prefaces the target token in the test file.
59+ * @param int|string $tokenType The token type to search for.
60+ *
61+ * @return void
62+ */
63+ public function testIsWpdbMethodCallShouldReturnFalse ( $ commentString , $ tokenType ) {
64+ $ stackPtr = $ this ->getTargetToken ( $ commentString , $ tokenType );
65+ $ result = self ::$ testClass ->testIsWpdbMethodCall ( self ::$ phpcsFile , $ stackPtr , array ( 'prepare ' => true , 'query ' => true ) );
66+ $ this ->assertFalse ( $ result );
67+ }
68+
69+ /**
70+ * Data provider.
71+ *
72+ * @return array
73+ * @see testIsWpdbMethodCallShouldReturnFalse()
74+ */
75+ public static function dataIsWpdbMethodCallShouldReturnFalse () {
76+ return array (
77+ array ( '/* test return false 1 */ ' , \T_VARIABLE ),
78+ array ( '/* test return false 2 */ ' , \T_VARIABLE ),
79+ array ( '/* test return false 3 */ ' , \T_VARIABLE ),
80+ array ( '/* test return false 4 */ ' , \T_VARIABLE ),
81+ array ( '/* test return false 5 */ ' , \T_VARIABLE ),
82+ array ( '/* test return false 6 */ ' , \T_VARIABLE ),
83+ array ( '/* test return false 7 */ ' , \T_VARIABLE ),
84+ array ( '/* test return false 8 */ ' , \T_VARIABLE ),
85+ array ( '/* test return false 9 */ ' , \T_STRING ),
86+ array ( '/* test return false 10 */ ' , \T_STRING ),
87+ array ( '/* test return false 11 */ ' , \T_VARIABLE ),
88+ array ( '/* test return false 12 */ ' , \T_VARIABLE ),
89+ array ( '/* test return false 13 */ ' , \T_VARIABLE ),
90+ array ( '/* test return false 14 */ ' , \T_VARIABLE ),
91+ array ( '/* test return false 15 */ ' , \T_VARIABLE ),
92+ );
93+ }
94+
95+ /**
96+ * Test is_wpdb_method_call() returns true for valid WPDB method calls.
97+ *
98+ * @dataProvider dataIsWpdbMethodCallShouldReturnTrue
99+ *
100+ * @param string $commentString The comment which prefaces the target token in the test file.
101+ * @param int|string $tokenType The token type to search for.
102+ *
103+ * @return void
104+ */
105+ public function testIsWpdbMethodCallShouldReturnTrue ( $ commentString , $ tokenType ) {
106+ $ stackPtr = $ this ->getTargetToken ( $ commentString , $ tokenType );
107+ $ result = self ::$ testClass ->testIsWpdbMethodCall ( self ::$ phpcsFile , $ stackPtr , array ( 'prepare ' => true , 'query ' => true ) );
108+ $ this ->assertTrue ( $ result );
109+ }
110+
111+ /**
112+ * Data provider.
113+ *
114+ * @return array
115+ * @see testIsWpdbMethodCallShouldReturnTrue()
116+ */
117+ public static function dataIsWpdbMethodCallShouldReturnTrue () {
118+ return array (
119+ array ( '/* test return true 1 */ ' , \T_VARIABLE ),
120+ array ( '/* test return true 2 */ ' , \T_VARIABLE ),
121+ array ( '/* test return true 3 */ ' , \T_VARIABLE ),
122+ array ( '/* test return true 4 */ ' , \T_VARIABLE ),
123+ array ( '/* test return true 5 */ ' , \T_VARIABLE ),
124+ array ( '/* test return true 6 */ ' , \T_VARIABLE ),
125+ array ( '/* test return true 7 */ ' , \T_STRING ),
126+ );
127+ }
128+
129+ /**
130+ * Test is_wpdb_method_call() returns false for WPDB method calls with non-target methods.
131+ *
132+ * @dataProvider dataIsWpdbMethodCallShouldReturnFalseForNonTargetMethods
133+ *
134+ * @param string $commentString The comment which prefaces the target token in the test file.
135+ * @param int|string $tokenType The token type to search for.
136+ *
137+ * @return void
138+ */
139+ public function testIsWpdbMethodCallShouldReturnFalseForNonTargetMethods ( $ commentString , $ tokenType ) {
140+ $ stackPtr = $ this ->getTargetToken ( $ commentString , $ tokenType );
141+ $ result = self ::$ testClass ->testIsWpdbMethodCall ( self ::$ phpcsFile , $ stackPtr , array ( 'prepare ' => true ) );
142+ $ this ->assertFalse ( $ result );
143+ }
144+
145+ /**
146+ * Data provider.
147+ *
148+ * @return array
149+ * @see testIsWpdbMethodCallShouldReturnFalseForNonTargetMethods()
150+ */
151+ public static function dataIsWpdbMethodCallShouldReturnFalseForNonTargetMethods () {
152+ return array (
153+ array ( '/* test non-target method 1 */ ' , \T_VARIABLE ),
154+ array ( '/* test non-target method 2 */ ' , \T_VARIABLE ),
155+ );
156+ }
157+
158+ /**
159+ * Test is_wpdb_method_call() correctly handles namespaced calls.
160+ *
161+ * @dataProvider dataIsWpdbMethodCallShouldHandleNamespacedCalls
162+ *
163+ * @param string $commentString The comment which prefaces the target token in the test file.
164+ * @param int|string $tokenType The token type to search for.
165+ * @param bool $expected Whether the result should be true or false.
166+ *
167+ * @return void
168+ */
169+ public function testIsWpdbMethodCallShouldHandleNamespacedCalls ( $ commentString , $ tokenType , $ expected ) {
170+ $ stackPtr = $ this ->getTargetToken ( $ commentString , $ tokenType );
171+ $ result = self ::$ testClass ->testIsWpdbMethodCall ( self ::$ phpcsFile , $ stackPtr , array ( 'prepare ' => true , 'query ' => true ) );
172+
173+ if ( $ expected ) {
174+ $ this ->assertTrue ( $ result );
175+ } else {
176+ $ this ->assertFalse ( $ result );
177+ }
178+ }
179+
180+ /**
181+ * Data provider.
182+ *
183+ * @return array
184+ * @see testIsWpdbMethodCallShouldHandleNamespacedCalls()
185+ */
186+ public static function dataIsWpdbMethodCallShouldHandleNamespacedCalls () {
187+ return array (
188+ array ( '/* test namespaced 1 */ ' , \T_STRING , true ),
189+ array ( '/* test namespaced 2 */ ' , \T_STRING , true ),
190+ array ( '/* test namespaced 3 */ ' , \T_STRING , false ),
191+ array ( '/* test namespaced 4 */ ' , \T_STRING , false ),
192+ array ( '/* test namespaced 5 */ ' , \T_STRING , false ),
193+ );
194+ }
195+ }
0 commit comments