@@ -14,6 +14,15 @@ class ConvertKitAPITest extends TestCase
1414 */
1515 protected $ api ;
1616
17+ /**
18+ * Location of the monologger log file.
19+ *
20+ * @since 1.2.0
21+ *
22+ * @var string
23+ */
24+ protected $ logFile = '' ;
25+
1726 /**
1827 * Load .env configuration into $_ENV superglobal, and initialize the API
1928 * class before each test.
@@ -28,10 +37,49 @@ protected function setUp(): void
2837 $ dotenv = Dotenv \Dotenv::createImmutable (dirname (dirname (__FILE__ )));
2938 $ dotenv ->load ();
3039
40+ // Set location where API class will create/write the log file.
41+ $ this ->logFile = dirname (dirname (__FILE__ )) . '/src/logs/debug.log ' ;
42+
43+ // Delete any existing debug log file.
44+ $ this ->deleteLogFile ();
45+
3146 // Setup API.
3247 $ this ->api = new \ConvertKit_API \ConvertKit_API ($ _ENV ['CONVERTKIT_API_KEY ' ], $ _ENV ['CONVERTKIT_API_SECRET ' ]);
3348 }
3449
50+ /**
51+ * Test that debug logging works when enabled and an API call is made.
52+ *
53+ * @since 1.2.0
54+ *
55+ * @return void
56+ */
57+ public function testDebugEnabled ()
58+ {
59+ // Setup API with debugging enabled.
60+ $ api = new \ConvertKit_API \ConvertKit_API ($ _ENV ['CONVERTKIT_API_KEY ' ], $ _ENV ['CONVERTKIT_API_SECRET ' ], true );
61+ $ result = $ api ->get_account ();
62+
63+ // Confirm that the log includes expected data.
64+ $ this ->assertStringContainsString ('ck-debug.INFO: GET account ' , $ this ->getLogFileContents ());
65+ $ this ->assertStringContainsString ('ck-debug.INFO: Finish request successfully ' , $ this ->getLogFileContents ());
66+ }
67+
68+ /**
69+ * Test that debug logging is not performed when disabled and an API call is made.
70+ *
71+ * @since 1.2.0
72+ *
73+ * @return void
74+ */
75+ public function testDebugDisabled ()
76+ {
77+ $ result = $ this ->api ->get_account ();
78+
79+ // Confirm that the log is empty / doesn't exist.
80+ $ this ->assertEmpty ($ this ->getLogFileContents ());
81+ }
82+
3583 /**
3684 * Test that a ClientException is thrown when invalid API credentials are supplied.
3785 *
@@ -2035,6 +2083,38 @@ public function testGetResourceInaccessibleURL()
20352083 $ markup = $ this ->api ->get_resource ('https://convertkit.com/a/url/that/does/not/exist ' );
20362084 }
20372085
2086+ /**
2087+ * Deletes the src/logs/debug.log file, if it remains following a previous test.
2088+ *
2089+ * @since 1.2.0
2090+ *
2091+ * @return void
2092+ */
2093+ private function deleteLogFile ()
2094+ {
2095+ if (file_exists ($ this ->logFile )) {
2096+ unlink ($ this ->logFile );
2097+ }
2098+ }
2099+
2100+ /**
2101+ * Returns the contents of the src/logs/debug.log file.
2102+ *
2103+ * @since 1.2.0
2104+ *
2105+ * @return string
2106+ */
2107+ private function getLogFileContents ()
2108+ {
2109+ // Return blank string if no log file.
2110+ if (!file_exists ($ this ->logFile )) {
2111+ return '' ;
2112+ }
2113+
2114+ // Return log file contents.
2115+ return file_get_contents ($ this ->logFile );
2116+ }
2117+
20382118 /**
20392119 * Generates a unique email address for use in a test, comprising of a prefix,
20402120 * date + time and PHP version number.
0 commit comments