LogTestTraitTest.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
  5. * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  6. *
  7. * Licensed under The MIT License
  8. * For full copyright and license information, please see the LICENSE.txt
  9. * Redistributions of files must retain the above copyright notice.
  10. *
  11. * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
  12. * @link https://cakephp.org CakePHP(tm) Project
  13. * @since 5.1.0
  14. * @license https://opensource.org/licenses/mit-license.php MIT License
  15. */
  16. namespace Cake\Test\TestCase\TestSuite;
  17. use Cake\Log\Log;
  18. use Cake\TestSuite\LogTestTrait;
  19. use Cake\TestSuite\TestCase;
  20. use PHPUnit\Framework\AssertionFailedError;
  21. use TestApp\Log\Engine\TestAppLog;
  22. /**
  23. * Tests LogTrait assertions
  24. */
  25. class LogTestTraitTest extends TestCase
  26. {
  27. use LogTestTrait;
  28. /**
  29. * Test expecting log messages
  30. */
  31. public function testExpectLog(): void
  32. {
  33. $this->setupLog('debug');
  34. Log::setConfig([
  35. 'error' => [
  36. 'className' => TestAppLog::class,
  37. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  38. ],
  39. ]);
  40. Log::debug('This usually needs to happen inside your app');
  41. $this->assertLogMessage('debug', 'This usually needs to happen inside your app');
  42. }
  43. /**
  44. * Test expecting log messages from different engines
  45. */
  46. public function testExpectMultipleLog(): void
  47. {
  48. $this->setupLog(['debug', 'error']);
  49. Log::debug('This usually needs to happen inside your app');
  50. Log::error('Some error message');
  51. $this->assertLogMessage('debug', 'This usually needs to happen inside your app');
  52. $this->assertLogMessage('error', 'Some error message');
  53. }
  54. /**
  55. * Test log messages from lower levels don't get mixed up with upper level ones
  56. */
  57. public function testExpectMultipleLogsMixedUpWithHigherFails(): void
  58. {
  59. $this->setupLog(['debug', 'error']);
  60. Log::debug('This usually needs to happen inside your app');
  61. Log::error('Some error message');
  62. $this->expectException(AssertionFailedError::class);
  63. $this->assertLogMessage('error', 'This usually needs to happen inside your app');
  64. }
  65. /**
  66. * Test log messages from higher levels don't get mixed up with lower level ones
  67. */
  68. public function testExpectMultipleLogsMixedUpWithLowerFails(): void
  69. {
  70. $this->setupLog(['debug', 'error']);
  71. Log::debug('This usually needs to happen inside your app');
  72. Log::error('Some error message');
  73. $this->expectException(AssertionFailedError::class);
  74. $this->assertLogMessage('debug', 'Some error message');
  75. }
  76. /**
  77. * Test expecting log messages contains
  78. */
  79. public function testExpectLogContains(): void
  80. {
  81. $this->setupLog('debug');
  82. Log::debug('This usually needs to happen inside your app');
  83. $this->assertLogMessageContains('debug', 'This usually needs');
  84. }
  85. /**
  86. * Test expecting log message without setup
  87. */
  88. public function testExpectLogWithoutSetup(): void
  89. {
  90. $this->expectException(AssertionFailedError::class);
  91. $this->expectExceptionMessage('Could not find the message `debug: ` in logs.');
  92. $this->assertLogMessage('debug', '');
  93. }
  94. /**
  95. * Test expecting log messages from different engines with custom configs
  96. */
  97. public function testExpectMultipleLogWithLevels(): void
  98. {
  99. $this->setupLog([
  100. 'debug' => [
  101. 'levels' => ['notice', 'info', 'debug'],
  102. ],
  103. 'error' => [
  104. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  105. ],
  106. ]);
  107. Log::notice('This is a notice message');
  108. Log::info('This is a info message');
  109. Log::debug('This is a debug message');
  110. Log::warning('This is a warning message');
  111. Log::error('This is a error message');
  112. Log::critical('This is a critical message');
  113. Log::alert('This is a alert message');
  114. Log::emergency('This is a emergency message');
  115. $this->assertLogMessage('notice', 'This is a notice message');
  116. $this->assertLogMessage('info', 'This is a info message');
  117. $this->assertLogMessage('debug', 'This is a debug message');
  118. $this->assertLogMessage('warning', 'This is a warning message');
  119. $this->assertLogMessage('error', 'This is a error message');
  120. $this->assertLogMessage('critical', 'This is a critical message');
  121. $this->assertLogMessage('alert', 'This is a alert message');
  122. $this->assertLogMessage('emergency', 'This is a emergency message');
  123. }
  124. /**
  125. * Test expecting log messages from different engines with custom configs
  126. */
  127. public function testExpectMultipleLogAbsent(): void
  128. {
  129. $this->setupLog([
  130. 'error' => [
  131. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  132. ],
  133. ]);
  134. Log::notice('This is a notice message');
  135. Log::info('This is a info message');
  136. Log::debug('This is a debug message');
  137. Log::warning('This is a warning message');
  138. Log::error('This is a error message');
  139. Log::critical('This is a critical message');
  140. Log::alert('This is a alert message');
  141. Log::emergency('This is a emergency message');
  142. $this->assertLogAbsent('notice', 'No notice messages should be captured');
  143. $this->assertLogAbsent('info', 'No info messages should be captured');
  144. $this->assertLogAbsent('debug', 'No debug messages should be captured');
  145. $this->assertLogMessage('warning', 'This is a warning message');
  146. $this->assertLogMessage('error', 'This is a error message');
  147. $this->assertLogMessage('critical', 'This is a critical message');
  148. $this->assertLogMessage('alert', 'This is a alert message');
  149. $this->assertLogMessage('emergency', 'This is a emergency message');
  150. }
  151. public function testAbsentLogWithSetup(): void
  152. {
  153. $this->setupLog([
  154. 'error' => [
  155. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  156. ],
  157. ]);
  158. $this->assertLogAbsent('warning', 'This is a warning message');
  159. $this->assertLogAbsent('error', 'This is a error message');
  160. $this->assertLogAbsent('critical', 'This is a critical message');
  161. $this->assertLogAbsent('alert', 'This is a critical message');
  162. $this->assertLogAbsent('emergency', 'This is a emergency message');
  163. }
  164. public function testAbsentLogWithoutSetup(): void
  165. {
  166. Log::setConfig([
  167. 'debug' => [
  168. 'className' => TestAppLog::class,
  169. 'levels' => ['notice', 'info', 'debug'],
  170. ],
  171. ]);
  172. Log::debug('This is a debug message');
  173. $this->expectNotToPerformAssertions();
  174. $this->assertLogAbsent('debug', 'Some error message');
  175. }
  176. /**
  177. * Test expecting log messages from different engines with custom configs
  178. */
  179. public function testExpectMultipleLogWithLevelsAndScopes(): void
  180. {
  181. $this->setupLog([
  182. 'debug' => [
  183. 'levels' => ['notice', 'info', 'debug'],
  184. ],
  185. 'error' => [
  186. 'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
  187. ],
  188. 'scoped' => [
  189. 'scopes' => ['app.security'],
  190. 'levels' => ['info'],
  191. ],
  192. ]);
  193. Log::info('This is a info message');
  194. Log::info('security settings changed', 'app.security');
  195. Log::warning('This is a warning message');
  196. Log::error('This is a error message');
  197. $this->assertLogMessage('info', 'This is a info message');
  198. $this->assertLogMessage('info', 'security settings changed');
  199. $this->assertLogMessage('info', 'security settings changed', 'app.security');
  200. $this->assertLogMessage('warning', 'This is a warning message');
  201. $this->assertLogMessage('error', 'This is a error message');
  202. }
  203. }