Skip to content

Commit be1faba

Browse files
committed
CONTRIBUTING: update the test writing tutorial
... to no longer use the `POSIXFunctions` sniff as an example as that sniff will be removed in WPCS 4.0.0 (and is now deprecated).
1 parent 81d9dd4 commit be1faba

File tree

1 file changed

+37
-36
lines changed

1 file changed

+37
-36
lines changed

.github/CONTRIBUTING.md

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -124,16 +124,16 @@ OK (60 tests, 6 assertions)
124124
125125
### Unit Testing conventions
126126
127-
If you look inside the `WordPress/Tests` subdirectory, you'll see the structure mimics the `WordPress/Sniffs` subdirectory structure. For example, the `WordPress/Sniffs/PHP/POSIXFunctionsSniff.php` sniff has its unit test class defined in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.php` which checks the `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc` test case file. See the file naming convention?
127+
If you look inside the `WordPress/Tests` subdirectory, you'll see the structure mimics the `WordPress/Sniffs` subdirectory structure. For example, the `WordPress/Sniffs/PHP/TypeCastsSniff.php` sniff has its unit test class defined in `WordPress/Tests/PHP/TypeCastsUnitTest.php` which checks the `WordPress/Tests/PHP/TypeCastsUnitTest.inc` test case file. See the file naming convention?
128128

129-
Lets take a look at what's inside `POSIXFunctionsUnitTest.php`:
129+
Lets take a look at what's inside `TypeCastsUnitTest.php`:
130130
131131
```php
132132
namespace WordPressCS\WordPress\Tests\PHP;
133133
134134
use PHP_CodeSniffer\Tests\Standards\AbstractSniffUnitTest;
135135
136-
final class POSIXFunctionsUnitTest extends AbstractSniffUnitTest {
136+
final class TypeCastsUnitTest extends AbstractSniffUnitTest {
137137
138138
/**
139139
* Returns the lines where errors should occur.
@@ -142,53 +142,54 @@ final class POSIXFunctionsUnitTest extends AbstractSniffUnitTest {
142142
*/
143143
public function getErrorList() {
144144
return array(
145+
10 => 1,
146+
11 => 1,
145147
13 => 1,
146-
16 => 1,
147-
18 => 1,
148-
20 => 1,
149-
22 => 1,
150-
24 => 1,
151148
26 => 1,
149+
27 => 1,
150+
28 => 1,
152151
);
153152
}
154153
155154
...
156155
}
157156
```
158157
159-
Also note the class name convention. The method `getErrorList()` MUST return an array of line numbers indicating errors (when running `phpcs`) found in `WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc`. Similarly, the `getWarningList()` method must return an array of line numbers with the number of expected warnings.
158+
Also note the class name convention. The method `getErrorList()` MUST return an array of line numbers indicating errors (when running `phpcs`) found in `WordPress/Tests/PHP/TypeCastsUnitTest.inc`. Similarly, the `getWarningList()` method must return an array of line numbers with the number of expected warnings.
160159
161160
If you run the following from the root directory of your WordPressCS clone:
162161
163162
```sh
164-
$ "vendor/bin/phpcs" --standard=Wordpress -s ./WordPress/Tests/PHP/POSIXFunctionsUnitTest.inc --sniffs=WordPress.PHP.POSIXFunctions
165-
...
166-
--------------------------------------------------------------------------------
167-
FOUND 7 ERRORS AFFECTING 7 LINES
168-
--------------------------------------------------------------------------------
169-
13 | ERROR | ereg() has been deprecated since PHP 5.3 and removed in PHP 7.0,
170-
| | please use preg_match() instead.
171-
| | (WordPress.PHP.POSIXFunctions.ereg_ereg)
172-
16 | ERROR | eregi() has been deprecated since PHP 5.3 and removed in PHP 7.0,
173-
| | please use preg_match() instead.
174-
| | (WordPress.PHP.POSIXFunctions.ereg_eregi)
175-
18 | ERROR | ereg_replace() has been deprecated since PHP 5.3 and removed in
176-
| | PHP 7.0, please use preg_replace() instead.
177-
| | (WordPress.PHP.POSIXFunctions.ereg_replace_ereg_replace)
178-
20 | ERROR | eregi_replace() has been deprecated since PHP 5.3 and removed in
179-
| | PHP 7.0, please use preg_replace() instead.
180-
| | (WordPress.PHP.POSIXFunctions.ereg_replace_eregi_replace)
181-
22 | ERROR | split() has been deprecated since PHP 5.3 and removed in PHP 7.0,
182-
| | please use explode(), str_split() or preg_split() instead.
183-
| | (WordPress.PHP.POSIXFunctions.split_split)
184-
24 | ERROR | spliti() has been deprecated since PHP 5.3 and removed in PHP
185-
| | 7.0, please use explode(), str_split() or preg_split()
186-
| | instead. (WordPress.PHP.POSIXFunctions.split_spliti)
187-
26 | ERROR | sql_regcase() has been deprecated since PHP 5.3 and removed in
188-
| | PHP 7.0, please use preg_match() instead.
189-
| | (WordPress.PHP.POSIXFunctions.ereg_sql_regcase)
190-
--------------------------------------------------------------------------------
163+
$ "vendor/bin/phpcs" --standard=Wordpress -s ./WordPress/Tests/PHP/TypeCastsUnitTest.inc --sniffs=WordPress.PHP.TypeCasts
191164
...
165+
----------------------------------------------------------------------------------------------------
166+
FOUND 6 ERRORS AND 4 WARNINGS AFFECTING 10 LINES
167+
----------------------------------------------------------------------------------------------------
168+
10 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(double)"
169+
| | (WordPress.PHP.TypeCasts.DoubleRealFound)
170+
11 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(real)"
171+
| | (WordPress.PHP.TypeCasts.DoubleRealFound)
172+
13 | ERROR | [ ] Using the "(unset)" cast is forbidden as the type cast is removed in PHP 8.0.
173+
| | Use the "unset()" language construct instead.
174+
| | (WordPress.PHP.TypeCasts.UnsetFound)
175+
15 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "(binary)"
176+
| | (WordPress.PHP.TypeCasts.BinaryFound)
177+
16 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "b"
178+
| | (WordPress.PHP.TypeCasts.BinaryFound)
179+
17 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "b"
180+
| | (WordPress.PHP.TypeCasts.BinaryFound)
181+
26 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(double)"
182+
| | (WordPress.PHP.TypeCasts.DoubleRealFound)
183+
27 | ERROR | [x] Normalized type keywords must be used; expected "(float)" but found "(real)"
184+
| | (WordPress.PHP.TypeCasts.DoubleRealFound)
185+
28 | ERROR | [ ] Using the "(unset)" cast is forbidden as the type cast is removed in PHP 8.0.
186+
| | Use the "unset()" language construct instead.
187+
| | (WordPress.PHP.TypeCasts.UnsetFound)
188+
29 | WARNING | [ ] Using binary casting is strongly discouraged. Found: "(binary)"
189+
| | (WordPress.PHP.TypeCasts.BinaryFound)
190+
----------------------------------------------------------------------------------------------------
191+
PHPCBF CAN FIX THE 4 MARKED SNIFF VIOLATIONS AUTOMATICALLY
192+
----------------------------------------------------------------------------------------------------
192193
```
193194
You'll see the line number and number of ERRORs we need to return in the `getErrorList()` method.
194195

0 commit comments

Comments
 (0)