|
| 1 | +# Testing Guide |
| 2 | + |
| 3 | +This document describes how to: |
| 4 | +- create and run tests for your development work, |
| 5 | +- ensure code meets coding standards, for best practices and security, |
| 6 | + |
| 7 | +If you're new to creating and running tests, this guide will walk you through how to do this. |
| 8 | + |
| 9 | +For those more experienced with creating and running tests, our tests are written in PHP using [PHPUnit](https://phpunit.de/). |
| 10 | + |
| 11 | +## Prerequisites |
| 12 | + |
| 13 | +If you haven't yet set up your local development environment, refer to the [Setup Guide](SETUP.md). |
| 14 | + |
| 15 | +If you haven't yet created a branch and made any code changes, refer to the [Development Guide](DEVELOPMENT.md) |
| 16 | + |
| 17 | +## Write (or modify) a test |
| 18 | + |
| 19 | +If your work creates new functionality, write a test. |
| 20 | + |
| 21 | +If your work fixes existing functionality, check if a test exists. Either update that test, or create a new test if one doesn't exist. |
| 22 | + |
| 23 | +Tests are written in PHP using [PHPUnit](https://phpunit.de/), and the existing `tests/ConvertKitAPITest.php` is a good place to start as a guide. |
| 24 | + |
| 25 | +## Run PHPUnit |
| 26 | + |
| 27 | +Once you have written your code and tests, run the tests to make sure there are no errors. |
| 28 | + |
| 29 | +To run the tests, enter the following commands in a separate Terminal window: |
| 30 | + |
| 31 | +```bash |
| 32 | +vendor/bin/phpunit --verbose --stop-on-failure |
| 33 | +``` |
| 34 | + |
| 35 | +If a test fails, you can inspect the output. |
| 36 | + |
| 37 | +Any errors should be corrected by making applicable code or test changes. |
| 38 | + |
| 39 | +## Run PHP CodeSniffer |
| 40 | + |
| 41 | +[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) checks that all code meets the [PSR-12 Coding Standards](https://www.php-fig.org/psr/psr-12/). |
| 42 | + |
| 43 | +To run CodeSniffer on tests, enter the following command: |
| 44 | + |
| 45 | +```bash |
| 46 | +vendor/bin/phpcs |
| 47 | +``` |
| 48 | + |
| 49 | +Any errors should be corrected by either: |
| 50 | +- making applicable code changes |
| 51 | +- (Experimental) running `vendor/bin/phpcbf` to automatically fix coding standards |
| 52 | + |
| 53 | +Need to change the coding standard rules applied? Either: |
| 54 | +- ignore a rule in the affected code, by adding `phpcs:ignore {rule}`, where {rule} is the given rule that failed in the above output. |
| 55 | +- edit the [phpcs.tests.xml](phpcs.xml) file. |
| 56 | + |
| 57 | +**Rules can be ignored with caution**, but it's essential that rules relating to coding style and inline code commenting / docblocks remain. |
| 58 | + |
| 59 | +## Run PHP CodeSniffer for Tests |
| 60 | + |
| 61 | +[PHP_CodeSniffer](https://github.com/squizlabs/PHP_CodeSniffer) checks that all test code meets the [PSR-12 Coding Standards](https://www.php-fig.org/psr/psr-12/). |
| 62 | + |
| 63 | +To run CodeSniffer on tests, enter the following command: |
| 64 | + |
| 65 | +```bash |
| 66 | +vendor/bin/phpcs --standard=phpcs.tests.xml |
| 67 | +``` |
| 68 | + |
| 69 | +`--standard=phpcs.tests.xml` tells PHP CodeSniffer to use the use the [phpcs.tests.xml](phpcs.tests.xml) configuration file |
| 70 | + |
| 71 | +Any errors should be corrected by either: |
| 72 | +- making applicable code changes |
| 73 | +- (Experimental) running `vendor/bin/phpcbf --standard=phpcs.tests.xml` to automatically fix coding standards |
| 74 | + |
| 75 | +Need to change the coding standard rules applied? Either: |
| 76 | +- ignore a rule in the affected code, by adding `phpcs:ignore {rule}`, where {rule} is the given rule that failed in the above output. |
| 77 | +- edit the [phpcs.tests.xml](phpcs.tests.xml) file. |
| 78 | + |
| 79 | +**Rules can be ignored with caution**, but it's essential that rules relating to coding style and inline code commenting / docblocks remain. |
| 80 | + |
| 81 | +## Next Steps |
| 82 | + |
| 83 | +Once your tests are written and successfully run locally, submit your branch via a new [Pull Request](https://github.com/ConvertKit/ConvertKitSDK-PHP/compare). |
| 84 | + |
| 85 | +It's best to create a Pull Request in draft mode, as this will trigger all tests to run as a GitHub Action, allowing you to double check all tests pass. |
| 86 | + |
| 87 | +If the PR tests fail, you can make code changes as necessary, pushing to the same branch. This will trigger the tests to run again. |
| 88 | + |
| 89 | +If the PR tests pass, you can publish the PR, assigning some reviewers. |
0 commit comments