Skip to content

Commit c1b61a2

Browse files
authored
Merge pull request #30 from ConvertKit/fix-tests
Fix tests
2 parents ce53870 + c971157 commit c1b61a2

15 files changed

+1397
-2873
lines changed

.env.dist.testing

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CONVERTKIT_API_FORM_ID="2765139"
2+
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
3+
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
4+
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
5+
CONVERTKIT_API_SEQUENCE_ID="1030824"
6+
CONVERTKIT_API_TAG_ID="2744672"
7+
CONVERTKIT_API_SUBSCRIBER_EMAIL="[email protected]"
8+
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CONVERTKIT_API_KEY_NO_DATA=
2+
CONVERTKIT_API_SECRET_NO_DATA=
3+
CONVERTKIT_API_KEY=
4+
CONVERTKIT_API_SECRET=
5+
CONVERTKIT_API_FORM_ID="2765139"
6+
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
7+
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
8+
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
9+
CONVERTKIT_API_SEQUENCE_ID="1030824"
10+
CONVERTKIT_API_TAG_ID="2744672"
11+
CONVERTKIT_API_SUBSCRIBER_EMAIL="[email protected]"
12+
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"

.github/PULL_REQUEST_TEMPLATE.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Summary
2+
3+
A brief description of what this PR adds, changes or fixes.
4+
5+
If it is adding functionality, a use case of why this is needed helps determine its suitability.
6+
7+
## Testing
8+
9+
Not sure how to perform testing, or perhaps didn't include a test in this PR? Walk through the following in order for a beginner-friendly guide:
10+
- [Setup](SETUP.md) - setting up your local environment for development and testing
11+
- [Development](DEVELOPMENT.md) - best practices for development
12+
- [Testing](TESTING.md) - how to write and run tests
13+
14+
## Checklist
15+
16+
* [ ] I have [written a test](TESTING.md#writing-a-test) and included it in this PR
17+
* [ ] I have [run all tests](TESTING.md#run-tests) and they pass
18+
* [ ] The code passes when [running the PHP CodeSniffer](TESTING.md#run-php-codesniffer)
19+
* [ ] The code passes when [running PHPStan](TESTING.md#run-phpstan)
20+
* [ ] I have assigned a reviewer or two to review this PR (if you're not sure who to assign, we can do this step for you)

.github/workflows/tests.yml

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
name: Run Tests
2+
3+
# When to run tests.
4+
on:
5+
pull_request:
6+
types:
7+
- opened
8+
- synchronize
9+
push:
10+
branches:
11+
- main
12+
13+
jobs:
14+
tests:
15+
# Name.
16+
name: PHP ${{ matrix.php-versions }}
17+
18+
# Virtual Environment to use.
19+
# @see: https://github.com/actions/virtual-environments
20+
runs-on: ubuntu-latest
21+
22+
# Defines PHP Versions matrix to run tests on
23+
strategy:
24+
matrix:
25+
php-versions: [ '7.4', '8.0', '8.1', '8.2' ]
26+
27+
# Steps to install, configure and run tests
28+
steps:
29+
# Checkout (copy) this repository's Plugin to this VM.
30+
- name: Checkout Code
31+
uses: actions/checkout@v3
32+
33+
# Install PHP version
34+
- name: Install PHP
35+
uses: shivammathur/setup-php@v2
36+
with:
37+
php-version: ${{ matrix.php-versions }}
38+
coverage: none
39+
40+
# Write any secrets, such as API keys, to the .env.dist.testing file now.
41+
# Make sure your committed .env.dist.testing file ends with a newline.
42+
# The formatting of the contents to include a blank newline is deliberate.
43+
- name: Define GitHub Secrets in .env.dist.testing
44+
uses: DamianReeves/[email protected]
45+
with:
46+
path: .env.dist.testing
47+
contents: |
48+
49+
CONVERTKIT_API_KEY=${{ secrets.CONVERTKIT_API_KEY }}
50+
CONVERTKIT_API_SECRET=${{ secrets.CONVERTKIT_API_SECRET }}
51+
CONVERTKIT_API_KEY_NO_DATA=${{ secrets.CONVERTKIT_API_KEY_NO_DATA }}
52+
CONVERTKIT_API_SECRET_NO_DATA=${{ secrets.CONVERTKIT_API_SECRET_NO_DATA }}
53+
write-mode: append
54+
55+
# Rename .env.dist.testing to .env, so PHPUnit reads it for tests.
56+
- name: Rename .env.dist.testing to .env
57+
run: mv .env.dist.testing .env
58+
59+
# Installs PHPUnit, PHP CodeSniffer and anything else needed to run tests.
60+
- name: Run Composer
61+
run: composer update
62+
63+
# Generate autoloader
64+
- name: Build PHP Autoloader
65+
run: composer dump-autoload
66+
67+
# Run Coding Standards.
68+
- name: Run Coding Standards
69+
run: php vendor/bin/phpcs --standard=phpcs.xml
70+
71+
# Run Coding Standards on Tests.
72+
- name: Run Coding Standards on Tests
73+
run: php vendor/bin/phpcs --standard=phpcs.tests.xml
74+
75+
# Run PHPUnit Tests.
76+
- name: Run PHPUnit Tests
77+
run: vendor/bin/phpunit --verbose --stop-on-failure

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
.DS_Store
22
.env
33
.env.testing
4+
.phpunit.result.cache
45
composer.lock
56
phpstan.neon
67
src/logs

TESTING.md

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
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.

composer.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,15 @@
2020
"monolog/monolog": "^2.0"
2121
},
2222
"require-dev": {
23+
"vlucas/phpdotenv": "^5.5",
2324
"phpunit/phpunit": "^5.7 || ^9.0",
2425
"squizlabs/php_codesniffer": "^3.3",
2526
"phpstan/phpstan": "^1.2"
2627
},
2728
"autoload": {
2829
"psr-4": {
2930
"ConvertKit_API\\": "src/"
30-
},
31-
"classmap": [
32-
"src/lib/"
33-
]
31+
}
3432
},
3533
"minimum-stability": "dev",
3634
"prefer-stable": true

phpcs.tests.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>Coding Standards for Tests</description>
4+
5+
<!-- Inspect files in the /tests folder -->
6+
<file>tests</file>
7+
8+
<!-- Run in verbose mode and specify the precise rule that failed in output -->
9+
<arg value="sv"/>
10+
<arg name="colors"/>
11+
12+
<!-- Use PSR-12 -->
13+
<rule ref="PSR12">
14+
<!-- Exclude "Each class must be in a namespace of at least one level (a top-level vendor name)" -->
15+
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
16+
</rule>
17+
</ruleset>

phpcs.xml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0"?>
2+
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
3+
<description>Coding Standards</description>
4+
5+
<!-- Inspect files in the /src folder -->
6+
<file>src</file>
7+
8+
<!-- Run in verbose mode and specify the precise rule that failed in output -->
9+
<arg value="sv"/>
10+
<arg name="colors"/>
11+
12+
<!-- Use PSR-12 -->
13+
<rule ref="PSR12">
14+
<!-- Exclude function not in camel caps format, to avoid breaking changes -->
15+
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />
16+
17+
<!-- Don't require spacing after header block -->
18+
<exclude name="PSR12.Files.FileHeader.SpacingAfterBlock" />
19+
20+
<!-- Permit else if over elseif -->
21+
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed" />
22+
</rule>
23+
24+
<!-- Use Squiz -->
25+
<rule ref="Squiz">
26+
<!-- Exclude "Class found in ".php" file; use ".inc" extension instead" -->
27+
<exclude name="Squiz.Files.FileExtension.ClassFound" />
28+
29+
<!-- Exclude PascalCase format for class name, to avoid breaking changes -->
30+
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />
31+
32+
<!-- Exclude variable naming, to avoid breaking changes -->
33+
<exclude name="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps" />
34+
<exclude name="Squiz.NamingConventions.ValidVariableName.NotCamelCaps" />
35+
<exclude name="Squiz.NamingConventions.ValidFunctionName.ScopeNotCamelCaps" />
36+
37+
<!-- Allow implicit true and false comparisons -->
38+
<exclude name="Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue" />
39+
<exclude name="Squiz.Operators.ComparisonOperatorUsage.NotAllowed" />
40+
41+
<!-- Don't require // end comments after each function, or Author / Copyright tags -->
42+
<exclude name="Squiz.Commenting.ClosingDeclarationComment.Missing" />
43+
<exclude name="Squiz.Commenting.FileComment.IncorrectAuthor" />
44+
<exclude name="Squiz.Commenting.FileComment.MissingCopyrightTag" />
45+
46+
<!-- Permit inline if statements -->
47+
<exclude name="Squiz.PHP.DisallowInlineIf.Found" />
48+
49+
<!-- Don't require various newlines and spacing before and after functions -->
50+
<exclude name="Squiz.WhiteSpace.FunctionSpacing.AfterLast" />
51+
<exclude name="Squiz.WhiteSpace.FunctionSpacing.After" />
52+
<exclude name="Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose" />
53+
<exclude name="Squiz.WhiteSpace.MemberVarSpacing.FirstIncorrect" />
54+
55+
<!-- Permit padding surrounding a concat operator -->
56+
<exclude name="Squiz.Strings.ConcatenationSpacing.PaddingFound" />
57+
</rule>
58+
</ruleset>

phpunit.xml

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="./vendor/autoload.php"
3-
color="true"
4-
convertErrorsToExceptions="true"
5-
convertNoticesToExceptions="true"
6-
convertWarningsToExceptions="true"
7-
stopOnFailures="false"
8-
syntaxCheck="false"
9-
beStrictAboutTestsThatDoNotTestAnything="false"
10-
>
2+
<phpunit bootstrap="./vendor/autoload.php" colors="true">
113
<testsuites>
124
<testsuite name="ConvertKit API Tests">
135
<directory>tests</directory>

0 commit comments

Comments
 (0)