Skip to content

Use monolog/monolog 3.x for PHP 8.1+ #64

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 3 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
80 changes: 80 additions & 0 deletions tests/ConvertKitAPITest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
*
Expand Down Expand Up @@ -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.
Expand Down