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", diff --git a/tests/ConvertKitAPITest.php b/tests/ConvertKitAPITest.php index e918d23..56483c2 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.