@@ -14,6 +14,15 @@ class ConvertKitAPITest extends TestCase
14
14
*/
15
15
protected $ api ;
16
16
17
+ /**
18
+ * Location of the monologger log file.
19
+ *
20
+ * @since 1.2.0
21
+ *
22
+ * @var string
23
+ */
24
+ protected $ logFile = '' ;
25
+
17
26
/**
18
27
* Load .env configuration into $_ENV superglobal, and initialize the API
19
28
* class before each test.
@@ -28,10 +37,49 @@ protected function setUp(): void
28
37
$ dotenv = Dotenv \Dotenv::createImmutable (dirname (dirname (__FILE__ )));
29
38
$ dotenv ->load ();
30
39
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
+
31
46
// Setup API.
32
47
$ this ->api = new \ConvertKit_API \ConvertKit_API ($ _ENV ['CONVERTKIT_API_KEY ' ], $ _ENV ['CONVERTKIT_API_SECRET ' ]);
33
48
}
34
49
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
+
35
83
/**
36
84
* Test that a ClientException is thrown when invalid API credentials are supplied.
37
85
*
@@ -2035,6 +2083,38 @@ public function testGetResourceInaccessibleURL()
2035
2083
$ markup = $ this ->api ->get_resource ('https://convertkit.com/a/url/that/does/not/exist ' );
2036
2084
}
2037
2085
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
+
2038
2118
/**
2039
2119
* Generates a unique email address for use in a test, comprising of a prefix,
2040
2120
* date + time and PHP version number.
0 commit comments