|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Tests for the Integrate_ConvertKit_WPForms_API class. |
| 4 | + * |
| 5 | + * @since 1.3.1 |
| 6 | + */ |
| 7 | +class APITest extends \Codeception\TestCase\WPTestCase |
| 8 | +{ |
| 9 | + /** |
| 10 | + * The testing implementation. |
| 11 | + * |
| 12 | + * @var \WpunitTester. |
| 13 | + */ |
| 14 | + protected $tester; |
| 15 | + |
| 16 | + /** |
| 17 | + * Holds the ConvertKit API class. |
| 18 | + * |
| 19 | + * @since 1.3.1 |
| 20 | + * |
| 21 | + * @var CKGF_API |
| 22 | + */ |
| 23 | + private $api; |
| 24 | + |
| 25 | + /** |
| 26 | + * Performs actions before each test. |
| 27 | + * |
| 28 | + * @since 1.3.1 |
| 29 | + */ |
| 30 | + public function setUp(): void |
| 31 | + { |
| 32 | + parent::setUp(); |
| 33 | + |
| 34 | + // Activate Plugin, to include the Plugin's constants in tests. |
| 35 | + activate_plugins('convertkit-gravity-forms/convertkit.php'); |
| 36 | + |
| 37 | + // Include class from /includes to test, as they won't be loaded by the Plugin |
| 38 | + // because Gravity Forms is not active. |
| 39 | + require_once 'includes/class-wp-ckgf.php'; |
| 40 | + |
| 41 | + // Initialize the classes we want to test. |
| 42 | + $this->api = new CKGF_API( $_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET'] ); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Performs actions after each test. |
| 47 | + * |
| 48 | + * @since 1.3.1 |
| 49 | + */ |
| 50 | + public function tearDown(): void |
| 51 | + { |
| 52 | + // Destroy the classes we tested. |
| 53 | + unset($this->api); |
| 54 | + |
| 55 | + parent::tearDown(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test that the User Agent string is in the expected format and |
| 60 | + * includes the Plugin's name and version number. |
| 61 | + * |
| 62 | + * @since 1.3.1 |
| 63 | + */ |
| 64 | + public function testUserAgent() |
| 65 | + { |
| 66 | + // When an API call is made, inspect the user-agent argument. |
| 67 | + add_filter( |
| 68 | + 'http_request_args', |
| 69 | + function($args, $url) { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter |
| 70 | + $this->assertStringContainsString( |
| 71 | + CKGF_PLUGIN_NAME . '/' . CKGF_PLUGIN_VERSION, |
| 72 | + $args['user-agent'] |
| 73 | + ); |
| 74 | + return $args; |
| 75 | + }, |
| 76 | + 10, |
| 77 | + 2 |
| 78 | + ); |
| 79 | + |
| 80 | + // Perform a request. |
| 81 | + $result = $this->api->account(); |
| 82 | + } |
| 83 | +} |
0 commit comments