<commit_before><?php

/**
 *
 * This file is part of the Apix Project.
 *
 * (c) <NAME> <franck at ouarz.net>
 *
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
 *
 */

namespace Apix\Log\tests\Logger;

use Psr\Log\Test\LoggerInterfaceTest;

abstract class TestCase extends LoggerInterfaceTest
{

    protected function _normalizeLogs($logs)
    {
        $normalize = function ($log) {
            return preg_replace_callback(
                '{^\[.+\] (\w+) (.+)?}',
                function ($match) {
                    return strtolower($match[1]) . ' '
                    . (
                        isset($match[2]) ? $match[2] : null
                    );
                },
                $log);
        };

        return array_map($normalize, $logs);
    }

    /**
     * This must return the log messages in order with a simple formatting: "<LOG LEVEL> <MESSAGE>"
     *
     * Example ->error('Foo') would yield "error Foo"
     *
     * @return string[]
     */
    public function getLogs()
    {
        return $this->_normalizeLogs(file($this->dest, FILE_IGNORE_NEW_LINES));
    }

    /**
     * @dataProvider providerMsgContextes
     */
    public function testMessageWithContext($msg, $context, $exp)
    {
        $this->getLogger()->alert('{'.$msg.'}', array( $msg => $context));

        $this->assertEquals(array('alert ' . $exp), $this->getLogs());
    }

    public function providerMsgContextes()
    {
        $obj = new \stdClass();
        $obj->baz = 'biz';
        $obj->nested = new \stdClass();
        $obj->nested->buz ='bez';

        return array(
            array('null', null, ''),
            array('bool1', true, '[bool: 1]'),
            array('bool2', false, '[bool: 0]'),
            array('string', 'Foo', 'Foo'),
            array('int', 0, '0'),
            array('float', 0.5, '0.5'),
            array('resource', fopen('php://memory', 'r'), '[type: resource]'),

            // objects
            array('obj__toString', new DummyTest(), '__toString!'),
            array('obj_stdClass', new \stdClass(), '{}'),
            array('obj_instance', $obj, '{"baz":"biz","nested":{"buz":"bez"}}'),
            
            // nested arrays...
            array('nested_values', array('foo','bar'), '["foo","bar"]'),
            array('nested_asso', array('foo'=>1,'bar'=>'2'), '{"foo":1,"bar":"2"}'),
            array('nested_object', array(new DummyTest), '[{"foo":"bar"}]'),
            array('nested_unicode', array('ƃol-xᴉdɐ'), '["\u0183ol-x\u1d09d\u0250"]'),
        );
    }

    /**
     * @dataProvider providerContextesOnly
     */
    public function testContextOnly($key, $context, $exp)
    {
        $this->getLogger()->debug($context);

        $this->assertEquals(array('debug ' . $exp), $this->getLogs());
    }

    public function providerContextesOnly()
    {
        $obj = new \stdClass();
        $obj->baz = 'biz';
        $obj->nested = new \stdClass();
        $obj->nested->buz ='bez';

        return array(
            array('null', null, ''),

            // objects
            array('obj__toString', new DummyTest(), '__toString!'),
            array('obj_stdClass', new \stdClass(), '{}'),
            array('obj_instance', $obj, '{"baz":"biz","nested":{"buz":"bez"}}'),
            
            // nested arrays...
            array('nested_values', array('foo','bar'), '["foo","bar"]'),
            array('nested_asso', array('foo'=>1,'bar'=>'2'), '{"foo":1,"bar":"2"}'),
            array('nested_object', array(new DummyTest), '[{"foo":"bar"}]'),
            array('nested_unicode', array('ƃol-xᴉdɐ'), '["\u0183ol-x\u1d09d\u0250"]')
        );
    }

    public function testContextIsAnException()
    {
        $this->getLogger()->critical( new \Exception('Boo!') );
        
        $this->assertStringStartsWith(
            "critical exception 'Exception' with message 'Boo!' in ",
            $this->getLogs()[0]
        );
    }

}

class DummyTest
{
    public $foo = 'bar';
    protected $foo2 = 'bar2';
    public function __toString()
    {
        return '__toString!';
    }
}<commit_msg>Fixing a PHP 5.3 specific syntax error.<commit_after><?php

/**
 *
 * This file is part of the Apix Project.
 *
 * (c) <NAME> <franck at ouarz.net>
 *
 * @license     http://opensource.org/licenses/BSD-3-Clause  New BSD License
 *
 */

