Exemple #1
0
 /**
  * @dataProvider methodProvider
  */
 public function testHandler($method, $level)
 {
     $handler = new TestHandler();
     $record = $this->getRecord($level, 'test' . $method);
     $this->assertFalse($handler->{'has' . $method}($record), 'has' . $method);
     $this->assertFalse($handler->{'has' . $method . 'ThatContains'}('test'), 'has' . $method . 'ThatContains');
     $this->assertFalse($handler->{'has' . $method . 'ThatPasses'}(function ($rec) {
         return true;
     }), 'has' . $method . 'ThatPasses');
     $this->assertFalse($handler->{'has' . $method . 'ThatMatches'}('/test\\w+/'));
     $this->assertFalse($handler->{'has' . $method . 'Records'}(), 'has' . $method . 'Records');
     $handler->handle($record);
     $this->assertFalse($handler->{'has' . $method}('bar'), 'has' . $method);
     $this->assertTrue($handler->{'has' . $method}($record), 'has' . $method);
     $this->assertTrue($handler->{'has' . $method}('test' . $method), 'has' . $method);
     $this->assertTrue($handler->{'has' . $method . 'ThatContains'}('test'), 'has' . $method . 'ThatContains');
     $this->assertTrue($handler->{'has' . $method . 'ThatPasses'}(function ($rec) {
         return true;
     }), 'has' . $method . 'ThatPasses');
     $this->assertTrue($handler->{'has' . $method . 'ThatMatches'}('/test\\w+/'));
     $this->assertTrue($handler->{'has' . $method . 'Records'}(), 'has' . $method . 'Records');
     $records = $handler->getRecords();
     unset($records[0]['formatted']);
     $this->assertEquals(array($record), $records);
 }
 public function getHandler()
 {
     $processor = new IntrospectionProcessor();
     $handler = new TestHandler();
     $handler->pushProcessor($processor);
     return $handler;
 }
 /**
  * @covers Monolog\Handler\AbstractProcessingHandler::processRecord
  */
 public function testProcessRecord()
 {
     $handler = new TestHandler();
     $handler->pushProcessor(new WebProcessor(array('REQUEST_URI' => '', 'REQUEST_METHOD' => '', 'REMOTE_ADDR' => '')));
     $handler->handle($this->getRecord());
     list($record) = $handler->getRecords();
     $this->assertEquals(3, count($record['extra']));
 }
 public function testLoggedServerWithError()
 {
     $handler = new TestHandler();
     $server = new Server(new Simple\Evaluator(), new Logger('API', array($handler)), Logger::WARNING);
     $server->reply('{"jsonrpc": "2.0", "method": "math/doesNotExist", "id": 123}');
     $this->assertTrue($handler->hasRecordThatContains('Message received: {"jsonrpc": "2.0", "method": "math/doesNotExist", "id": 123}', Logger::WARNING));
     $this->assertTrue($handler->hasRecordThatContains('Sending reply: {"jsonrpc":"2.0","id":123,"error":{"code":-32601,"message":"Method not found"}}', Logger::WARNING));
 }
