Skip to content

Fix tests #30

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 40 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1253f9d
Started work on testing and pull request guides
n7studios Mar 1, 2023
6a6a078
Ignore .phpunit.result.cache
n7studios Mar 1, 2023
b9cf8a7
Fix incorrect PHPUnit configuration
n7studios Mar 1, 2023
d092c79
Make tests compatible with PHPUnit 9.x
n7studios Mar 1, 2023
7df4587
Use .env file for configuration, replacing tests/config.example.php
n7studios Mar 1, 2023
556798a
Merge remote-tracking branch 'origin/1.0-beta' into fix-tests
n7studios Mar 2, 2023
16f51f9
Started work on refactoring and improving test coverage
n7studios Mar 2, 2023
b678865
More work on improving test coverage
n7studios Mar 2, 2023
306b6ec
Completed first pass of improved test coverage
n7studios Mar 2, 2023
134772f
Updated .env files
n7studios Mar 2, 2023
ed0ebd9
Started applying coding standards to tests
n7studios Mar 2, 2023
fa34ce8
Just use PSR-12 for coding standards for now
n7studios Mar 2, 2023
d77b521
First pass at GitHub action to automate tests
n7studios Mar 2, 2023
878634d
Update testing guide
n7studios Mar 2, 2023
9ce7380
Query subscibers by email address, instead of using inefficient pagin…
n7studios Mar 3, 2023
ef52956
Implement code from ConvertKit API library for fetching legacy forms …
n7studios Mar 3, 2023
f9445fd
Remove unused depdencies
n7studios Mar 3, 2023
0b7f052
Remove unused .env variables
n7studios Mar 3, 2023
2317d4c
Improved test coverage for get_resource()
n7studios Mar 3, 2023
eab7d04
Add missing CONVERTKIT_API_SEQUENCE_ID in .env files
n7studios Mar 3, 2023
b4e79cc
Apply PSR-12 coding standards to API class
n7studios Mar 3, 2023
e197ec8
Remove blank lines after opening function brace
n7studios Mar 3, 2023
9dc57ff
Updated coding standard rules to favor PSR-12, and import/honor Squiz…
n7studios Mar 3, 2023
2664982
Fix TypeError on type hinting DOMNodeList
n7studios Mar 3, 2023
4cda118
Merge pull request #33 from ConvertKit/coding-standards
n7studios Mar 6, 2023
366a9e7
Merge pull request #32 from ConvertKit/fix-get-resource
n7studios Mar 6, 2023
78262b4
Merge pull request #31 from ConvertKit/fix-get-subscriber-id-performance
n7studios Mar 6, 2023
bc4dfaf
Assert less / greater than or equal to, as two subscribers might be s…
n7studios Mar 6, 2023
3918ecd
Updated testing docs to include using PHP CodeSniffer on code
n7studios Mar 6, 2023
ea8699c
Fix coding standards on tests
n7studios Mar 6, 2023
4aff3b7
Fix typo on tests
n7studios Mar 6, 2023
69523a4
Adds get, post, put and delete function calls
n7studios Mar 6, 2023
2db290b
Move repetitive `api_version` property into `make_request()`
n7studios Mar 6, 2023
c7f51f3
Replace check_if_subscriber_in_array()
n7studios Mar 6, 2023
cfd5a86
Merge remote-tracking branch 'origin/fix-sort-order-tests' into remov…
n7studios Mar 6, 2023
7d7676b
Remove leading forwardslash on endpoints
n7studios Mar 6, 2023
be25cfe
Merge pull request #37 from ConvertKit/move-api-version-into-make-req…
n7studios Mar 6, 2023
e00000c
Merge pull request #39 from ConvertKit/remove-subscriber-array-check-…
n7studios Mar 7, 2023
2619b1b
Merge pull request #36 from ConvertKit/add-http-verb-functions
n7studios Mar 7, 2023
c971157
Merge pull request #35 from ConvertKit/fix-sort-order-tests
n7studios Mar 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .env.dist.testing
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
CONVERTKIT_API_SEQUENCE_ID="1030824"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_SUBSCRIBER_EMAIL="[email protected]"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
12 changes: 12 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
CONVERTKIT_API_KEY_NO_DATA=
CONVERTKIT_API_SECRET_NO_DATA=
CONVERTKIT_API_KEY=
CONVERTKIT_API_SECRET=
CONVERTKIT_API_FORM_ID="2765139"
CONVERTKIT_API_LEGACY_FORM_URL="https://app.convertkit.com/landing_pages/470099"
CONVERTKIT_API_LANDING_PAGE_URL="https://cheerful-architect-3237.ck.page/cc5eb21744"
CONVERTKIT_API_LEGACY_LANDING_PAGE_URL="https://app.convertkit.com/landing_pages/470103"
CONVERTKIT_API_SEQUENCE_ID="1030824"
CONVERTKIT_API_TAG_ID="2744672"
CONVERTKIT_API_SUBSCRIBER_EMAIL="[email protected]"
CONVERTKIT_API_SUBSCRIBER_ID="1579118532"
20 changes: 20 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
## Summary

A brief description of what this PR adds, changes or fixes.

If it is adding functionality, a use case of why this is needed helps determine its suitability.

## Testing

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:
- [Setup](SETUP.md) - setting up your local environment for development and testing
- [Development](DEVELOPMENT.md) - best practices for development
- [Testing](TESTING.md) - how to write and run tests

## Checklist

* [ ] I have [written a test](TESTING.md#writing-a-test) and included it in this PR
* [ ] I have [run all tests](TESTING.md#run-tests) and they pass
* [ ] The code passes when [running the PHP CodeSniffer](TESTING.md#run-php-codesniffer)
* [ ] The code passes when [running PHPStan](TESTING.md#run-phpstan)
* [ ] 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)
77 changes: 77 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
name: Run Tests

# When to run tests.
on:
pull_request:
types:
- opened
- synchronize
push:
branches:
- main

jobs:
tests:
# Name.
name: PHP ${{ matrix.php-versions }}

# Virtual Environment to use.
# @see: https://github.com/actions/virtual-environments
runs-on: ubuntu-latest

# Defines PHP Versions matrix to run tests on
strategy:
matrix:
php-versions: [ '7.4', '8.0', '8.1', '8.2' ]

# Steps to install, configure and run tests
steps:
# Checkout (copy) this repository's Plugin to this VM.
- name: Checkout Code
uses: actions/checkout@v3

# Install PHP version
- name: Install PHP
uses: shivammathur/setup-php@v2
with:
php-version: ${{ matrix.php-versions }}
coverage: none

# Write any secrets, such as API keys, to the .env.dist.testing file now.
# Make sure your committed .env.dist.testing file ends with a newline.
# The formatting of the contents to include a blank newline is deliberate.
- name: Define GitHub Secrets in .env.dist.testing
uses: DamianReeves/[email protected]
with:
path: .env.dist.testing
contents: |

CONVERTKIT_API_KEY=${{ secrets.CONVERTKIT_API_KEY }}
CONVERTKIT_API_SECRET=${{ secrets.CONVERTKIT_API_SECRET }}
CONVERTKIT_API_KEY_NO_DATA=${{ secrets.CONVERTKIT_API_KEY_NO_DATA }}
CONVERTKIT_API_SECRET_NO_DATA=${{ secrets.CONVERTKIT_API_SECRET_NO_DATA }}
write-mode: append

# Rename .env.dist.testing to .env, so PHPUnit reads it for tests.
- name: Rename .env.dist.testing to .env
run: mv .env.dist.testing .env

# Installs PHPUnit, PHP CodeSniffer and anything else needed to run tests.
- name: Run Composer
run: composer update

# Generate autoloader
- name: Build PHP Autoloader
run: composer dump-autoload

# Run Coding Standards.
- name: Run Coding Standards
run: php vendor/bin/phpcs --standard=phpcs.xml

# Run Coding Standards on Tests.
- name: Run Coding Standards on Tests
run: php vendor/bin/phpcs --standard=phpcs.tests.xml

# Run PHPUnit Tests.
- name: Run PHPUnit Tests
run: vendor/bin/phpunit --verbose --stop-on-failure
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
.DS_Store
.env
.env.testing
.phpunit.result.cache
composer.lock
phpstan.neon
src/logs
Expand Down
89 changes: 89 additions & 0 deletions TESTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
# Testing Guide

This document describes how to:
- create and run tests for your development work,
- ensure code meets coding standards, for best practices and security,

If you're new to creating and running tests, this guide will walk you through how to do this.

For those more experienced with creating and running tests, our tests are written in PHP using [PHPUnit](https://phpunit.de/).

## Prerequisites

If you haven't yet set up your local development environment, refer to the [Setup Guide](SETUP.md).

If you haven't yet created a branch and made any code changes, refer to the [Development Guide](DEVELOPMENT.md)

## Write (or modify) a test

If your work creates new functionality, write a test.

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.

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.

## Run PHPUnit

Once you have written your code and tests, run the tests to make sure there are no errors.

To run the tests, enter the following commands in a separate Terminal window:

```bash
vendor/bin/phpunit --verbose --stop-on-failure
```

If a test fails, you can inspect the output.

Any errors should be corrected by making applicable code or test changes.

## Run PHP CodeSniffer

[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/).

To run CodeSniffer on tests, enter the following command:

```bash
vendor/bin/phpcs
```

Any errors should be corrected by either:
- making applicable code changes
- (Experimental) running `vendor/bin/phpcbf` to automatically fix coding standards

Need to change the coding standard rules applied? Either:
- ignore a rule in the affected code, by adding `phpcs:ignore {rule}`, where {rule} is the given rule that failed in the above output.
- edit the [phpcs.tests.xml](phpcs.xml) file.

**Rules can be ignored with caution**, but it's essential that rules relating to coding style and inline code commenting / docblocks remain.

## Run PHP CodeSniffer for Tests

[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/).

To run CodeSniffer on tests, enter the following command:

```bash
vendor/bin/phpcs --standard=phpcs.tests.xml
```

`--standard=phpcs.tests.xml` tells PHP CodeSniffer to use the use the [phpcs.tests.xml](phpcs.tests.xml) configuration file

Any errors should be corrected by either:
- making applicable code changes
- (Experimental) running `vendor/bin/phpcbf --standard=phpcs.tests.xml` to automatically fix coding standards

Need to change the coding standard rules applied? Either:
- ignore a rule in the affected code, by adding `phpcs:ignore {rule}`, where {rule} is the given rule that failed in the above output.
- edit the [phpcs.tests.xml](phpcs.tests.xml) file.

**Rules can be ignored with caution**, but it's essential that rules relating to coding style and inline code commenting / docblocks remain.

## Next Steps

Once your tests are written and successfully run locally, submit your branch via a new [Pull Request](https://github.com/ConvertKit/ConvertKitSDK-PHP/compare).

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.

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.

If the PR tests pass, you can publish the PR, assigning some reviewers.
6 changes: 2 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,15 @@
"monolog/monolog": "^2.0"
},
"require-dev": {
"vlucas/phpdotenv": "^5.5",
"phpunit/phpunit": "^5.7 || ^9.0",
"squizlabs/php_codesniffer": "^3.3",
"phpstan/phpstan": "^1.2"
},
"autoload": {
"psr-4": {
"ConvertKit_API\\": "src/"
},
"classmap": [
"src/lib/"
]
}
},
"minimum-stability": "dev",
"prefer-stable": true
Expand Down
17 changes: 17 additions & 0 deletions phpcs.tests.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>Coding Standards for Tests</description>

<!-- Inspect files in the /tests folder -->
<file>tests</file>

<!-- Run in verbose mode and specify the precise rule that failed in output -->
<arg value="sv"/>
<arg name="colors"/>

<!-- Use PSR-12 -->
<rule ref="PSR12">
<!-- Exclude "Each class must be in a namespace of at least one level (a top-level vendor name)" -->
<exclude name="PSR1.Classes.ClassDeclaration.MissingNamespace" />
</rule>
</ruleset>
58 changes: 58 additions & 0 deletions phpcs.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?xml version="1.0"?>
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="PHP_CodeSniffer" xsi:noNamespaceSchemaLocation="phpcs.xsd">
<description>Coding Standards</description>

<!-- Inspect files in the /src folder -->
<file>src</file>

<!-- Run in verbose mode and specify the precise rule that failed in output -->
<arg value="sv"/>
<arg name="colors"/>

<!-- Use PSR-12 -->
<rule ref="PSR12">
<!-- Exclude function not in camel caps format, to avoid breaking changes -->
<exclude name="PSR1.Methods.CamelCapsMethodName.NotCamelCaps" />

<!-- Don't require spacing after header block -->
<exclude name="PSR12.Files.FileHeader.SpacingAfterBlock" />

<!-- Permit else if over elseif -->
<exclude name="PSR2.ControlStructures.ElseIfDeclaration.NotAllowed" />
</rule>

<!-- Use Squiz -->
<rule ref="Squiz">
<!-- Exclude "Class found in ".php" file; use ".inc" extension instead" -->
<exclude name="Squiz.Files.FileExtension.ClassFound" />

<!-- Exclude PascalCase format for class name, to avoid breaking changes -->
<exclude name="Squiz.Classes.ValidClassName.NotCamelCaps" />

<!-- Exclude variable naming, to avoid breaking changes -->
<exclude name="Squiz.NamingConventions.ValidVariableName.MemberNotCamelCaps" />
<exclude name="Squiz.NamingConventions.ValidVariableName.NotCamelCaps" />
<exclude name="Squiz.NamingConventions.ValidFunctionName.ScopeNotCamelCaps" />

<!-- Allow implicit true and false comparisons -->
<exclude name="Squiz.Operators.ComparisonOperatorUsage.ImplicitTrue" />
<exclude name="Squiz.Operators.ComparisonOperatorUsage.NotAllowed" />

<!-- Don't require // end comments after each function, or Author / Copyright tags -->
<exclude name="Squiz.Commenting.ClosingDeclarationComment.Missing" />
<exclude name="Squiz.Commenting.FileComment.IncorrectAuthor" />
<exclude name="Squiz.Commenting.FileComment.MissingCopyrightTag" />

<!-- Permit inline if statements -->
<exclude name="Squiz.PHP.DisallowInlineIf.Found" />

<!-- Don't require various newlines and spacing before and after functions -->
<exclude name="Squiz.WhiteSpace.FunctionSpacing.AfterLast" />
<exclude name="Squiz.WhiteSpace.FunctionSpacing.After" />
<exclude name="Squiz.WhiteSpace.FunctionClosingBraceSpace.SpacingBeforeClose" />
<exclude name="Squiz.WhiteSpace.MemberVarSpacing.FirstIncorrect" />

<!-- Permit padding surrounding a concat operator -->
<exclude name="Squiz.Strings.ConcatenationSpacing.PaddingFound" />
</rule>
</ruleset>
10 changes: 1 addition & 9 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php"
color="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailures="false"
syntaxCheck="false"
beStrictAboutTestsThatDoNotTestAnything="false"
>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="ConvertKit API Tests">
<directory>tests</directory>
Expand Down
Loading