namespace Apix\Log\tests\Logger;

use Psr\Log\Test\LoggerInterfaceTest;

abstract class TestCase extends LoggerInterfaceTest
{

    protected function _normalizeLogs($logs)
    {
        $normalize = function ($log) {
            return preg_replace_callback(
                '{^\[.+\] (\w+) (.+)?}',
                function ($match) {
                    return strtolower($match[1]) . ' '
                    . (
                        isset($match[2]) ? $match[2] : null
                    );
                },
                $log);
        };

        return array_map($normalize, $logs);
    }

    /**
     * This must return the log messages in order with a simple formatting: "<LOG LEVEL> <MESSAGE>"
     *
     * Example ->error('Foo') would yield "error Foo"
     *
     * @return string[]
     */
    public function getLogs()
    {
        return $this->_normalizeLogs(file($this->dest, FILE_IGNORE_NEW_LINES));
    }

    /**
     * @dataProvider providerMsgContextes
     */
    public function testMessageWithContext($msg, $context, $exp)
    {
        $this->getLogger()->alert('{'.$msg.'}', array( $msg => $context));

        $this->assertEquals(array('alert ' . $exp), $this->getLogs());
    }

    public function providerMsgContextes()
    {
        $obj = new \stdClass();
        $obj->baz = 'biz';
        $obj->nested = new \stdClass();
        $obj->nested->buz ='bez';

        return array(
            array('null', null, ''),
            array('bool1', true, '[bool: 1]'),
            array('bool2', false, '[bool: 0]'),
            array('string', 'Foo', 'Foo'),
            array('int', 0, '0'),
            array('float', 0.5, '0.5'),
            array('resource', fopen('php://memory', 'r'), '[type: resource]'),

            // objects
            array('obj__toString', new DummyTest(), '__toString!'),
            array('obj_stdClass', new \stdClass(), '{}'),
            array('obj_instance', $obj, '{"baz":"biz","nested":{"buz":"bez"}}'),
            
            // nested arrays...
            array('nested_values', array('foo','bar'), '["foo","bar"]'),
            array('nested_asso', array('foo'=>1,'bar'=>'2'), '{"foo":1,"bar":"2"}'),
            array('nested_object', array(new DummyTest), '[{"foo":"bar"}]'),
            array('nested_unicode', array('ƃol-xᴉdɐ'), '["\u0183ol-x\u1d09d\u0250"]'),
        );
    }

    /**
     * @dataProvider providerContextesOnly
     */
    public function testContextOnly($key, $context, $exp)
    {
        $this->getLogger()->debug($context);

        $this->assertEquals(array('debug ' . $exp), $this->getLogs());
    }

    public function providerContextesOnly()
    {
        $obj = new \stdClass();
        $obj->baz = 'biz';
        $obj->nested = new \stdClass();
        $obj->nested->buz ='bez';

        return array(
            array('null', null, ''),

            // objects
            array('obj__toString', new DummyTest(), '__toString!'),
            array('obj_stdClass', new \stdClass(), '{}'),
            array('obj_instance', $obj, '{"baz":"biz","nested":{"buz":"bez"}}'),
            
            // nested arrays...
            array('nested_values', array('foo','bar'), '["foo","bar"]'),
            array('nested_asso', array('foo'=>1,'bar'=>'2'), '{"foo":1,"bar":"2"}'),
            array('nested_object', array(new DummyTest), '[{"foo":"bar"}]'),
            array('nested_unicode', array('ƃol-xᴉdɐ'), '["\u0183ol-x\u1d09d\u0250"]')
        );
    }

    public function testContextIsAnException()
    {
        $this->getLogger()->critical( new \Exception('Boo!') );
        
        $logs = $this->getLogs();
        $this->assertStringStartsWith(
            "critical exception 'Exception' with message 'Boo!' in ",
            $logs[0]
        );
    }

}

class DummyTest
{
    public $foo = 'bar';
    protected $foo2 = 'bar2';
    public function __toString()
    {
        return '__toString!';
    }
}
{'fraction_non_alphanumeric': 0.1966461575582097, 'fraction_numerical': 0.01158161418747738, 'mean_word_length': 2.084077380952381, 'pattern_counts': {'":': 24, '<': 13, '<?xml version=': 0, '>': 67, 'https://': 0, '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': '1749440', 'n_tokens_mistral': 2623, 'n_tokens_neox': 2532, 'n_words': 484}