/core/src/test/php/net/xp_framework/unittest/logging/LogCategoryTest.class.php

https://github.com/oanas/xp-framework · PHP · 439 lines · 197 code · 39 blank · 203 comment · 0 complexity · 4de5b407f5dc5b0aab64a632288bbe01 MD5 · raw file

  1. <?php
  2. /* This class is part of the XP framework
  3. *
  4. * $Id$
  5. */
  6. uses(
  7. 'unittest.TestCase',
  8. 'util.log.Logger',
  9. 'util.log.Appender',
  10. 'util.log.LogAppender',
  11. 'util.log.layout.PatternLayout'
  12. );
  13. /**
  14. * Tests LogCategory class
  15. *
  16. * @purpose Unit Test
  17. */
  18. class LogCategoryTest extends TestCase {
  19. public $cat= NULL;
  20. /**
  21. * Setup method. Creates logger and cat member for easier access to
  22. * the Logger instance
  23. *
  24. */
  25. public function setUp() {
  26. $this->cat= new LogCategory('test');
  27. }
  28. /**
  29. * Create a mock appender which simply stores all messages passed to
  30. * its append() method.
  31. *
  32. * @return util.log.Appender
  33. */
  34. protected function mockAppender() {
  35. $appender= newinstance('util.log.Appender', array(), '{
  36. public $messages= array();
  37. public function append(LoggingEvent $event) {
  38. $this->messages[]= array(
  39. strtolower(LogLevel::nameOf($event->getLevel())),
  40. $this->layout->format($event)
  41. );
  42. }
  43. }');
  44. return $appender->withLayout(new PatternLayout('%m'));
  45. }
  46. /**
  47. * Helper method
  48. *
  49. * @param string method
  50. * @param mixed[] args default ["Argument"]
  51. * @throws unittest.AssertionFailedError
  52. */
  53. protected function assertLog($method, $args= array('Argument')) {
  54. $app= $this->cat->addAppender($this->mockAppender());
  55. call_user_func_array(array($this->cat, $method), $args);
  56. $this->assertEquals(array(array_merge((array)$method, $args)), $app->messages);
  57. }
  58. /**
  59. * Helper method
  60. *
  61. * @param string method
  62. * @param mixed[] args default ["Argument"]
  63. * @throws unittest.AssertionFailedError
  64. */
  65. protected function assertLogf($method, $args= array('Argument')) {
  66. $app= $this->cat->addAppender($this->mockAppender());
  67. call_user_func_array(array($this->cat, $method), $args);
  68. $this->assertEquals(array(array_merge((array)substr($method, 0, -1), (array)vsprintf(array_shift($args), $args))), $app->messages);
  69. }
  70. /**
  71. * Ensure the logger category initially has no appenders
  72. *
  73. */
  74. #[@test]
  75. public function initiallyNoAppenders() {
  76. $this->assertFalse($this->cat->hasAppenders());
  77. }
  78. /**
  79. * Tests adding an appender returns the added appender
  80. *
  81. */
  82. #[@test]
  83. public function addAppenderReturnsAddedAppender() {
  84. $appender= $this->mockAppender();
  85. $this->assertEquals($appender, $this->cat->addAppender($appender));
  86. }
  87. /**
  88. * Tests adding an appender returns the log category
  89. *
  90. */
  91. #[@test]
  92. public function withAppenderReturnsCategory() {
  93. $this->assertEquals($this->cat, $this->cat->withAppender($this->mockAppender()));
  94. }
  95. /**
  96. * Tests hasAppenders() and addAppender() methods
  97. *
  98. */
  99. #[@test]
  100. public function hasAppendersAfterAdding() {
  101. $this->cat->addAppender($this->mockAppender());
  102. $this->assertTrue($this->cat->hasAppenders());
  103. }
  104. /**
  105. * Tests hasAppenders() and removeAppender() methods
  106. *
  107. */
  108. #[@test]
  109. public function hasNoMoreAppendersAfterRemoving() {
  110. $a= $this->cat->addAppender($this->mockAppender());
  111. $this->cat->removeAppender($a);
  112. $this->assertFalse($this->cat->hasAppenders());
  113. }
  114. /**
  115. * Tests addAppender() method
  116. *
  117. */
  118. #[@test]
  119. public function addAppenderTwice() {
  120. $a= $this->mockAppender();
  121. $this->cat->addAppender($a);
  122. $this->cat->addAppender($a);
  123. $this->cat->removeAppender($a);
  124. $this->assertFalse($this->cat->hasAppenders());
  125. }
  126. /**
  127. * Tests addAppender() and removeAppender() methods
  128. *
  129. */
  130. #[@test]
  131. public function addAppenderTwiceWithDifferentFlags() {
  132. $a= $this->mockAppender();
  133. $this->cat->addAppender($a, LogLevel::INFO);
  134. $this->cat->addAppender($a, LogLevel::WARN);
  135. $this->cat->removeAppender($a, LogLevel::INFO);
  136. $this->assertTrue($this->cat->hasAppenders());
  137. $this->cat->removeAppender($a, LogLevel::WARN);
  138. $this->assertFalse($this->cat->hasAppenders());
  139. }
  140. /**
  141. * Tests adding an appender sets default layout if appender does not
  142. * have a layout.
  143. *
  144. */
  145. #[@test]
  146. public function addAppenderSetsDefaultLayout() {
  147. $appender= newinstance('util.log.Appender', array(), '{
  148. public function append(LoggingEvent $event) { }
  149. }');
  150. $this->cat->addAppender($appender);
  151. $this->assertClass($appender->getLayout(), 'util.log.layout.DefaultLayout');
  152. }
  153. /**
  154. * Tests adding an appender does not overwrite layout
  155. *
  156. */
  157. #[@test]
  158. public function addAppenderDoesNotOverwriteLayout() {
  159. $appender= newinstance('util.log.Appender', array(), '{
  160. public function append(LoggingEvent $event) { }
  161. }');
  162. $this->cat->addAppender($appender->withLayout(new PatternLayout('%m')));
  163. $this->assertClass($appender->getLayout(), 'util.log.layout.PatternLayout');
  164. }
  165. /**
  166. * Tests adding an appender sets default layout if appender does not
  167. * have a layout.
  168. *
  169. */
  170. #[@test]
  171. public function withAppenderSetsLayout() {
  172. $appender= newinstance('util.log.Appender', array(), '{
  173. public function append(LoggingEvent $event) { }
  174. }');
  175. $this->cat->withAppender($appender);
  176. $this->assertClass($appender->getLayout(), 'util.log.layout.DefaultLayout');
  177. }
  178. /**
  179. * Tests adding an appender does not overwrite layout
  180. *
  181. */
  182. #[@test]
  183. public function withAppenderDoesNotOverwriteLayout() {
  184. $appender= newinstance('util.log.Appender', array(), '{
  185. public function append(LoggingEvent $event) { }
  186. }');
  187. $this->cat->withAppender($appender->withLayout(new PatternLayout('%m')));
  188. $this->assertClass($appender->getLayout(), 'util.log.layout.PatternLayout');
  189. }
  190. /**
  191. * Tests equals() method
  192. *
  193. */
  194. #[@test]
  195. public function logCategoriesWithSameIdentifierAreEqual() {
  196. $this->assertEquals(new LogCategory('test'), $this->cat);
  197. }
  198. /**
  199. * Tests equals() method
  200. *
  201. */
  202. #[@test]
  203. public function logCategoriesDifferingAppendersNotEqual() {
  204. $this->assertNotEquals(
  205. new LogCategory('test'),
  206. $this->cat->withAppender($this->mockAppender())
  207. );
  208. }
  209. /**
  210. * Tests equals() method
  211. *
  212. */
  213. #[@test]
  214. public function logCategoriesAppendersDifferingInFlagsNotEqual() {
  215. $appender= $this->mockAppender();
  216. $this->assertNotEquals(
  217. create(new LogCategory('test'))->withAppender($appender, LogLevel::WARN),
  218. $this->cat->withAppender($appender)
  219. );
  220. }
  221. /**
  222. * Tests equals() method
  223. *
  224. */
  225. #[@test]
  226. public function logCategoriesSameAppendersEqual() {
  227. $appender= $this->mockAppender();
  228. $this->assertEquals(
  229. create(new LogCategory('test'))->withAppender($appender),
  230. $this->cat->withAppender($appender)
  231. );
  232. }
  233. /**
  234. * Tests debug() method
  235. *
  236. */
  237. #[@test]
  238. public function debug() {
  239. $this->assertLog(__FUNCTION__);
  240. }
  241. /**
  242. * Tests debugf() method
  243. *
  244. */
  245. #[@test]
  246. public function debugf() {
  247. $this->assertLogf(__FUNCTION__, array('Hello %s', __CLASS__));
  248. }
  249. /**
  250. * Tests info() method
  251. *
  252. */
  253. #[@test]
  254. public function info() {
  255. $this->assertLog(__FUNCTION__);
  256. }
  257. /**
  258. * Tests infof() method
  259. *
  260. */
  261. #[@test]
  262. public function infof() {
  263. $this->assertLogf(__FUNCTION__, array('Hello %s', __CLASS__));
  264. }
  265. /**
  266. * Tests warn() method
  267. *
  268. */
  269. #[@test]
  270. public function warn() {
  271. $this->assertLog(__FUNCTION__);
  272. }
  273. /**
  274. * Tests warnf() method
  275. *
  276. */
  277. #[@test]
  278. public function warnf() {
  279. $this->assertLogf(__FUNCTION__, array('Hello %s', __CLASS__));
  280. }
  281. /**
  282. * Tests error() method
  283. *
  284. */
  285. #[@test]
  286. public function error() {
  287. $this->assertLog(__FUNCTION__);
  288. }
  289. /**
  290. * Tests errorf() method
  291. *
  292. */
  293. #[@test]
  294. public function errorf() {
  295. $this->assertLogf(__FUNCTION__, array('Hello %s', __CLASS__));
  296. }
  297. /**
  298. * Tests mark() method
  299. *
  300. */
  301. #[@test]
  302. public function mark() {
  303. $app= $this->cat->addAppender($this->mockAppender());
  304. $this->cat->mark();
  305. $this->assertEquals(array(array('info', str_repeat('-', 72))), $app->messages);
  306. }
  307. /**
  308. * Tests flags
  309. *
  310. */
  311. #[@test]
  312. public function warningMessageOnlyGetsAppendedToWarnAppender() {
  313. $app1= $this->cat->addAppender($this->mockAppender(), LogLevel::INFO);
  314. $app2= $this->cat->addAppender($this->mockAppender(), LogLevel::WARN);
  315. $this->cat->warn('Test');
  316. $this->assertEquals(array(), $app1->messages);
  317. $this->assertEquals(array(array('warn', 'Test')), $app2->messages);
  318. }
  319. /**
  320. * Tests adding a deprecated util.log.LogAppender results in it being
  321. * wrapped in a util.log.LogAppenderAdapter
  322. *
  323. * @deprecated
  324. */
  325. #[@test]
  326. public function logAppenderAdapter() {
  327. $added= $this->cat->addAppender(newinstance('util.log.LogAppender', array(), '{
  328. public function append() { }
  329. }'));
  330. $this->assertClass($added, 'util.log.LogAppenderAdapter');
  331. }
  332. /**
  333. * Tests getAppenders()
  334. *
  335. */
  336. #[@test]
  337. public function getAppenders() {
  338. $appender= $this->mockAppender();
  339. $this->cat->addAppender($appender);
  340. $this->assertEquals(array($appender), $this->cat->getAppenders());
  341. }
  342. /**
  343. * Tests getAppenders()
  344. *
  345. */
  346. #[@test]
  347. public function getAppendersWithoutAppendersAdded() {
  348. $this->assertEquals(array(), $this->cat->getAppenders());
  349. }
  350. /**
  351. * Tests getAppenders()
  352. *
  353. */
  354. #[@test]
  355. public function getAllAppendersWhenErrorLevelAppenderExists() {
  356. $appender= $this->cat->addAppender($this->mockAppender(), LogLevel::ERROR);
  357. $this->assertEquals(array($appender), $this->cat->getAppenders());
  358. }
  359. /**
  360. * Tests getAppenders()
  361. *
  362. */
  363. #[@test]
  364. public function getErrorLevelAppendersWhenErrorLevelAppendersExist() {
  365. $appender= $this->cat->addAppender($this->mockAppender(), LogLevel::ERROR);
  366. $this->assertEquals(array($appender), $this->cat->getAppenders(LogLevel::ERROR));
  367. }
  368. /**
  369. * Tests getAppenders()
  370. *
  371. */
  372. #[@test]
  373. public function getInfoLevelAppendersWhenOnlyErrorLevelAppendersExist() {
  374. $appender= $this->cat->addAppender($this->mockAppender(), LogLevel::ERROR);
  375. $this->assertEquals(array(), $this->cat->getAppenders(LogLevel::INFO));
  376. }
  377. /**
  378. * Tests getAppenders()
  379. *
  380. */
  381. #[@test]
  382. public function getInfoLevelAppendersWhenErrorAndInfoLevelAppenderExists() {
  383. $appender= $this->cat->addAppender($this->mockAppender(), LogLevel::ERROR | LogLevel::INFO);
  384. $this->assertEquals(array($appender), $this->cat->getAppenders(LogLevel::INFO));
  385. }
  386. /**
  387. * Tests getAppenders()
  388. *
  389. */
  390. #[@test]
  391. public function getAllAppenders() {
  392. $app1= $this->cat->addAppender($this->mockAppender(), LogLevel::ERROR);
  393. $app2= $this->cat->addAppender($this->mockAppender(), LogLevel::WARN);
  394. $app3= $this->cat->addAppender($this->mockAppender(), LogLevel::INFO);
  395. $app4= $this->cat->addAppender($this->mockAppender(), LogLevel::DEBUG);
  396. $this->assertEquals(array($app1, $app2, $app3, $app4), $this->cat->getAppenders());
  397. }
  398. }
  399. ?>