Download the PHP package skills17/phpunit-helpers without Composer

On this page you can find all versions of the php package skills17/phpunit-helpers. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package phpunit-helpers

skills17/phpunit-helpers

This package provides some PHPUnit helpers for usage in a skills competition environment. It includes:

Table of contents

Installation

Requirements:

To install this package, simply run the following command:

Additionally, create a phpunit.xml file in the root folder of your task:

To use the provided result printer, the printerClass and printerFile settings are required. The other ones are suggested settings but can be modified to match your requirements.

It is suggested to add the two additional composer scripts so it is possible to run the tests by executing composer test or composer test:json.

Usage

A config.json file needs to be created that contains some information about the task. It should be placed in the root folder of your task, next to the composer.json file.

Grouping

A core concept is test groups. You usually don't want to test everything for one criterion in one test function but instead split it into multiple ones for a cleaner test class and a better overview.

In PHP, tests are grouped by a function prefix defined in the config.json file.

For example, if you have the criteria that GET /countries is implemented correctly and that awards 3 points, you could have the following test functions:

Now, all these functions have a common prefix: CountriesIndex (test can be omitted) and the config.json will look similar to this:

Each one of the test methods will now award 1 point if they pass which results in 3 points in total for the whole group. If you want that all three test functions have to pass to get 3 points (and 0 otherwise), you can set the "required": true attribute.

Non-database tests

To write a test for an application or a part of it that does not require a database, you can simply extend the Skills17\PHPUnit\BaseTest class. It further extends the normal TestCase class of PHPUnit so all PHPUnit assert functions are available as well.

Database tests

If the application under test requires a database, it should get reset before every test run to make sure the data is consistent across multiple test runs. For this case, there are two classes available to extend.

Additionally, a database dump has to be provided and specified in the config.json if it does not have the default name.

Read tests

If the test and the part of the application that gets tested only reads data from the database but never writes/changes it, the Skills17\PHPUnit\Database\ReadTest class should get extended.

To improve the performance, this type of test only resets the database once before the test class gets executed.

Write tests

If the test or the part of the application that gets tested writes data (insert, update, delete), the Skills17\PHPUnit\Database\WriteTest class should get extended.

This makes sure that the database gets reset before every test function in the class gets executed, not only once at the beginning of the class like in a read test. It makes sure that each function starts with a fresh database dump, which is especially useful when a previous function failed and so the database is in an unknown state or when only a subset of functions get executed as they have been filtered.

Extra tests

To prevent cheating, extra tests can be used. They are not available to the competitors and should test exactly the same things as the normal tests do, but with different values.

For example, if your normal test contains a check to search the list of all countries by 'Sw', copy the test into an extra test and change the search string to 'Ca'. Since the competitors will not know the extra test, it would detect statically returned values that were returned to simply satisfy the 'Sw*' tests instead of actually implement the search logic.

Extra tests are detected by their namespace, which should contain \Extra\. That means, if your normal test is in the Skills17\CountriesApp\Test namespace, the extra test could be in Skills17\CountriesApp\Test\Extra. The class- and method names should exactly equal the ones from the normal tests. If they don't, a warning will be displayed.

If an extra test fails while the corresponding normal test passes, a warning will be displayed that a manual review of that test is required since it detected possible cheating. The penalty then has to be decided manually from case to case, the points visible in the output assumed that the test passed and there was no cheating.

For the distribution of the task to the competitors, simply delete the folder containing all extra tests. Nothing else needs to be done or configured.

Output

When the environment variable FORMAT is set to json, the result will be printed in json. Otherwise, a well formatted summary will be visible.

setUp fixture

If you overwrite the setUp fixture in your own test class, make sure to call the parent method.

Best practices

Time limit

It is strongly recommended to enforce a time limit on the tests. Otherwise, an endless loop can break the whole testing pipeline.

The following steps show how a time limit can be configured.

  1. Install the composer package phpunit/php-invoker
  2. Add enforceTimeLimit="true" to the phpunit.xml config file
  3. Annotate all tests with eather @large (60s timeout), @medium (10s timeout) or @small (1s timeout)

The timeouts for all test sizes can be configured.

Writing good tests

For additional advice for writing good tests in a competition environment, read this blog post.

License

MIT


All versions of phpunit-helpers with dependencies

PHP Build Version
Package Version
Requires php Version >=7.4
phpunit/phpunit Version ^9
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package skills17/phpunit-helpers contains the following files

Loading the files please wait ....