Exemple #5
0
 public function test_that_query_is_handled_by_pipeline()
 {
     $handler = new UserByEmailHandler();
     $this->queryMap->registerHandlers([UserByEmailQuery::class => $handler]);
     $query = new UserByEmailQuery('*****@*****.**');
     $user = $this->pipeline->fetch($query);
     $this->assertTrue($this->queryMap->hasHandler(UserByEmailQuery::class) && $this->logHandler->hasInfoThatContains(sprintf('Query received {%s}', ClassName::canonical(UserByEmailQuery::class))) && $this->logHandler->hasInfoThatContains(sprintf('Query handled {%s}', ClassName::canonical(UserByEmailQuery::class))) && $user['email'] === '*****@*****.**');
 }
 public function test_that_command_is_executed_by_pipeline()
 {
     $handler = new RegisterUserHandler();
     $this->commandMap->registerHandlers([RegisterUserCommand::class => $handler]);
     $command = new RegisterUserCommand();
     $command->setFirstName('James')->setMiddleName('D')->setLastName('Smith')->setEmail('*****@*****.**')->setPassword('secret');
     $this->pipeline->execute($command);
     $this->assertTrue($this->commandMap->hasHandler(RegisterUserCommand::class) && $this->logHandler->hasInfoThatContains(sprintf('Command received {%s}', ClassName::canonical(RegisterUserCommand::class))) && $this->logHandler->hasInfoThatContains(sprintf('Command handled {%s}', ClassName::canonical(RegisterUserCommand::class))) && $handler->isHandled());
 }
 public function getLogger()
 {
     $logger = new Logger('foo');
     $logger->pushHandler($handler = new TestHandler());
     $logger->pushProcessor(new PsrLogMessageProcessor());
     $handler->setFormatter(new LineFormatter('%level_name% %message%'));
     $this->handler = $handler;
     return $logger;
 }
 /**
  * Get all the logger entries.
  *
  * @return array
  */
 public function getLoggerStrings()
 {
     $records = $this->testLogger->getRecords();
     $return = [];
     foreach ($records as $record) {
         $return[] = $record['message'];
     }
     return $return;
 }
 public function testNon2XXResponsesGetLoggedAsWarning()
 {
     $testHandler = new TestHandler();
     $logger = new Logger('test', [$testHandler]);
     $response = $this->performTestRequest($logger, new Request('GET', 'http://example.com'), new Response(300));
     $this->assertEquals(300, $response->getStatusCode());
     $this->assertCount(2, $testHandler->getRecords());
     $this->assertEquals('HTTP response 300', $testHandler->getRecords()[1]['message']);
     $this->assertEquals('WARNING', $testHandler->getRecords()[1]['level_name']);
 }
 /**
  * This tests that the handler pushes out exceptions
  */
 public function testSesWithLoggerHandler()
 {
     $sesMock = $this->getMock('Aws\\Ses\\SesClient', ['sendEmail'], [], '', false);
     $sesMock->expects($this->once())->method('sendEmail')->will($this->throwException(new SesException()));
     $testHandler = new TestHandler();
     $handlerLogger = new Logger('handler-logger', [$testHandler]);
     $handler = new SesHandler("*****@*****.**", "test", "*****@*****.**", $sesMock);
     $handler->setLogger($handlerLogger);
     $logger = new Logger('test', [$handler]);
     $logger->error('The error');
     $this->assertTrue($testHandler->hasErrorRecords());
 }
 public function testContentBadResponse()
 {
     $twitterOAuth = $this->getMockBuilder('TwitterOAuth\\TwitterOAuth')->disableOriginalConstructor()->getMock();
     $twitterOAuth->expects($this->once())->method('get')->will($this->throwException(new TwitterException()));
     $twitter = new Twitter($twitterOAuth);
     $logHandler = new TestHandler();
     $logger = new Logger('test', array($logHandler));
     $twitter->setLogger($logger);
     $twitter->match('https://twitter.com/DoerteDev/statuses/506522223860277248');
     $this->assertEmpty($twitter->getContent());
     $this->assertTrue($logHandler->hasWarning('Twitter extract failed for: 506522223860277248'), 'Warning message matched');
 }
 public function testHandle()
 {
     $testHandler = new TestHandler();
     $handler = new SamplingHandler($testHandler, 2);
     for ($i = 0; $i < 10000; $i++) {
         $handler->handle($this->getRecord());
     }
     $count = count($testHandler->getRecords());
     // $count should be half of 10k, so between 4k and 6k
     $this->assertLessThan(6000, $count);
     $this->assertGreaterThan(4000, $count);
 }
 public function testImgurFail()
 {
     $imgurClient = $this->getMockBuilder('Imgur\\Client')->disableOriginalConstructor()->getMock();
     $imgurClient->expects($this->any())->method('api')->will($this->throwException(new \Guzzle\Http\Exception\RequestException()));
     $imgur = new Imgur($imgurClient);
     $logHandler = new TestHandler();
     $logger = new Logger('test', array($logHandler));
     $imgur->setLogger($logger);
     $imgur->match('http://imgur.com/gallery/IoKwI7E');
     $this->assertEmpty($imgur->getContent());
     $this->assertTrue($logHandler->hasWarning('Imgur extract failed for: IoKwI7E'), 'Warning message matched');
 }
 public function testHandleError()
 {
     $logger = new Logger('test', array($handler = new TestHandler()));
     $errHandler = new ErrorHandler($logger);
     $errHandler->registerErrorHandler(array(E_USER_NOTICE => Logger::EMERGENCY), false);
     trigger_error('Foo', E_USER_ERROR);
     $this->assertCount(1, $handler->getRecords());
     $this->assertTrue($handler->hasErrorRecords());
     trigger_error('Foo', E_USER_NOTICE);
     $this->assertCount(2, $handler->getRecords());
     $this->assertTrue($handler->hasEmergencyRecords());
 }
