Skip to content

Commit 4d6533c

Browse files
committed
WIP WPDBTrait::is_wpdb_method_call(): add basic tests
Check the way the tests are structured. There is for sure room to improve their organization. This commit is just the basic structured generated by Claude. Everything needs to be reviewed.
1 parent 7706f7e commit 4d6533c

File tree

2 files changed

+269
-0
lines changed

2 files changed

+269
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
/*
4+
* Test cases that should NOT be recognized as WPDB method calls.
5+
*/
6+
7+
// Not a WPDB variable.
8+
/* test return false 1 */ $db->prepare( 'SELECT * FROM table' );
9+
/* test return false 2 */ $custom_db->query( 'SELECT * FROM table' );
10+
11+
// WPDB variable but not a method call.
12+
/* test return false 3 */ $wpdb;
13+
/* test return false 4 */ $wpdb['key'];
14+
/* test return false 5 */ $wpdb = new wpdb();
15+
16+
// Property access (not method calls) - definitely no parentheses.
17+
/* test return false 6 */ echo $wpdb->table_name;
18+
/* test return false 7 */ echo $wpdb->insert_id;
19+
20+
// Function calls, not object method calls.
21+
/* test return false 8 */ $result = wpdb( 'SELECT * FROM table' );
22+
23+
// Not WPDB class references.
24+
/* test return false 9 */ wpdb_custom();
25+
/* test return false 10 */ \wpdb_custom();
26+
27+
// Method calls on different variables.
28+
/* test return false 11 */ $database->prepare( 'SELECT * FROM table' );
29+
/* test return false 12 */ $custom->query( 'SELECT * FROM table' );
30+
31+
// WPDB variable but no parentheses (property access).
32+
/* test return false 13 */ echo $wpdb->last_error;
33+
/* test return false 14 */ echo $wpdb->num_rows;
34+
/* test return false 15 */ echo $wpdb->prefix;
35+
36+
/*
37+
* Test cases that should be recognized as WPDB method calls.
38+
*/
39+
40+
// Standard WPDB method calls.
41+
/* test return true 1 */ $wpdb->prepare( 'SELECT * FROM table WHERE id = %d', $id );
42+
/* test return true 2 */ $wpdb->query( 'DELETE FROM table WHERE id = 1' );
43+
/* test return true 3 */ $wpdb->prepare( 'INSERT INTO table (name) VALUES (%s)', $name );
44+
/* test return true 4 */ $wpdb->query( $sql );
45+
46+
// WPDB method calls with whitespace.
47+
/* test return true 5 */ $wpdb -> prepare( 'SELECT * FROM table' );
48+
49+
// Static WPDB method calls.
50+
/* test return true 6 */ $wpdb::prepare( 'SELECT * FROM table WHERE id = %d', $id );
51+
52+
// Global WPDB class references.
53+
/* test return true 7 */ \wpdb::prepare( 'SELECT * FROM table WHERE id = %d', $id );
54+
55+
/*
56+
* Test cases for methods that are not in the target methods list.
57+
*/
58+
59+
// WPDB method calls but not target methods.
60+
/* test non-target method 1 */ $wpdb->insert( 'table', array( 'name' => 'value' ) );
61+
/* test non-target method 2 */ $wpdb->update( 'table', array( 'name' => 'value' ), array( 'id' => 1 ) );
62+
63+
/*
64+
* Test cases for namespaced calls (should handle correctly).
65+
*/
66+
67+
// Global namespace (should work).
68+
/* test namespaced 1 */ wpdb::prepare( 'SELECT * FROM table' );
69+
/* test namespaced 2 */ \wpdb::prepare( 'SELECT * FROM table' );
70+
71+
// Custom namespaces (should not work).
72+
/* test namespaced 3 */ MyNamespace\wpdb::prepare( 'SELECT * FROM table' );
73+
/* test namespaced 4 */ \MyNamespace\wpdb::prepare( 'SELECT * FROM table' );
74+
/* test namespaced 5 */ SomeNamespace\wpdb::prepare( 'SELECT * FROM table' );
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
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

Comments
 (0)