Skip to content

Tests: switch to PHPUnit #110

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

Merged
merged 6 commits into from
Mar 17, 2022
Merged

Tests: switch to PHPUnit #110

merged 6 commits into from
Mar 17, 2022

Conversation

jrfnl
Copy link
Collaborator

@jrfnl jrfnl commented Mar 7, 2022

Note: This PR will be easiest to review by looking at each commit individually.

Fixes #90


Tests: switch to PHPUnit - set up the basics

  • Composer: require PHPUnit instead of the Nette test framework
  • Composer: adjust the scripts to run the tests via PHPUnit.
  • GH Actions: adjust the workflow to run the tests via PHPUnit.
    Includes allowing for PHPUnit 9.x to be installed when testing against PHP 8.2.
  • Add a PHPUnit configuration file.
  • Allow for PHPUnit specific files in various places.
  • Remove the Nette specific PHP ini file.

Tests: add test bootstrap and basic UnitTestCase

  • The test bootstrap.php file has been crafted to allow for running the tests both via a Composer installed PHPUnit version, as well as running the tests via a PHPUnit PHAR file.
    The reason for this is, that as this application supports a wide range of PHP versions, a wide range of PHPUnit versions is needed to test against all the relevant PHP versions.
    When running the tests locally, this would mean running composer update between every test run, which gets tedious fast and slows development down.
    By explicitly supporting running the tests via a PHPUnit PHAR file, this extra step of running composer update between every test run on a different PHP version is eliminated.
  • The UnitTestCase.php file is a bare bones PHPUnit TestCase file.
    This TestCase only contains a couple of methods to provide polyfills for PHPUnit assertions which have changed names across PHPUnit versions.
    An alternative would be to use something like the PHPUnit Polyfills package, however as that requires a minimum PHP version of PHP 5.4, that is not an option at this time.
    The polyfilled functions I added to the UnitTestCase are 100% based on the polyfills from the PHPUnit Polyfills and are tried and tested in that package (which I wrote, so I'm not violating copyright here ;-) ).

Note: the name UnitTestCase file has been chosen deliberately as this package is typically a package which could benefit from having both unit as well as integration tests and naming the file/class UnitTestCase allows for adding an IntegrationTestCase base class later on.

Tests: switch to using the PHPUnit test runner

Includes:

  • Removing the @testCase annotations.
  • Removing the require statements at the top of each test file.
    Instead the file loading will be handled via the bootstrap.php file for PHPUnit.
  • Switching out usage of the Nette TestCase and replacing it with the PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase.
  • Removing the test class instantiations and ->run() commands at the end of the test files in favour of letting the PHPUnit test runner handle this.

Tests: switch to using the PHPUnit assertions and expectations

Includes:

  • Ensuring that assertions use strict type comparisons (assertSame() instead of equal).
  • Uses a few polyfilled assertions/expectations from the UnitTestCase class to ensure the tests can run without problems on the needed range of PHPUnit versions.
  • Includes minor adjustments to how the "paths" are passed in the ManagerRunTest as the tests for Nette were run from the tests directory, while PHPUnit is run from the project root directory.

Tests: use the PL_TESTROOT constant

... as declared in the test bootstrap file to allow for more stability when test files get moved around.

Tests: re-organize test directory structure

  • Move all existing test file in a Unit subdirectory as these are unit tests.
    This allows for more easily adding a second set of (integration) tests at a later point in time.
  • Let the subdirectory structure of the tests\Unit directory reflect the structure of the src directory.
  • Let the class names of the test files reflect the class name + method name of the method under test.

jrfnl added 6 commits March 7, 2022 10:45
* Composer: require PHPUnit instead of the Nette test framework
* Composer: adjust the scripts to run the tests via PHPUnit.
* GH Actions: adjust the workflow to run the tests via PHPUnit.
    Includes allowing for PHPUnit 9.x to be installed when testing against PHP 8.2.
* Add a PHPUnit configuration file.
* Allow for PHPUnit specific files in various places.
* Remove the Nette specific PHP ini file.
* The test `bootstrap.php` file has been crafted to allow for running the tests both via a Composer installed PHPUnit version, as well as running the tests via a PHPUnit PHAR file.
    The reason for this is, that as this application supports a wide range of PHP versions, a wide range of PHPUnit versions is needed to test against all the relevant PHP versions.
    When running the tests locally, this would mean running `composer update` between every test run, which gets tedious fast and slow development down.
    By explicitly supporting running the tests via a PHPUnit PHAR file, this extra step of running `composer update` between every test run on a different PHP version is eliminated.
* The `UnitTestCase.php` file is a bare bones PHPUnit TestCase file.
    This TestCase only contains a couple of methods to provide polyfills for PHPUnit assertions which have changed names across PHPUnit versions.
    An alternative would be to use something like the [PHPUnit Polyfills](https://github.com/Yoast/PHPUnit-Polyfills) package, however as that requires a minimum PHP version of PHP 5.4, that is not an option at this time.
    The polyfilled functions I added to the `UnitTestCase` are 100% based on the polyfills from the PHPUnit Polyfills and are tried and tested in that package (which I wrote, so I'm not violating copyright here ;-) ).

Note: the name `UnitTestCase` file has been chosen deliberately as this package is typically a package which could benefit from having both unit as well as integration tests and naming the file/class `UnitTestCase` allows for adding an `IntegrationTestCase` base class later on.
Includes:
* Removing the `@testCase` annotations.
* Removing the `require` statements at the top of each test file.
    Instead the file loading will be handled via the `bootstrap.php` file for PHPUnit.
* Switching out usage of the Nette `TestCase` and replacing it with the `PHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase`.
* Removing the test class instantiations and `->run()` commands at the end of the test files in favour of letting the PHPUnit test runner handle this.
Includes:
* Ensure that assertions use strict type comparisons (`assertSame()` instead of `equal`).
* Uses a few polyfilled assertions/expectations from the `UnitTestCase` class to ensure the tests can run without problems on the needed range of PHPUnit versions.
* Includes minor adjustments to how the "paths" are passed in the `ManagerRunTest` as the tests for Nette were run from the `tests` directory, while PHPUnit is run from the project root directory.
... as declared in the test bootstrap file to allow for more stability when test files get moved around.
* Move all existing test file in a `Unit` subdirectory as these are unit tests.
    This allows for more easily adding a second set of (integration) tests at a later point in time.
* Let the subdirectory structure of the `tests\Unit` directory reflect the structure of the `src` directory.
* Let the class names of the test files reflect the class name + method name of the method under test.
@jrfnl jrfnl added this to the 2.0.0 milestone Mar 7, 2022
@jrfnl jrfnl requested a review from grogy March 7, 2022 09:52
Copy link
Member

@grogy grogy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am happy. Your commits are as a guide "How to replace Nette Tester with PHPUnit"

@grogy grogy merged commit ff3f688 into develop Mar 17, 2022
@grogy grogy deleted the feature/90-switch-to-phpunit branch March 17, 2022 13:17
@grogy
Copy link
Member

grogy commented Mar 17, 2022

Thank you 💙

@jrfnl
Copy link
Collaborator Author

jrfnl commented Mar 17, 2022

Your commits are as a guide "How to replace Nette Tester with PHPUnit"

😄 Nice! Oh and if it weren't for the fact that PHP 5.3 is still supported in this package, I would have added the PHPUnit Polyfills as well instead of having our "own" polyfilled functions in the TestCase class.

We could maybe consider dropping support for PHP 5.3 in the 2.0 version ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

Change test suite from Nette to PHPUnit
2 participants