Skip to content

Commit

Permalink
Merge pull request #96 from deliciousbrains/add-unit-tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
ianmjones authored Apr 14, 2023
2 parents cc7b24c + 3df7ecd commit c6168c3
Show file tree
Hide file tree
Showing 13 changed files with 2,617 additions and 5 deletions.
93 changes: 93 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
workflows:
version: 2
main:
jobs:
- php72-build
- php73-build
- php74-build
- php80-build

version: 2

job-references:
mysql_image: &mysql_image
cimg/mysql:5.7

setup_environment: &setup_environment
name: "Setup Environment Variables"
command: |
echo "export PATH=$HOME/.composer/vendor/bin:$PATH" >> $BASH_ENV
source /home/circleci/.bashrc
install_dependencies: &install_dependencies
name: "Install Dependencies"
command: |
sudo apt-get update && sudo apt-get install mysql-client subversion
php_job: &php_job
environment:
- WP_TESTS_DIR: "/tmp/wordpress-tests-lib"
- WP_CORE_DIR: "/tmp/wordpress/"
steps:
- checkout
- run: php --version
- run: composer --version
- run: *setup_environment
- run: *install_dependencies
- run:
name: "Run Tests"
command: |
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
make test-unit
WP_MULTISITE=1 make test-unit
make test-style
jobs:
php56-build:
<<: *php_job
docker:
- image: cimg/php:5.6
- image: *mysql_image

php70-build:
<<: *php_job
docker:
- image: cimg/php:7.0
- image: *mysql_image

php71-build:
<<: *php_job
docker:
- image: cimg/php:7.1
- image: *mysql_image

php72-build:
<<: *php_job
docker:
- image: cimg/php:7.2
- image: *mysql_image

php73-build:
<<: *php_job
docker:
- image: cimg/php:7.3
- image: *mysql_image

php74-build:
<<: *php_job
docker:
- image: cimg/php:7.4
- image: *mysql_image

php80-build:
<<: *php_job
docker:
- image: cimg/php:8.0
- image: *mysql_image

php81-build:
<<: *php_job
docker:
- image: cimg/php:8.1
- image: *mysql_image
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/vendor/
/.idea
/.idea
*.cache
73 changes: 73 additions & 0 deletions .phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<?xml version="1.0"?>
<ruleset name="WordPress Coding Standards based custom ruleset for your plugin">
<description>Generally-applicable sniffs for WordPress plugins.</description>

<!-- What to scan -->
<file>.</file>
<exclude-pattern>/vendor/</exclude-pattern>
<exclude-pattern>/node_modules/</exclude-pattern>
<exclude-pattern>/tests/*</exclude-pattern>

<!-- How to scan -->
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
<arg value="sp"/> <!-- Show sniff and progress -->
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
<arg name="colors"/>
<arg name="extensions" value="php"/>
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->

<!-- Rules: Check PHP version compatibility -->
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
<config name="testVersion" value="7.2-"/>
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
<rule ref="PHPCompatibilityWP"/>

<!-- Rules: WordPress Coding Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
<config name="minimum_supported_wp_version" value="4.6"/>
<rule ref="WordPress">
<!-- Unable to fix -->
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound"/>
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound"/>
<exclude name="WordPress.DB.DirectDatabaseQuery.NoCaching"/>

<!-- TODO: Maybe fix -->
<exclude name="WordPress.WP.CronInterval.ChangeDetected"/>
<exclude name="WordPress.WP.AlternativeFunctions.rand_rand"/>
<exclude name="WordPress.DB.DirectDatabaseQuery.DirectQuery"/>

<!-- TODO: Should fix -->
<exclude name="WordPress.DB.PreparedSQL.InterpolatedNotPrepared"/>
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
<exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments"/>
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
<exclude name="Generic.Commenting.DocComment.SpacingAfter"/>
</rule>
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
<properties>
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
<property name="prefixes" type="array" value="wpbp"/>
</properties>

<!-- TODO: Maybe fix -->
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound"/>
</rule>
<rule ref="WordPress.WP.I18n">
<properties>
<!-- Value: replace the text domain used. -->
<property name="text_domain" type="array" value="wp-background-processing"/>
</properties>

<!-- TODO: Should fix -->
<exclude name="WordPress.WP.I18n.MissingArgDomain"/>
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment"/>
</rule>
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
<properties>
<property name="blank_line_check" value="true"/>
</properties>
</rule>
</ruleset>
18 changes: 18 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
.PHONY: test
test: test-unit test-style

.PHONY: test-unit
test-unit: vendor
vendor/bin/phpunit

.PHONY: test-style
test-style: vendor
vendor/bin/phpcs

vendor: composer.json
composer install --ignore-platform-reqs

.PHONY: clean
clean:
rm -rf vendor
rm -f tests/.phpunit.result.cache
72 changes: 71 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ WP Background Processing can be used to fire off non-blocking asynchronous reque

Inspired by [TechCrunch WP Asynchronous Tasks](https://github.com/techcrunch/wp-async-task).

__Requires PHP 5.2+__
__Requires PHP 5.6+__

## Install

Expand Down Expand Up @@ -167,6 +167,76 @@ function wpbp_http_request_args( $r, $url ) {
add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2);
```

## Contributing

Contributions are welcome via Pull Requests, but please do raise an issue before
working on anything to discuss the change if there isn't already an issue. If there
is an approved issue you'd like to tackle, please post a comment on it to let people know
you're going to have a go at it so that effort isn't wasted through duplicated work.

### Unit & Style Tests

When working on the library, please add unit tests to the appropriate file in the
`tests` directory that cover your changes.

#### Setting Up

We use the standard WordPress test libraries for running unit tests.

Please run the following command to set up the libraries:

```shell
bin/install-wp-tests.sh db_name db_user db_pass
```

Substitute `db_name`, `db_user` and `db_pass` as appropriate.

Please be aware that running the unit tests is a **destructive operation**, *database
tables will be cleared*, so please use a database name dedicated to running unit tests.
The standard database name usually used by the WordPress community is `wordpress_test`, e.g.

```shell
bin/install-wp-tests.sh wordpress_test root root
```

Please refer to the [Initialize the testing environment locally](https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/#3-initialize-the-testing-environment-locally)
section of the WordPress Handbook's [Plugin Integration Tests](https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/)
entry should you run into any issues.

#### Running Unit Tests

To run the unit tests, simply run:

```shell
make test-unit
```

If the `composer` dependencies aren't in place, they'll be automatically installed first.

#### Running Style Tests

It's important that the code in the library use a consistent style to aid in quickly
understanding it, and to avoid some common issues. `PHP_Code_Sniffer` is used with
mostly standard WordPress rules to help check for consistency.

To run the style tests, simply run:

```shell
make test-style
```

If the `composer` dependencies aren't in place, they'll be automatically installed first.

#### Running All Tests

To make things super simple, just run the following to run all tests:

```shell
make
```

If the `composer` dependencies aren't in place, they'll be automatically installed first.

## License

[GPLv2+](http://www.gnu.org/licenses/gpl-2.0.html)
Loading

0 comments on commit c6168c3

Please sign in to comment.