PageRenderTime 79ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/silex/silex/tests/Silex/Tests/Provider/MonologServiceProviderTest.php

https://gitlab.com/Szedrik/rest
PHP | 212 lines | 141 code | 48 blank | 23 comment | 3 complexity | 7c510f0171873644ba27b484b93e6181 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Silex framework.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * This source file is subject to the MIT license that is bundled
  8. * with this source code in the file LICENSE.
  9. */
  10. namespace Silex\Tests\Provider;
  11. use Monolog\Formatter\JsonFormatter;
  12. use Monolog\Handler\TestHandler;
  13. use Monolog\Logger;
  14. use Silex\Application;
  15. use Silex\Provider\MonologServiceProvider;
  16. use Symfony\Component\HttpFoundation\RedirectResponse;
  17. use Symfony\Component\HttpFoundation\Request;
  18. /**
  19. * MonologProvider test cases.
  20. *
  21. * @author Igor Wiedler <igor@wiedler.ch>
  22. */
  23. class MonologServiceProviderTest extends \PHPUnit_Framework_TestCase
  24. {
  25. public function testRequestLogging()
  26. {
  27. $app = $this->getApplication();
  28. $app->get('/foo', function () use ($app) {
  29. return 'foo';
  30. });
  31. $this->assertFalse($app['monolog.handler']->hasInfoRecords());
  32. $request = Request::create('/foo');
  33. $app->handle($request);
  34. $this->assertTrue($app['monolog.handler']->hasDebug('> GET /foo'));
  35. $this->assertTrue($app['monolog.handler']->hasDebug('< 200'));
  36. $records = $app['monolog.handler']->getRecords();
  37. $this->assertContains('Matched route "GET_foo"', $records[0]['message']);
  38. }
  39. public function testManualLogging()
  40. {
  41. $app = $this->getApplication();
  42. $app->get('/log', function () use ($app) {
  43. $app['monolog']->addDebug('logging a message');
  44. });
  45. $this->assertFalse($app['monolog.handler']->hasDebugRecords());
  46. $request = Request::create('/log');
  47. $app->handle($request);
  48. $this->assertTrue($app['monolog.handler']->hasDebug('logging a message'));
  49. }
  50. public function testOverrideFormatter()
  51. {
  52. $app = new Application();
  53. $app->register(new MonologServiceProvider(), array(
  54. 'monolog.formatter' => new JsonFormatter(),
  55. 'monolog.logfile' => 'php://memory',
  56. ));
  57. $this->assertInstanceOf('Monolog\Formatter\JsonFormatter', $app['monolog.handler']->getFormatter());
  58. }
  59. public function testErrorLogging()
  60. {
  61. $app = $this->getApplication();
  62. $app->error(function (\Exception $e) {
  63. return 'error handled';
  64. });
  65. /*
  66. * Simulate 404, logged to error level
  67. */
  68. $this->assertFalse($app['monolog.handler']->hasErrorRecords());
  69. $request = Request::create('/error');
  70. $app->handle($request);
  71. $records = $app['monolog.handler']->getRecords();
  72. $pattern = "#Symfony\\\\Component\\\\HttpKernel\\\\Exception\\\\NotFoundHttpException: No route found for \"GET /error\" \(uncaught exception\) at .* line \d+#";
  73. $this->assertMatchingRecord($pattern, Logger::ERROR, $app['monolog.handler']);
  74. /*
  75. * Simulate unhandled exception, logged to critical
  76. */
  77. $app->get('/error', function () {
  78. throw new \RuntimeException('very bad error');
  79. });
  80. $this->assertFalse($app['monolog.handler']->hasCriticalRecords());
  81. $request = Request::create('/error');
  82. $app->handle($request);
  83. $pattern = "#RuntimeException: very bad error \(uncaught exception\) at .* line \d+#";
  84. $this->assertMatchingRecord($pattern, Logger::CRITICAL, $app['monolog.handler']);
  85. }
  86. public function testRedirectLogging()
  87. {
  88. $app = $this->getApplication();
  89. $app->get('/foo', function () use ($app) {
  90. return new RedirectResponse('/bar', 302);
  91. });
  92. $this->assertFalse($app['monolog.handler']->hasInfoRecords());
  93. $request = Request::create('/foo');
  94. $app->handle($request);
  95. $this->assertTrue($app['monolog.handler']->hasDebug('< 302 /bar'));
  96. }
  97. public function testErrorLoggingGivesWayToSecurityExceptionHandling()
  98. {
  99. $app = $this->getApplication();
  100. $app['monolog.level'] = Logger::ERROR;
  101. $app->register(new \Silex\Provider\SecurityServiceProvider(), array(
  102. 'security.firewalls' => array(
  103. 'admin' => array(
  104. 'pattern' => '^/admin',
  105. 'http' => true,
  106. 'users' => array(),
  107. ),
  108. ),
  109. ));
  110. $app->get('/admin', function () {
  111. return 'SECURE!';
  112. });
  113. $request = Request::create('/admin');
  114. $app->run($request);
  115. $this->assertEmpty($app['monolog.handler']->getRecords(), 'Expected no logging to occur');
  116. }
  117. public function testStringErrorLevel()
  118. {
  119. $app = $this->getApplication();
  120. $app['monolog.level'] = 'info';
  121. $this->assertSame(Logger::INFO, $app['monolog.handler']->getLevel());
  122. }
  123. /**
  124. * @expectedException \InvalidArgumentException
  125. * @expectedExceptionMessage Provided logging level 'foo' does not exist. Must be a valid monolog logging level.
  126. */
  127. public function testNonExistentStringErrorLevel()
  128. {
  129. $app = $this->getApplication();
  130. $app['monolog.level'] = 'foo';
  131. $app['monolog.handler']->getLevel();
  132. }
  133. public function testDisableListener()
  134. {
  135. $app = $this->getApplication();
  136. unset($app['monolog.listener']);
  137. $app->handle(Request::create('/404'));
  138. $this->assertEmpty($app['monolog.handler']->getRecords(), 'Expected no logging to occur');
  139. }
  140. protected function assertMatchingRecord($pattern, $level, $handler)
  141. {
  142. $found = false;
  143. $records = $handler->getRecords();
  144. foreach ($records as $record) {
  145. if (preg_match($pattern, $record['message']) && $record['level'] == $level) {
  146. $found = true;
  147. continue;
  148. }
  149. }
  150. $this->assertTrue($found, "Trying to find record matching $pattern with level $level");
  151. }
  152. protected function getApplication()
  153. {
  154. $app = new Application();
  155. $app->register(new MonologServiceProvider(), array(
  156. 'monolog.handler' => function () use ($app) {
  157. $level = MonologServiceProvider::translateLevel($app['monolog.level']);
  158. return new TestHandler($level);
  159. },
  160. 'monolog.logfile' => 'php://memory',
  161. ));
  162. return $app;
  163. }
  164. }