LoggerTest.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  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()
  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()
  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. public function testEmergency()
  53. {
  54. $this->cli->shouldReceive("emergency")->once()->with("Testing emergency");
  55. $this->logger->emergency("Testing emergency");
  56. }
  57. public function testAlert()
  58. {
  59. $this->cli->shouldReceive("alert")->once()->with("Testing alert");
  60. $this->logger->alert("Testing alert");
  61. }
  62. public function testCritical()
  63. {
  64. $this->cli->shouldReceive("critical")->once()->with("Testing critical");
  65. $this->logger->critical("Testing critical");
  66. }
  67. public function testError()
  68. {
  69. $this->cli->shouldReceive("error")->once()->with("Testing error");
  70. $this->logger->error("Testing error");
  71. }
  72. public function testWarning()
  73. {
  74. $this->cli->shouldReceive("warning")->once()->with("Testing warning");
  75. $this->logger->warning("Testing warning");
  76. }
  77. public function testNotice()
  78. {
  79. $this->cli->shouldReceive("notice")->once()->with("Testing notice");
  80. $this->logger->notice("Testing notice");
  81. }
  82. public function testInfo()
  83. {
  84. $this->cli->shouldReceive("info")->once()->with("Testing info");
  85. $this->logger->info("Testing info");
  86. }
  87. public function testDebug()
  88. {
  89. $this->cli->shouldReceive("debug")->once()->with("Testing debug");
  90. $this->logger->debug("Testing debug");
  91. }
  92. public function testLog()
  93. {
  94. $this->cli->shouldReceive("critical")->once()->with("Testing log");
  95. $this->logger->log(LogLevel::CRITICAL, "Testing log");
  96. }
  97. public function testLevelEmergency()
  98. {
  99. $this->cli->shouldReceive(LogLevel::EMERGENCY)->once()->with("Testing log");
  100. $this->logger->withLogLevel(LogLevel::EMERGENCY)->emergency("Testing log");
  101. }
  102. public function testLevelAlert()
  103. {
  104. $this->cli->shouldReceive("alert")->never();
  105. $this->logger->withLogLevel(LogLevel::EMERGENCY)->alert("Testing log");
  106. }
  107. public function testLevelNotice()
  108. {
  109. $this->cli->shouldReceive(LogLevel::NOTICE)->once()->with("Notice");
  110. $this->logger->withLogLevel(LogLevel::NOTICE)->notice("Notice");
  111. }
  112. public function testLevelDebug()
  113. {
  114. $this->cli->shouldReceive(LogLevel::DEBUG)->once()->with("Debug");
  115. $this->logger->withLogLevel(LogLevel::DEBUG)->debug("Debug");
  116. }
  117. public function testWithInvalidLogLevel1()
  118. {
  119. $this->expectException(InvalidArgumentException::class);
  120. $this->expectExceptionMessage("Unknown log level: INVALID");
  121. $this->logger->withLogLevel("INVALID");
  122. }
  123. public function testWithInvalidLogLevel2()
  124. {
  125. $this->expectException(InvalidArgumentException::class);
  126. $this->expectExceptionMessage("Unknown log level: 0");
  127. $this->logger->withLogLevel(0);
  128. }
  129. public function testContext()
  130. {
  131. $this->cli->shouldReceive("info")->once()->with("With context");
  132. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  133. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  134. $this->cli->shouldReceive("inline")->once()->with("context: ");
  135. $this->cli->shouldReceive("info")->once()->with("CONTEXT");
  136. $this->logger->info("With context", [
  137. "context" => "CONTEXT",
  138. ]);
  139. }
  140. public function testEmptyContext()
  141. {
  142. $this->cli->shouldReceive("info")->once()->with("No context");
  143. $this->logger->info("No context", []);
  144. }
  145. public function testPlaceholders()
  146. {
  147. $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
  148. $this->logger->info("I am {username}!", [
  149. "username" => "Spartacus",
  150. ]);
  151. }
  152. public function testPlaceholdersAndContext()
  153. {
  154. $this->cli->shouldReceive("info")->once()->with("I am Spartacus!");
  155. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  156. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  157. $this->cli->shouldReceive("inline")->once()->with("date: ");
  158. $this->cli->shouldReceive("info")->once()->with("2015-03-01");
  159. $this->logger->info("I am {username}!", [
  160. "username" => "Spartacus",
  161. "date" => "2015-03-01",
  162. ]);
  163. }
  164. public function testRecursiveContext()
  165. {
  166. $this->cli->shouldReceive("info")->once()->with("INFO");
  167. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  168. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  169. $this->cli->shouldReceive("inline")->once()->with("data: ");
  170. $this->cli->shouldReceive("info")->once()->with("[");
  171. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  172. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  173. $this->cli->shouldReceive("inline")->once()->with("field1: ");
  174. $this->cli->shouldReceive("info")->once()->with("One");
  175. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  176. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  177. $this->cli->shouldReceive("inline")->once()->with("field2: ");
  178. $this->cli->shouldReceive("info")->once()->with("Two");
  179. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  180. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  181. $this->cli->shouldReceive("inline")->once()->with("extra: ");
  182. $this->cli->shouldReceive("info")->once()->with("[");
  183. $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
  184. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  185. $this->cli->shouldReceive("inline")->once()->with("0: ");
  186. $this->cli->shouldReceive("info")->once()->with("Three");
  187. $this->cli->shouldReceive("tab")->with(3)->once()->andReturn($this->cli);
  188. $this->cli->shouldReceive("info")->once()->andReturn($this->cli);
  189. $this->cli->shouldReceive("inline")->once()->with("1: ");
  190. $this->cli->shouldReceive("info")->once()->with("Four");
  191. $this->cli->shouldReceive("tab")->with(2)->once()->andReturn($this->cli);
  192. $this->cli->shouldReceive("info")->once()->with("]");
  193. $this->cli->shouldReceive("tab")->with(1)->once()->andReturn($this->cli);
  194. $this->cli->shouldReceive("info")->once()->with("]");
  195. $this->logger->info("INFO", [
  196. "data" => [
  197. "field1" => "One",
  198. "field2" => "Two",
  199. "extra" => ["Three", "Four"],
  200. ],
  201. ]);
  202. }
  203. }