<commit_before><?php
/**
 * @package syrup
 * @copyright 2015 Keboola
 */

namespace Keboola\Syrup\Tests\Monolog\Processor;

use Keboola\StorageApi\Client;
use Keboola\StorageApi\Event;
use Keboola\Syrup\Monolog\Handler\StorageApiHandler;
use Monolog\Logger;
use Symfony\Component\HttpFoundation\Request;
use Keboola\Syrup\Service\StorageApi\StorageApiService;
use Keboola\Syrup\Test\Monolog\TestCase;
use Symfony\Component\HttpFoundation\RequestStack;

class StorageApiHandlerTest extends TestCase
{
    /**
     * @covers \Keboola\Syrup\Monolog\Handler\StorageApiHandler::__construct
     * @covers \Keboola\Syrup\Monolog\Handler\StorageApiHandler::handle
     */
    public function testHandler()
    {
        $storageApiService = new StorageApiService();
        $handler = new StorageApiHandler(SYRUP_APP_NAME, $storageApiService);
        $record = $this->getRecord(Logger::INFO, 'infoMessage');
        $handler->handle($record);

        $request = new Request();
        $request->headers->add(['x-storageapi-token' => SYRUP_SAPI_TEST_TOKEN]);
        $requestStack = new RequestStack();
        $requestStack->push($request);

        $storageApiService = new StorageApiService('https://connection.keboola.com', $requestStack);
        $client = $storageApiService->getClient();
        $events = $client->listEvents(['q' => 'message: infoMessage + runId:' . $client->getRunId()]);
        // nothing is logged, because SAPI client was not initialized
        $this->assertEquals(0, count($events));

        $handler = new StorageApiHandler(SYRUP_APP_NAME, $storageApiService);
        $record = $this->getRecord(Logger::INFO, 'infoMessage', ['exceptionId' => '123', 'job' => '345']);
        $record['component'] = 'fooBar';
        $record['http'] = 'fooBaz';
        $handler->handle($record);
        // wait for elastic search to update
        sleep(2);
        $events = $client->listEvents(['q' => 'message: infoMessage + runId:' . $client->getRunId()]);
        $this->assertEquals(1, count($events));
        $event = $events[0];

        $this->assertArrayHasKey('component', $event);
        $this->assertEquals('fooBar', $event['component']);
        $this->assertArrayHasKey('id', $event);
        $this->assertArrayHasKey('event', $event);
        $this->assertArrayHasKey('message', $event);
        $this->assertArrayHasKey('description', $event);
        $this->assertArrayHasKey('type', $event);
        $this->assertArrayHasKey('runId', $event);
        $this->assertEquals($client->getRunId(), $event['runId']);
        $this->assertArrayHasKey('results', $event);
        $this->assertArrayHasKey('exceptionId', $event['results']);
        $this->assertArrayHasKey('job', $event['results']);
        $this->assertArrayHasKey('params', $event);<commit_msg>fix: handler test<commit_after><?php
/**
 * @package syrup
 * @copyright 2015 Keboola
 */

namespace Keboola\Syrup\Tests\Monolog\Processor;

use Keboola\StorageApi\Client;
use Keboola\StorageApi\Event;
use Keboola\Syrup\Monolog\Handler\StorageApiHandler;
use Monolog\Logger;
use Symfony\Component\HttpFoundation\Request;
use Keboola\Syrup\Service\StorageApi\StorageApiService;
use Keboola\Syrup\Test\Monolog\TestCase;
use Symfony\Component\HttpFoundation\RequestStack;

class StorageApiHandlerTest extends TestCase
{
    /**
     * @covers \Keboola\Syrup\Monolog\Handler\StorageApiHandler::__construct
     * @covers \Keboola\Syrup\Monolog\Handler\StorageApiHandler::handle
     */
    public function testHandler()
    {
        $storageApiService = new StorageApiService();
        $handler = new StorageApiHandler(SYRUP_APP_NAME, $storageApiService);
        $record = $this->getRecord(Logger::INFO, 'infoMessage');
        $handler->handle($record);

        $request = new Request();
        $request->headers->add(['x-storageapi-token' => SYRUP_SAPI_TEST_TOKEN]);
        $requestStack = new RequestStack();
        $requestStack->push($request);

        $storageApiService = new StorageApiService('https://connection.keboola.com', $requestStack);
        $client = $storageApiService->getClient();
        $client->setRunId(uniqid());
        $events = $client->listEvents(['q' => 'message: infoMessage + runId:' . $client->getRunId()]);
        // nothing is logged, because SAPI client was not initialized
        $this->assertEquals(0, count($events));

        $handler = new StorageApiHandler(SYRUP_APP_NAME, $storageApiService);
        $record = $this->getRecord(Logger::INFO, 'infoMessage', ['exceptionId' => '123', 'job' => '345']);
        $record['component'] = 'fooBar';
        $record['http'] = 'fooBaz';
        $handler->handle($record);
        // wait for elastic search to update
        sleep(2);
        $events = $client->listEvents(['q' => 'message: infoMessage + runId:' . $client->getRunId()]);
        $this->assertEquals(1, count($events));
        $event = $events[0];

        $this->assertArrayHasKey('component', $event);
        $this->assertEquals('fooBar', $event['component']);
        $this->assertArrayHasKey('id', $event);
        $this->assertArrayHasKey('event', $event);
        $this->assertArrayHasKey('message', $event);
        $this->assertArrayHasKey('description', $event);
        $this->assertArrayHasKey('type', $event);
        $this->assertArrayHasKey('runId', $event);
        $this->assertEquals($client->getRunId(), $event['runId']);
        $this->assertArrayHasKey('results', $event);
        $this->assertArrayHasKey('exceptionId', $event['results']);
        $this->assertArrayHasKey('job', $event['results']);
        $this->assertArrayHasKey('params', $event);
{'fraction_non_alphanumeric': 0.16462003915287418, 'fraction_numerical': 0.004983093077059975, 'mean_word_length': 3.8157669237360756, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 70, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7058226', 'n_tokens_mistral': 1683, 'n_tokens_neox': 1670, 'n_words': 276}