From 9a589791c0a93265220e01fbe5324124a40fe352 Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 3 Jul 2023 12:49:02 +0100 Subject: [PATCH 1/3] Added logging tests --- tests/ConvertKitAPITest.php | 80 +++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index e918d23..d9140b9 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -14,6 +14,15 @@ class ConvertKitAPITest extends TestCase */ protected $api; + /** + * Location of the monologger log file. + * + * @since 1.2.0 + * + * @var string + */ + protected $logFile = ''; + /** * Load .env configuration into $_ENV superglobal, and initialize the API * class before each test. @@ -28,10 +37,49 @@ protected function setUp(): void $dotenv = Dotenv\Dotenv::createImmutable(dirname(dirname(__FILE__))); $dotenv->load(); + // Set location where API class will create/write the log file. + $this->logFile = dirname(dirname(__FILE__)).'/src/logs/debug.log'; + + // Delete any existing debug log file. + $this->deleteLogFile(); + // Setup API. $this->api = new \ConvertKit_API\ConvertKit_API($_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET']); } + /** + * Test that debug logging works when enabled and an API call is made. + * + * @since 1.2.0 + * + * @return void + */ + public function testDebugEnabled() + { + // Setup API with debugging enabled. + $api = new \ConvertKit_API\ConvertKit_API($_ENV['CONVERTKIT_API_KEY'], $_ENV['CONVERTKIT_API_SECRET'], true); + $result = $api->get_account(); + + // Confirm that the log includes expected data. + $this->assertStringContainsString('ck-debug.INFO: GET account', $this->getLogFileContents()); + $this->assertStringContainsString('ck-debug.INFO: Finish request successfully', $this->getLogFileContents()); + } + + /** + * Test that debug logging is not performed when disabled and an API call is made. + * + * @since 1.2.0 + * + * @return void + */ + public function testDebugDisabled() + { + $result = $this->api->get_account(); + + // Confirm that the log is empty / doesn't exist. + $this->assertEmpty($this->getLogFileContents()); + } + /** * Test that a ClientException is thrown when invalid API credentials are supplied. * @@ -2035,6 +2083,38 @@ public function testGetResourceInaccessibleURL() $markup = $this->api->get_resource('https://convertkit.com/a/url/that/does/not/exist'); } + /** + * Deletes the src/logs/debug.log file, if it remains following a previous test. + * + * @since 1.2.0 + * + * @return void + */ + private function deleteLogFile() + { + if(file_exists($this->logFile)) { + unlink($this->logFile); + } + } + + /** + * Returns the contents of the src/logs/debug.log file. + * + * @since 1.2.0 + * + * @return string + */ + private function getLogFileContents() + { + // Return blank string if no log file. + if(!file_exists($this->logFile)) { + return ''; + } + + // Return log file contents. + return file_get_contents($this->logFile); + } + /** * Generates a unique email address for use in a test, comprising of a prefix, * date + time and PHP version number. From 27a14d8b3759124d969cd221b37e51f8dadc227b Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 3 Jul 2023 12:49:40 +0100 Subject: [PATCH 2/3] Coding standards for tests --- tests/ConvertKitAPITest.php | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index d9140b9..56483c2 100644 --- a/tests/ConvertKitAPITest.php +++ b/tests/ConvertKitAPITest.php @@ -16,9 +16,9 @@ class ConvertKitAPITest extends TestCase /** * Location of the monologger log file. - * + * * @since 1.2.0 - * + * * @var string */ protected $logFile = ''; @@ -38,7 +38,7 @@ protected function setUp(): void $dotenv->load(); // Set location where API class will create/write the log file. - $this->logFile = dirname(dirname(__FILE__)).'/src/logs/debug.log'; + $this->logFile = dirname(dirname(__FILE__)) . '/src/logs/debug.log'; // Delete any existing debug log file. $this->deleteLogFile(); @@ -49,9 +49,9 @@ protected function setUp(): void /** * Test that debug logging works when enabled and an API call is made. - * + * * @since 1.2.0 - * + * * @return void */ public function testDebugEnabled() @@ -67,15 +67,15 @@ public function testDebugEnabled() /** * Test that debug logging is not performed when disabled and an API call is made. - * + * * @since 1.2.0 - * + * * @return void */ public function testDebugDisabled() { $result = $this->api->get_account(); - + // Confirm that the log is empty / doesn't exist. $this->assertEmpty($this->getLogFileContents()); } @@ -2085,29 +2085,29 @@ public function testGetResourceInaccessibleURL() /** * Deletes the src/logs/debug.log file, if it remains following a previous test. - * + * * @since 1.2.0 - * + * * @return void */ private function deleteLogFile() { - if(file_exists($this->logFile)) { + if (file_exists($this->logFile)) { unlink($this->logFile); } } /** * Returns the contents of the src/logs/debug.log file. - * + * * @since 1.2.0 - * + * * @return string */ private function getLogFileContents() { // Return blank string if no log file. - if(!file_exists($this->logFile)) { + if (!file_exists($this->logFile)) { return ''; } From 98b7ec49e408093b0ceaf89df0f9c128f3b8d8aa Mon Sep 17 00:00:00 2001 From: Tim Carr Date: Mon, 3 Jul 2023 12:51:20 +0100 Subject: [PATCH 3/3] Support monolog/monolog 3.x --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 3cdf706..3a1c43a 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,7 @@ "require": { "php": ">=7.4", "guzzlehttp/guzzle": "^6.5|^7.0", - "monolog/monolog": "^2.0" + "monolog/monolog": "^2.0|^3.0" }, "require-dev": { "vlucas/phpdotenv": "^5.5",