Skip to content

Commit 29374b7

Browse files
Merge pull request #62 from alleyinteractive/feature/issue-46/compare-stub-errors-with-expected
Issue-46: Improve tests to match the error reported for the stub files with the expected error
2 parents e38770a + 11bc927 commit 29374b7

20 files changed

+236
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
This project adheres to [Keep a CHANGELOG](https://keepachangelog.com/en/1.0.0/).
44

5+
### 2.3.3
6+
- Changed: Improves tests to ensure tests expected to fail only fail for expected reasons.
7+
58
### 2.3.2
69

710
- Lock `wp-coding-standards/wpcs` to 3.1.0 to avoid issues with the latest version.

README.md

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,19 @@ references these rules, like this:
7373
When contributing to this project, modifications to the ruleset should have a
7474
corresponding test in the `tests` directory. For the most part, this takes the
7575
form of a passing test in `tests/fixtures/pass` and a failing one in
76-
`tests/fixtures/fail`. You can run the tests with `composer phpunit`. If you
76+
`tests/fixtures/fail`.
77+
78+
You can run the tests with `composer phpunit`. If you
7779
want to run PHPCS against the test fixtures, you can run
7880
`composer phpcs:fixtures` to ensure that what is passing/failing matches your
79-
expectations. For failing fixtures in `tests/fixtures/fail`, we recommend
81+
expectations.
82+
83+
### Special Considerations for failure expectations
84+
For failing fixtures in `tests/fixtures/fail`, we recommend
8085
keeping the files smaller and focused on the specific sniff being tested.
86+
A corresponding file should be created in `tests/fixtures/fail/expectations`
87+
referencing the expected source of the failures for that test file. Name the file
88+
the same as you do the fixture (e.g. `tests/fixtures/fail/foo.php` and `tests/fixtures/fail/expectations/foo.php`).
8189

8290
## Changelog
8391

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
"phpcbf": "phpcbf --standard=Alley-Interactive tests/*Test.php",
3131
"phpcs": "phpcs --standard=Alley-Interactive tests/*Test.php -sn",
3232
"phpcs:fixtures": "phpcs -sn --standard=Alley-Interactive tests/fixtures",
33+
"phpcs:expectations": "phpcs -sn --standard=Alley-Interactive tests/fixtures/fail/expectations",
3334
"phpunit": "phpunit",
3435
"test": [
3536
"@phpcs",

tests/FixtureTest.php

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,22 @@ public function test_passing_fixtures( string $file ): void {
3232
* Test that fixtures fail.
3333
*
3434
* @param string $file The file to test.
35+
* @param string $expectation The expectations file for this test.
3536
*/
3637
#[DataProvider( 'failing_fixture_data_provider' )]
37-
public function test_failing_fixtures( string $file ): void {
38-
$this->process_phpcs_output( $this->run_phpcs( $file ), expect_to_fail: true );
38+
public function test_failing_fixtures( string $file, ?string $expectation = null ): void {
39+
40+
$expectations = [];
41+
42+
if ( ! empty( $expectation ) && file_exists( $expectation ) ) {
43+
$expectations = include $expectation;
44+
}
45+
46+
$this->process_phpcs_output(
47+
$this->run_phpcs( $file ),
48+
ignored_errors: $expectations,
49+
expect_to_fail: true
50+
);
3951
}
4052

4153
/**
@@ -53,7 +65,19 @@ public static function passing_fixture_data_provider(): array {
5365
* @return array<array<string|array<string>>>
5466
*/
5567
public static function failing_fixture_data_provider(): array {
56-
return self::get_files_in_directory( __DIR__ . '/fixtures/fail' );
68+
$data = self::get_files_in_directory( __DIR__ . '/fixtures/fail' );
69+
$expectations = self::get_files_in_directory( __DIR__ . '/fixtures/fail/expectations' );
70+
71+
foreach ( $data as $key => $data_set ) {
72+
if ( ! isset( $expectations[ $key ] ) ) {
73+
$data[ $key ][] = null;
74+
continue;
75+
}
76+
77+
$data[ $key ][] = $expectations[ $key ][0];
78+
}
79+
80+
return $data;
5781
}
5882

5983
/**
@@ -76,6 +100,10 @@ protected static function get_files_in_directory( string $directory ): array {
76100
$data = [];
77101

78102
foreach ( $files as $file ) {
103+
if ( is_dir( $file ) ) {
104+
continue;
105+
}
106+
79107
$data[ basename( $file ) ] = [ $file ];
80108
}
81109

tests/PhpcsHelper.php

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,13 +76,6 @@ protected function process_phpcs_output(
7676
);
7777

7878
if ( ! empty( $unexpected_messages ) ) {
79-
// Bail if we expect the test to fail and there are errors.
80-
if ( $expect_to_fail ) {
81-
$this->assertTrue( true );
82-
83-
return;
84-
}
85-
8679
$this->fail(
8780
sprintf(
8881
'Unexpected errors found in %s: %s',
@@ -104,8 +97,12 @@ protected function process_phpcs_output(
10497
),
10598
);
10699

107-
if ( $expect_to_fail ) {
100+
if ( $expect_to_fail && empty( $errors ) ) {
108101
$this->fail( 'Test did not fail as expected' );
102+
} else if ( $expect_to_fail && ! empty( $errors ) ) {
103+
$this->assertNotEmpty( $errors );
104+
} else {
105+
$this->assertEmpty( $errors );
109106
}
110107
}
111108
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Array expectations file.
4+
*
5+
* @package Alley\WP\Coding_Standards
6+
*/
7+
8+
/**
9+
* Return the array of expected failures that this test file should generate.
10+
*/
11+
return [
12+
'Generic.Arrays.DisallowLongArraySyntax.Found',
13+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Function Empty Arguments expectations file.
4+
*
5+
* @package Alley\WP\Coding_Standards
6+
*/
7+
8+
/**
9+
* Return the array of expected failures that this test file should generate.
10+
*/
11+
return [
12+
'PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket',
13+
'PSR2.Methods.FunctionCallSignature.SpaceBeforeCloseBracket',
14+
];
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Functions expectations file.
4+
*
5+
* @package Alley\WP\Coding_Standards
6+
*/
7+
8+
/**
9+
* Return the array of expected failures that this test file should generate.
10+
*/
11+
return [
12+
'PSR2.Methods.FunctionCallSignature.SpaceAfterOpenBracket',
13+
'PSR2.Methods.FunctionCallSignature.SpaceBeforeCloseBracket',
14+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Function Empty Arguments expectations file.
4+
*
5+
* @package Alley\WP\Coding_Standards
6+
*/
7+
8+
/**
9+
* Return the array of expected failures that this test file should generate.
10+
*/
11+
return [
12+
'Generic.Files.OneObjectStructurePerFile.MultipleFound',
13+
];
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
/**
3+
* Shorthand Tag expectations file.
4+
*
5+
* @package Alley\WP\Coding_Standards
6+
*/
7+
8+
/**
9+
* Return the array of expected failures that this test file should generate.
10+
*/
11+
return [
12+
'Alley.PHP.DisallowShortEchoTag.Found',
13+
];

0 commit comments

Comments
 (0)