Skip to content

Commit c6168c3

Browse files
authored
Merge pull request #96 from deliciousbrains/add-unit-tests
Add unit tests
2 parents cc7b24c + 3df7ecd commit c6168c3

File tree

13 files changed

+2617
-5
lines changed

13 files changed

+2617
-5
lines changed

.circleci/config.yml

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
workflows:
2+
version: 2
3+
main:
4+
jobs:
5+
- php72-build
6+
- php73-build
7+
- php74-build
8+
- php80-build
9+
10+
version: 2
11+
12+
job-references:
13+
mysql_image: &mysql_image
14+
cimg/mysql:5.7
15+
16+
setup_environment: &setup_environment
17+
name: "Setup Environment Variables"
18+
command: |
19+
echo "export PATH=$HOME/.composer/vendor/bin:$PATH" >> $BASH_ENV
20+
source /home/circleci/.bashrc
21+
22+
install_dependencies: &install_dependencies
23+
name: "Install Dependencies"
24+
command: |
25+
sudo apt-get update && sudo apt-get install mysql-client subversion
26+
27+
php_job: &php_job
28+
environment:
29+
- WP_TESTS_DIR: "/tmp/wordpress-tests-lib"
30+
- WP_CORE_DIR: "/tmp/wordpress/"
31+
steps:
32+
- checkout
33+
- run: php --version
34+
- run: composer --version
35+
- run: *setup_environment
36+
- run: *install_dependencies
37+
- run:
38+
name: "Run Tests"
39+
command: |
40+
rm -rf $WP_TESTS_DIR $WP_CORE_DIR
41+
bash bin/install-wp-tests.sh wordpress_test root '' 127.0.0.1 latest
42+
make test-unit
43+
WP_MULTISITE=1 make test-unit
44+
make test-style
45+
46+
jobs:
47+
php56-build:
48+
<<: *php_job
49+
docker:
50+
- image: cimg/php:5.6
51+
- image: *mysql_image
52+
53+
php70-build:
54+
<<: *php_job
55+
docker:
56+
- image: cimg/php:7.0
57+
- image: *mysql_image
58+
59+
php71-build:
60+
<<: *php_job
61+
docker:
62+
- image: cimg/php:7.1
63+
- image: *mysql_image
64+
65+
php72-build:
66+
<<: *php_job
67+
docker:
68+
- image: cimg/php:7.2
69+
- image: *mysql_image
70+
71+
php73-build:
72+
<<: *php_job
73+
docker:
74+
- image: cimg/php:7.3
75+
- image: *mysql_image
76+
77+
php74-build:
78+
<<: *php_job
79+
docker:
80+
- image: cimg/php:7.4
81+
- image: *mysql_image
82+
83+
php80-build:
84+
<<: *php_job
85+
docker:
86+
- image: cimg/php:8.0
87+
- image: *mysql_image
88+
89+
php81-build:
90+
<<: *php_job
91+
docker:
92+
- image: cimg/php:8.1
93+
- image: *mysql_image

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/vendor/
2-
/.idea
2+
/.idea
3+
*.cache

