Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Filters: add tests for GitModified and GitStaged filters + improve existing Filter test #201

Merged
merged 4 commits into from
Dec 31, 2023

Conversation

jrfnl
Copy link
Member

@jrfnl jrfnl commented Dec 31, 2023

Description

Prelim PR to allow for safely making the changes proposed in #198.

Tests/Filters: new AbstractFilterTestCase

... to contain some basic utilities and logic for use in Filter related tests.

Includes switching the Filter/AcceptTest to use the new abstract and to use the available utilities.

Tests/AcceptTest: use named data sets

With non-named data sets, when a test fails, PHPUnit will display the number of the test which failed.

With tests which have a lot of data sets, this makes it interesting (and time-consuming) to debug those, as one now has to figure out which of the data sets in the data provider corresponds to that number.

Using named data sets makes debugging failing tests more straight forward as PHPUnit will display the data set name instead of the number.
Using named data sets also documents what exactly each data set is testing.

Includes making the data type in the docblocks more specific.

Tests/AcceptTest: minor tweaks

  • Import all used classes.
  • Make the data provider static.
  • Prevent a potential call to stty by setting the report width.

Filters: add tests for GitModified and GitStaged filters

This is an initial set of tests for the GitModified and GitStaged filters.

The tests are largely the same for both filters as the logic under test is also largely the same.
Still, having separate test classes will allow for differentiating the tests if the logic in the filters themselves would start to diverge over time or if the output of the individual git commands would give reason to add extra tests.

Notes:

  • To allow for mocking the output of the "git modified"/"git staged" commands, the handling of the function call to exec has been moved to a separate method in both the GitModified, as well as the GitStaged class.
  • This commit adds three new helper methods to the AbstractFilterTestCase:
    • getMockedClass() to handle creating a mock object of the filter under test in a PHPUnit cross-version compatible manner.
    • getBaseDir() to get the project root directory for use as the $basedir for the filters.
    • getFakeFileList() to get, as the name says, a fake file list for use in the tests, which contains a range of different file/directory situations which filter may need to take into account.
      Note: . and .. are not included as the PHP_CodeSniffer\Files\FileList (which is basically what we're faking here) does not include those either, so that would create unrealistic test scenarios.
      Also note that, aside from some non-existent files added to the list for testing purposes, the files in this list do actually need to exist for the tests to be valid.

Suggested changelog entry

N/A

Related issues/external references

Related to #146
Related to #198 / #199

... to contain some basic utilities and logic for use in Filter related tests.

Includes switching the `Filter/AcceptTest` to use the new abstract and to use the available utilities.
With non-named data sets, when a test fails, PHPUnit will display the number of the test which failed.

With tests which have a _lot_ of data sets, this makes it _interesting_ (and time-consuming) to debug those, as one now has to figure out which of the data sets in the data provider corresponds to that number.

Using named data sets makes debugging failing tests more straight forward as PHPUnit will display the data set name instead of the number.
Using named data sets also documents what exactly each data set is testing.

Includes making the data type in the docblocks more specific.
* Import all used classes.
* Make the data provider static.
* Prevent a potential call to `stty` by setting the report width.
This is an initial set of tests for the `GitModified` and `GitStaged` filters.

The tests are largely the same for both filters as the logic under test is also largely the same.
Still, having separate test classes will allow for differentiating the tests if the logic in the filters themselves would start to diverge over time or if the output of the individual git commands would give reason to add extra tests.

Notes:
* To allow for mocking the output of the "git modified"/"git staged" commands, the handling of the function call to `exec` has been moved to a separate method in both the `GitModified`, as well as the `GitStaged` class.
* This commit adds three new helper methods to the `AbstractFilterTestCase`:
    - `getMockedClass()` to handle creating a mock object of the filter under test in a PHPUnit cross-version compatible manner.
    - `getBaseDir()` to get the project root directory for use as the `$basedir` for the filters.
    - `getFakeFileList()` to get, as the name says, a fake file list for use in the tests, which contains a range of different file/directory situations which filter may need to take into account.
        Note: `.` and `..` are not included as the `PHP_CodeSniffer\Files\FileList` (which is basically what we're faking here) does not include those either, so that would create unrealistic test scenarios.
        Also note that, aside from some non-existent files added to the list for testing purposes, the files in this list _do_ actually need to exist for the tests to be valid.
@jrfnl jrfnl added this to the 3.x Next milestone Dec 31, 2023
@jrfnl jrfnl merged commit 33429dd into master Dec 31, 2023
46 checks passed
@jrfnl jrfnl deleted the feature/filter-add-tests-for-gitmodified-gitstaged branch December 31, 2023 08:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant