-
Notifications
You must be signed in to change notification settings - Fork 23
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
Conversation
* 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.
There was a problem hiding this 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"
Thank you 💙 |
😄 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 We could maybe consider dropping support for PHP 5.3 in the 2.0 version ? |
Note: This PR will be easiest to review by looking at each commit individually.
Fixes #90
Tests: switch to PHPUnit - set up the basics
Includes allowing for PHPUnit 9.x to be installed when testing against PHP 8.2.
Tests: add test bootstrap and basic UnitTestCase
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.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/classUnitTestCase
allows for adding anIntegrationTestCase
base class later on.Tests: switch to using the PHPUnit test runner
Includes:
@testCase
annotations.require
statements at the top of each test file.Instead the file loading will be handled via the
bootstrap.php
file for PHPUnit.TestCase
and replacing it with thePHP_Parallel_Lint\PhpParallelLint\Tests\UnitTestCase
.->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:
assertSame()
instead ofequal
).UnitTestCase
class to ensure the tests can run without problems on the needed range of PHPUnit versions.ManagerRunTest
as the tests for Nette were run from thetests
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
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.
tests\Unit
directory reflect the structure of thesrc
directory.