.phpcs.xml

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?xml version="1.0"?>
2+
<ruleset name="WordPress Coding Standards based custom ruleset for your plugin">
3+
<description>Generally-applicable sniffs for WordPress plugins.</description>
4+
5+
<!-- What to scan -->
6+
<file>.</file>
7+
<exclude-pattern>/vendor/</exclude-pattern>
8+
<exclude-pattern>/node_modules/</exclude-pattern>
9+
<exclude-pattern>/tests/*</exclude-pattern>
10+
11+
<!-- How to scan -->
12+
<!-- Usage instructions: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Usage -->
13+
<!-- Annotated ruleset: https://github.com/squizlabs/PHP_CodeSniffer/wiki/Annotated-ruleset.xml -->
14+
<arg value="sp"/> <!-- Show sniff and progress -->
15+
<arg name="basepath" value="./"/><!-- Strip the file paths down to the relevant bit -->
16+
<arg name="colors"/>
17+
<arg name="extensions" value="php"/>
18+
<arg name="parallel" value="8"/><!-- Enables parallel processing when available for faster results. -->
19+
20+
<!-- Rules: Check PHP version compatibility -->
21+
<!-- https://github.com/PHPCompatibility/PHPCompatibility#sniffing-your-code-for-compatibility-with-specific-php-versions -->
22+
<config name="testVersion" value="7.2-"/>
23+
<!-- https://github.com/PHPCompatibility/PHPCompatibilityWP -->
24+
<rule ref="PHPCompatibilityWP"/>
25+
26+
<!-- Rules: WordPress Coding Standards -->
27+
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards -->
28+
<!-- https://github.com/WordPress-Coding-Standards/WordPress-Coding-Standards/wiki/Customizable-sniff-properties -->
29+
<config name="minimum_supported_wp_version" value="4.6"/>
30+
<rule ref="WordPress">
31+
<!-- Unable to fix -->
32+
<exclude name="WordPress.Files.FileName.InvalidClassFileName"/>
33+
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedClassFound"/>
34+
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound"/>
35+
<exclude name="WordPress.DB.DirectDatabaseQuery.NoCaching"/>
36+
37+
<!-- TODO: Maybe fix -->
38+
<exclude name="WordPress.WP.CronInterval.ChangeDetected"/>
39+
<exclude name="WordPress.WP.AlternativeFunctions.rand_rand"/>
40+
<exclude name="WordPress.DB.DirectDatabaseQuery.DirectQuery"/>
41+
42+
<!-- TODO: Should fix -->
43+
<exclude name="WordPress.DB.PreparedSQL.InterpolatedNotPrepared"/>
44+
<exclude name="PEAR.Functions.FunctionCallSignature.ContentAfterOpenBracket"/>
45+
<exclude name="PEAR.Functions.FunctionCallSignature.MultipleArguments"/>
46+
<exclude name="PEAR.Functions.FunctionCallSignature.CloseBracketLine"/>
47+
<exclude name="Generic.Commenting.DocComment.SpacingAfter"/>
48+
</rule>
49+
<rule ref="WordPress.NamingConventions.PrefixAllGlobals">
50+
<properties>
51+
<!-- Value: replace the function, class, and variable prefixes used. Separate multiple prefixes with a comma. -->
52+
<property name="prefixes" type="array" value="wpbp"/>
53+
</properties>
54+
55+
<!-- TODO: Maybe fix -->
56+
<exclude name="WordPress.NamingConventions.PrefixAllGlobals.DynamicHooknameFound"/>
57+
</rule>
58+
<rule ref="WordPress.WP.I18n">
59+
<properties>
60+
<!-- Value: replace the text domain used. -->
61+
<property name="text_domain" type="array" value="wp-background-processing"/>
62+
</properties>
63+
64+
<!-- TODO: Should fix -->
65+
<exclude name="WordPress.WP.I18n.MissingArgDomain"/>
66+
<exclude name="WordPress.WP.I18n.MissingTranslatorsComment"/>
67+
</rule>
68+
<rule ref="WordPress.WhiteSpace.ControlStructureSpacing">
69+
<properties>
70+
<property name="blank_line_check" value="true"/>
71+
</properties>
72+
</rule>
73+
</ruleset>

Makefile

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
.PHONY: test
2+
test: test-unit test-style
3+
4+
.PHONY: test-unit
5+
test-unit: vendor
6+
vendor/bin/phpunit
7+
8+
.PHONY: test-style
9+
test-style: vendor
10+
vendor/bin/phpcs
11+
12+
vendor: composer.json
13+
composer install --ignore-platform-reqs
14+
15+
.PHONY: clean
16+
clean:
17+
rm -rf vendor
18+
rm -f tests/.phpunit.result.cache

README.md

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ WP Background Processing can be used to fire off non-blocking asynchronous reque
44

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

7-
__Requires PHP 5.2+__
7+
__Requires PHP 5.6+__
88

99
## Install
1010

@@ -167,6 +167,76 @@ function wpbp_http_request_args( $r, $url ) {
167167
add_filter( 'http_request_args', 'wpbp_http_request_args', 10, 2);
168168
```
169169

170+
## Contributing
171+
172+
Contributions are welcome via Pull Requests, but please do raise an issue before
173+
working on anything to discuss the change if there isn't already an issue. If there
174+
is an approved issue you'd like to tackle, please post a comment on it to let people know
175+
you're going to have a go at it so that effort isn't wasted through duplicated work.
176+
177+
### Unit & Style Tests
178+
179+
When working on the library, please add unit tests to the appropriate file in the
180+
`tests` directory that cover your changes.
181+
182+
#### Setting Up
183+
184+
We use the standard WordPress test libraries for running unit tests.
185+
186+
Please run the following command to set up the libraries:
187+
188+
```shell
189+
bin/install-wp-tests.sh db_name db_user db_pass
190+
```
191+
192+
Substitute `db_name`, `db_user` and `db_pass` as appropriate.
193+
194+
Please be aware that running the unit tests is a **destructive operation**, *database
195+
tables will be cleared*, so please use a database name dedicated to running unit tests.
196+
The standard database name usually used by the WordPress community is `wordpress_test`, e.g.
197+
198+
```shell
199+
bin/install-wp-tests.sh wordpress_test root root
200+
```
201+
202+
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)
203+
section of the WordPress Handbook's [Plugin Integration Tests](https://make.wordpress.org/cli/handbook/misc/plugin-unit-tests/)
204+
entry should you run into any issues.
205+
206+
#### Running Unit Tests
207+
208+
To run the unit tests, simply run:
209+
210+
```shell
211+
make test-unit
212+
```
213+
214+
If the `composer` dependencies aren't in place, they'll be automatically installed first.
215+
216+
#### Running Style Tests
217+
218+
It's important that the code in the library use a consistent style to aid in quickly
219+
understanding it, and to avoid some common issues. `PHP_Code_Sniffer` is used with
220+
mostly standard WordPress rules to help check for consistency.
221+
222+
To run the style tests, simply run:
223+
224+
```shell
225+
make test-style
226+
```
227+
228+
If the `composer` dependencies aren't in place, they'll be automatically installed first.
229+
230+
#### Running All Tests
231+
232+
To make things super simple, just run the following to run all tests:
233+
234+
```shell
235+
make
236+
```
237+
238+
If the `composer` dependencies aren't in place, they'll be automatically installed first.
239+
170240
## License
171241

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

0 commit comments

Comments
 (0)