LoggerTest.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. namespace League\CLImate\Tests;
  3. use League\CLImate\CLImate;
  4. use League\CLImate\Decorator\Style;
  5. use League\CLImate\Logger;
  6. use Mockery;
  7. use Mockery\Mock;
  8. use PHPUnit\Framework\TestCase;
  9. use Psr\Log\InvalidArgumentException;
  10. use Psr\Log\LoggerInterface;
  11. use Psr\Log\LogLevel;
  12. class LoggerTest extends TestCase
  13. {
  14. /**
  15. * @var Logger $logger The instance we are testing.
  16. */
  17. private $logger;
  18. /**
  19. * @var CLImate|Mock $cli A climate instance to test with.
  20. */
  21. private $cli;
  22. public function setUp(): void
  23. {
  24. $this->cli = Mockery::mock(CLImate::class);
  25. $style = Mockery::mock(Style::class);
  26. $style->shouldReceive("get")->andReturn(true);
  27. $this->cli->style = $style;
  28. $this->logger = new Logger(LogLevel::DEBUG, $this->cli);
  29. }
  30. public function tearDown(): void
  31. {
  32. Mockery::close();
  33. }
  34. public function testConstructor1()
  35. {
  36. $logger = new Logger;
  37. $this->assertInstanceOf(LoggerInterface::class, $logger);
  38. $this->assertInstanceOf(Logger::class, $logger);
  39. }
  40. public function testConstructor2()
  41. {
  42. $this->expectException(InvalidArgumentException::class);
  43. $this->expectExceptionMessage("Unknown log level: ");
  44. new Logger("");
  45. }
  46. public function testConstructor3()
  47. {
  48. $this->expectException(InvalidArgumentException::class);
  49. $this->expectExceptionMessage("Unknown log level: 15");
  50. new Logger(15);
  51. }
  52. /**
  53. * @doesNotPerformAssertions
  54. */
  55. public function testEmergency()
  56. {
  57. $this->cli->shouldReceive("emergency")->once()->with("Testing emergency");
  58. $this->logger->emergency("Testing emergency");
  59. }
  60. /**
  61. * @doesNotPerformAssertions
  62. */
  63. public function testAlert()
  64. {
  65. $this->cli->shouldReceive("alert")->once()->with("Testing alert");
  66. $this->logger->alert("Testing alert");
  67. }
  68. /**
  69. * @doesNotPerformAssertions
  70. */
  71. public function testCritical()
  72. {
  73. $this->cli->shouldReceive("critical")->once()->with("Testing critical");
  74. $this->logger->critical("Testing critical");
  75. }
  76. /**
  77. * @doesNotPerformAssertions
  78. */
  79. public function testError()
  80. {
  81. $this->cli->shouldReceive("error")->once()->with("Testing error");
  82. $this->logger->error("Testing error");
  83. }
  84. /**
  85. * @doesNotPerformAssertions
  86. */
  87. public function testWarning()
  88. {
  89. $this->cli->shouldReceive("warning")->once()->with("Testing warning");
  90. $this->logger->warning("Testing warning");
  91. }
  92. /**
  93. * @doesNotPerformAssertions
  94. */
  95. public function testNotice()
  96. {
  97. $this->cli->shouldReceive("notice")->once()->with("Testing notice");
  98. $this->logger->notice("Testing notice");
  99. }
  100. /**
  101. * @doesNotPerformAssertions
  102. */
  103. public function testInfo()
  104. {
  105. $this->cli->shouldReceive("info")->once()->with("Testing info");
  106. $this->logger->info("Testing info");
  107. }
  108. /**
  109. * @doesNotPerformAssertions
  110. */
  111. public function testDebug()
  112. {
  113. $this->cli->shouldReceive("debug")->once()->with("Testing debug");
  114. $this->logger->debug("Testing debug");
  115. }
  116. /**
  117. * @doesNotPerformAssertions
  118. */
  119. public function testLog()
  120. {
  121. $this->cli->shouldReceive("critical")->once()->with("Testing log");
  122. $this->logger->log(LogLevel::CRITICAL, "Testing log");
  123. }
  124. /**
  125. * @doesNotPerformAssertions
  126. */
  127. public function testLevelEmergency()
  128. {
  129. $this->cli->shouldReceive(LogLevel::EMERGENCY)->once()->with("Testing log");
  130. $this->logger->withLogLevel(LogLevel::EMERGENCY)->emergency("Testing log");
  131. }
  132. /**
  133. * @doesNotPerformAssertions
  134. */
  135. public function testLevelAlert()
  136. {
  137. $this->cli->shouldReceive("alert")->never();
  138. $this->logger->withLogLevel(LogLevel::EMERGENCY)->alert("Testing log");
  139. }
  140. /**
  141. * @doesNotPerformAssertions
  142. */
  143. public function testLevelNotice()
  144. {
  145. $this->cli->shouldReceive(LogLevel::NOTICE)->once()->with("Notice");
  146. $this->logger->withLogLevel(LogLevel::NOTICE)->notice("Notice");
  147. }
  148. /**
  149. * @doesNotPerformAssertions
  150. */
  151. public function testLevelDebug()
  152. {
  153. $this->cli->shouldReceive(LogLevel::DEBUG)->once()->with("Debug");
  154. $this->logger->withLogLevel(LogLevel::DEBUG)->debug("Debug");
  155. }
  156. public function testWithInvalidLogLevel1()
  157. {
  158. $this->expectException(InvalidArgumentException::class);
  159. $this->expectExceptionMessage("Unknown log level: INVALID");
  160. $this->logger->withLogLevel("INVALID");
  161. }
  162. public function testWithInvalidLogLevel2()
  163. {
  164. $this->expectException(InvalidArgumentException::class);
  165. $this->expectExceptionMessage("Unknown log level: 0");
  166. $this->logger->withLogLevel(0);
  167. }
  168. /**
  169. * @doesNotPerformAssertions
  170. */
  171. public function testContext()
  172. {
  173. $this->cli->shouldReceive("info")->once()->with("With context");
  174. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  175. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  176. $this->cli->shouldReceive("inline")->once()->with("context: ");
  177. $this->cli->shouldReceive("info")->once()->with("CONTEXT");
  178. $this->logger->info("With context", [
  179. "context" => "CONTEXT",
  180. ]);
  181. }
  182. /**
  183. * @doesNotPerformAssertions
  184. */
  185. public function testEmptyContext()
  186. {
  187. $this->cli->shouldReceive("info")->once()->with("No context");
  188. $this->logger->info("No context", []);
  189. }
  190. /**
  191. * @doesNotPerformAssertions
  192. */
  193. public function testPlaceholders()
  194. {
  195. $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
  196. $this->logger->info("I am {username}!", [
  197. "username" => "Spartacus",
  198. ]);
  199. }
  200. /**
  201. * @doesNotPerformAssertions
  202. */
  203. public function testPlaceholdersAndContext()
  204. {
  205. $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
  206. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  207. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  208. $this->cli->shouldReceive("inline")->once()->with("date: ");
  209. $this->cli->shouldReceive("info")->once()->with("2015-03-01");
  210. $this->logger->info("I am {username}!", [
  211. "username" => "Spartacus",
  212. "date" => "2015-03-01",
  213. ]);
  214. }
  215. /**
  216. * @doesNotPerformAssertions
  217. */
  218. public function testRecursiveContext()
  219. {
  220. $this->cli->shouldReceive("info")->once()->with("INFO");
  221. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  222. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  223. $this->cli->shouldReceive("inline")->once()->with("data: ");
  224. $this->cli->shouldReceive("info")->once()->with("[");
  225. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  226. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  227. $this->cli->shouldReceive("inline")->once()->with("field1: ");
  228. $this->cli->shouldReceive("info")->once()->with("One");
  229. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  230. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  231. $this->cli->shouldReceive("inline")->once()->with("field2: ");
  232. $this->cli->shouldReceive("info")->once()->with("Two");
  233. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  234. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  235. $this->cli->shouldReceive("inline")->once()->with("extra: ");
  236. $this->cli->shouldReceive("info")->once()->with("[");
  237. $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
  238. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  239. $this->cli->shouldReceive("inline")->once()->with("0: ");
  240. $this->cli->shouldReceive("info")->once()->with("Three");
  241. $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
  242. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  243. $this->cli->shouldReceive("inline")->once()->with("1: ");
  244. $this->cli->shouldReceive("info")->once()->with("Four");
  245. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  246. $this->cli->shouldReceive("info")->once()->with("]");
  247. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  248. $this->cli->shouldReceive("info")->once()->with("]");
  249. $this->logger->info("INFO", [
  250. "data" => [
  251. "field1" => "One",
  252. "field2" => "Two",
  253. "extra" => ["Three", "Four"],
  254. ],
  255. ]);
  256. }
  257. }