Exemple #15
0
 /**
  * This tests that the handler pushes out exceptions
  */
 public function testSnsWithLoggerHandler()
 {
     $snsMock = $this->getMock('Aws\\Sns\\SnsClient', ['publish'], [], '', false);
     $snsMock->expects($this->once())->method('publish')->will($this->throwException(new SnsException('Error', new Command('Command'))));
     $testHandler = new TestHandler();
     $handlerLogger = new Logger('handler-logger', [$testHandler]);
     $handler = new SnsHandler("arn::test", "test", $snsMock);
     $handler->setLogger($handlerLogger);
     $logger = new Logger('test', [$handler]);
     $logger->error('The error');
     $this->assertTrue($testHandler->hasErrorRecords());
 }
 public function testLoggingLevels()
 {
     $test_handler = new TestHandler();
     $this->driver->getLogger()->setHandlers(array($test_handler));
     $levels = array_map('strtoupper', (new \Buttress\Logger\Logger())->getLevels());
     foreach ($levels as $level) {
         $this->driver->log($real_level = constant(\Monolog\Logger::class . "::{$level}"), $message = 'INFO');
         $result = $test_handler->hasRecordThatPasses(function ($record) use($message) {
             return $record['message'] = $message;
         }, $real_level);
         $this->assertTrue($result, "Logger didn't output for {$level}.");
     }
 }
 public function testHandleBufferLimit()
 {
     $test = new TestHandler();
     $handler = new BufferHandler($test, 2);
     $handler->handle($this->getRecord(Logger::DEBUG));
     $handler->handle($this->getRecord(Logger::DEBUG));
     $handler->handle($this->getRecord(Logger::INFO));
     $handler->handle($this->getRecord(Logger::WARNING));
     $handler->close();
     $this->assertTrue($test->hasWarningRecords());
     $this->assertTrue($test->hasInfoRecords());
     $this->assertFalse($test->hasDebugRecords());
 }
 /**
  * @covers Monolog\Handler\GroupHandler::handle
  */
 public function testHandleUsesProcessors()
 {
     $test = new TestHandler();
     $handler = new GroupHandler(array($test));
     $handler->pushProcessor(function ($record) {
         $record['extra']['foo'] = true;
         return $record;
     });
     $handler->handle($this->getRecord(Logger::WARNING));
     $this->assertTrue($test->hasWarningRecords());
     $records = $test->getRecords();
     $this->assertTrue($records[0]['extra']['foo']);
 }
 public function testActivationLevel()
 {
     $test = new TestHandler();
     $app = $this->getApplication();
     $app['monolog.fingerscrossed.handler'] = $test;
     $app['monolog.fingerscrossed.level'] = Logger::WARNING;
     $handler = $app['monolog']->popHandler();
     $handler->handle($this->getRecord(Logger::DEBUG));
     $this->assertFalse($test->hasDebugRecords());
     $handler->handle($this->getRecord(Logger::WARNING));
     $this->assertTrue($test->hasDebugRecords());
     $this->assertTrue($test->hasWarningRecords());
 }
 public function testHandleAnother()
 {
     $test = new TestHandler();
     $handler = new BubbleHandler($test, new \Bubble\CatchBubble(\Bubble\CatchBubble::TIMEOUT));
     $this->assertTrue($handler instanceof AbstractHandler);
     $debug = $this->getRecord(Logger::DEBUG, 'one message');
     $handler->handle($debug);
     $secondSame = $debug;
     $secondSame['message'] = 'another message';
     $secondSame['datetime'] = clone $debug['datetime'];
     $secondSame['datetime']->add(new \DateInterval('PT1S'));
     $handler->handle($secondSame);
     $this->assertCount(2, $test->getRecords());
 }
 /**
  * Test instance with Monolog passed.
  *
  * @throws \WebservicesNl\Common\Exception\Client\InputException
  * @throws \InvalidArgumentException
  * @throws \WebservicesNl\Common\Exception\Server\NoServerAvailableException
  */
 public function testInstanceWithLogger()
 {
     $handler = new TestHandler();
     $logger = new Logger('unit-test', [$handler]);
     $config = new PlatformConfig();
     $factory = SoapFactory::build($config);
     $factory->setLogger($logger);
     $soapClient = $factory->create(['username' => 'johndoe', 'password' => 'fakePassword']);
     static::assertAttributeInstanceOf('\\Psr\\Log\\LoggerInterface', 'logger', $factory);
     static::assertTrue($factory->hasLogger());
     static::assertAttributeInstanceOf('\\Psr\\Log\\LoggerInterface', 'logger', $soapClient);
     static::assertTrue($handler->hasInfoThatContains('Created SoapClient for Webservices'));
     static::assertTrue($handler->hasDebugThatContains('Created SoapClient'));
     static::assertInstanceOf('WebservicesNl\\Protocol\\Soap\\Config\\Platform\\Webservices\\Converter', $soapClient->getConverter());
 }
 /**
  * @dataProvider methodProvider
  */
 public function testHandler($method, $level)
 {
     $handler = new TestHandler();
     $record = $this->getRecord($level, 'test' . $method);
     $this->assertFalse($handler->{'has' . $method}($record));
     $this->assertFalse($handler->{'has' . $method . 'Records'}());
     $handler->handle($record);
     $this->assertFalse($handler->{'has' . $method}('bar'));
     $this->assertTrue($handler->{'has' . $method}($record));
     $this->assertTrue($handler->{'has' . $method}('test' . $method));
     $this->assertTrue($handler->{'has' . $method . 'Records'}());
     $records = $handler->getRecords();
     unset($records[0]['formatted']);
     $this->assertEquals(array($record), $records);
 }
 public function testContentWithException()
 {
     $client = new Client();
     $mock = new Mock([new Response(400, [], Stream::factory('oops'))]);
     $client->getEmitter()->attach($mock);
     $camplus = new Camplus();
     $camplus->setClient($client);
     $logHandler = new TestHandler();
     $logger = new Logger('test', array($logHandler));
     $camplus->setLogger($logger);
     $camplus->match('http://campl.us/rL9Q');
     // this one will catch an exception
     $this->assertEmpty($camplus->getContent());
     $this->assertTrue($logHandler->hasWarning('Camplus extract failed for: rL9Q'), 'Warning message matched');
 }
 public function testContentWithException()
 {
     $client = new Client();
     $mock = new Mock([new Response(400, [], Stream::factory('oops'))]);
     $client->getEmitter()->attach($mock);
     $deviantart = new Deviantart();
     $deviantart->setClient($client);
     $logHandler = new TestHandler();
     $logger = new Logger('test', array($logHandler));
     $deviantart->setLogger($logger);
     $deviantart->match('http://mibreit.deviantart.com/art/A-Piece-of-Heaven-357105002');
     // this one will catch an exception
     $this->assertEmpty($deviantart->getContent());
     $this->assertTrue($logHandler->hasWarning('Deviantart extract failed for: http://mibreit.deviantart.com/art/A-Piece-of-Heaven-357105002'), 'Warning message matched');
 }
 public function testInvokeProxiesToLogger()
 {
     $ex = new \RuntimeException('foo');
     $test = new TestHandler();
     $request = ServerRequestFactory::fromGlobals();
     $response = new Response();
     $logger = new Logger('default', [$test]);
     $error = new ResponseCache($logger);
     $response = $error($ex, $request, $response, function ($request, $response) {
         $response->getBody()->write('complete');
         return $response;
     });
     $this->assertSame('complete', $response->getBody()->__toString());
     $this->assertCount(1, $test->getRecords());
 }
 public function testExpiration()
 {
     $startTime = microtime(true);
     $this->manager->aquire("my_key");
     $duration = microtime(true) - $startTime;
     $this->assertTrue($duration < 2);
     $loggerRecordList = $this->loggerTestHandler->getRecords();
     $this->assertEquals(0, count($loggerRecordList));
     $this->manager->aquire("my_key");
     $duration2 = microtime(true) - $startTime;
     $this->assertTrue($duration2 > 4);
     $loggerRecordList = $this->loggerTestHandler->getRecords();
     $this->assertEquals(1, count($loggerRecordList));
     $record = $loggerRecordList[0];
     $message = $record["message"];
     $this->assertEquals(1, preg_match('/Dead lock detected.+\\/Tests\\/Manager\\/ManagerTest\\.php\\(\\d+\\)/', $message));
 }
 protected function outputLog()
 {
     if ($this->option('debug')) {
         $this->info('Log:');
         foreach ($this->logHandler->getRecords() as $record) {
             $this->line(str_replace(PHP_EOL, '', $record['formatted']));
         }
     }
 }
 public function testKernelException()
 {
     $request = Request::create('/syrup/run', 'POST');
     $request->headers->set('X-StorageApi-Token', SYRUP_SAPI_TEST_TOKEN);
     $message = uniqid();
     $event = new GetResponseForExceptionEvent(self::$kernel, $request, HttpKernelInterface::MASTER_REQUEST, new UserException($message));
     $this->listener->onKernelException($event);
     $records = $this->testLogHandler->getRecords();
     $this->assertCount(1, $records);
     $record = array_pop($records);
     $this->assertArrayHasKey('priority', $record);
     $this->assertEquals('ERROR', $record['priority']);
     $this->assertArrayHasKey('exception', $record);
     $this->assertArrayHasKey('class', $record['exception']);
     $this->assertEquals('Keboola\\Syrup\\Exception\\UserException', $record['exception']['class']);
     $response = $event->getResponse();
     $this->assertEquals(400, $response->getStatusCode());
     $jsonResponse = json_decode($response->getContent(), true);
     $this->assertArrayHasKey('status', $jsonResponse);
     $this->assertEquals('error', $jsonResponse['status']);
     $this->assertArrayHasKey('code', $jsonResponse);
     $this->assertEquals(400, $jsonResponse['code']);
     $this->assertArrayHasKey('exceptionId', $jsonResponse);
     $this->assertArrayHasKey('runId', $jsonResponse);
     $message = uniqid();
     $event = new GetResponseForExceptionEvent(self::$kernel, $request, HttpKernelInterface::MASTER_REQUEST, new ClientException($message));
     $this->listener->onKernelException($event);
     $records = $this->testLogHandler->getRecords();
     $this->assertCount(2, $records);
     $record = array_pop($records);
     $this->assertArrayHasKey('priority', $record);
     $this->assertEquals('CRITICAL', $record['priority']);
     $this->assertArrayHasKey('exception', $record);
     $this->assertArrayHasKey('class', $record['exception']);
     $this->assertEquals('Keboola\\StorageApi\\ClientException', $record['exception']['class']);
     $response = $event->getResponse();
     $this->assertEquals(500, $response->getStatusCode());
     $jsonResponse = json_decode($response->getContent(), true);
     $this->assertArrayHasKey('status', $jsonResponse);
     $this->assertEquals('error', $jsonResponse['status']);
     $this->assertArrayHasKey('code', $jsonResponse);
     $this->assertEquals(500, $jsonResponse['code']);
     $this->assertArrayHasKey('exceptionId', $jsonResponse);
     $this->assertArrayHasKey('runId', $jsonResponse);
     $exception = new UserException(uniqid());
     $exception->setData(['d1' => uniqid(), 'd2' => uniqid()]);
     $event = new GetResponseForExceptionEvent(self::$kernel, $request, HttpKernelInterface::MASTER_REQUEST, $exception);
     $this->listener->onKernelException($event);
     $records = $this->testLogHandler->getRecords();
     $this->assertCount(3, $records);
     $record = array_pop($records);
     $this->assertArrayHasKey('context', $record);
     $this->assertArrayHasKey('data', $record['context']);
     $this->assertArrayHasKey('d1', $record['context']['data']);
     $this->assertArrayHasKey('d2', $record['context']['data']);
 }
 function testRefreshingExistingCache()
 {
     $client = $this->prepare([new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/profile.json')), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/profile.json')), new Response(200, [], file_get_contents(__DIR__ . '/Fixtures/profile.json'))], array_merge($this->conf(), ['cache' => true]));
     $req = Client\Request\Factory::create('profile');
     $result = $client->request(Client\Request\Factory::create('profile'));
     $req = Client\Request\Factory::create('profile');
     $req->setUseCache(false);
     $result = $client->request($req);
     $this->assertTrue($this->loghandler->hasInfoThatContains('Refreshing cache'));
 }
 public function testWarn()
 {
     $context = array("foo" => "bar");
     Simple::warn("hello", $context);
     $this->assertTrue($this->handler->hasWarningRecords());
     foreach ($this->handler->getRecords() as $record) {
         $this->assertEquals("hello", $record['message']);
         $this->assertEquals("WARNING", $record['level_name']);
         $this->assertEquals($context, $record['context']);
     }
 }