You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The "insecure wpdb query" part is obvious from the context, so omitting would make it easier to focus on what's unique about that test case.
Individual tests and data providers
All of the test classes only have two tests: test_unsafe_code and test_safe_code. That's unconventional from a PHPUnit perspective, and introduces a layer of abstraction that doesn't feel necessary. We also hardcode line numbers, which isn't descriptive either.
A more straight-forward way might be to create a unique test function for each payload:
function test_concatenated_variable() {
$payload = <<<PAYLOAD
$wpdb->query( "SELECT * FROM $wpdb->users WHERE foo = '" . $foo . "' LIMIT 1" ); // unsafePAYLOAD$expected = 'WordPressDotOrg.sniffs.DirectDB.UnescapedDBParameter Unescaped parameter $foo used in $wpdb->query';$actual = $phpcs->processString( $payload ); // just psuedocode, not sure if a function like that exists out of the box$this->assertSame( $expected, $actual ); // could also use `expectOutputString()`, `expectException()`, etc, depending on how PHPCS provides the result.}
In cases where there are lots of similar payloads for a type of test, data providers can keep them organized, and give good error messages when a specific case triggers an error.
If PHPCS requires something a file, then maybe we could simulate that with an IO stream?
Putting things in a file is consistent w/ how WPCS does their tests, though. @jrfnl, do you feel like that's best? If so, do you have any suggestions for improving clarity, etc?
The text was updated successfully, but these errors were encountered:
The tests are kind time-consuming to grok. Here are some ideas to improve that:
Descriptive names
The "insecure wpdb query" part is obvious from the context, so omitting would make it easier to focus on what's unique about that test case.
Individual tests and data providers
All of the test classes only have two tests:
test_unsafe_code
andtest_safe_code
. That's unconventional from a PHPUnit perspective, and introduces a layer of abstraction that doesn't feel necessary. We also hardcode line numbers, which isn't descriptive either.A more straight-forward way might be to create a unique test function for each payload:
In cases where there are lots of similar payloads for a type of test, data providers can keep them organized, and give good error messages when a specific case triggers an error.
If PHPCS requires something a file, then maybe we could simulate that with an IO stream?
Putting things in a file is consistent w/ how WPCS does their tests, though. @jrfnl, do you feel like that's best? If so, do you have any suggestions for improving clarity, etc?
The text was updated successfully, but these errors were encountered: