<commit_before><?php

namespace WeiTest
{
    class ResponseTest extends TestCase
    {
        /**
         * @var \Wei\Response
         */
        protected $object;

        public function testFromCreateToSend()
        {
            $response = $this->object;

            // Prepare
            $response->setStatusCode(200);
            $response->setContent('body');
            $response->setHeader(array(
                'Key' => 'Value',
                'Key1' => 'Value1'
            ));
            $response->setCookie('key', 'value');

            // Send
            $output = $this->getOutput($response);

            $this->assertEquals('body', $output);
            $this->assertEquals(200, $response->getStatusCode());
            $this->assertEquals('body', $response->getContent());
            $this->assertTrue($response->isHeaderSent());
            $this->assertEquals('value', $response->getCookie('key'));
        }

        public function testSend()
        {
            $response = $this->object;

            $this->assertFalse($response->isSent());
            $this->assertEquals('content', $this->getOutput($response, 'content', 304));
            $this->assertEquals(304, $response->getStatusCode());
            $this->assertTrue($response->isSent());
        }

        public function testToString()
        {
            $response = $this->object;

            $response->setHeader(array(
                'Key' => 'Value',
                'Key1' => 'Value1'
            ));

            $this->assertEquals("HTTP/1.1 200 OK\r\nKey: Value\r\nKey1: Value1\r\n\r\n", (string)$response);
        }

        public function getOutput(\Wei\Response $response, $content = null, $statusCode = null)
        {
            ob_start();
            // Equals to $response->send($content, $statusCode);
            $response($content, $statusCode);
            return ob_get_clean();
        }

        public function testVersion()
        {
            $response = $this->object;

            $response->setVersion('1.1');
            $this->assertEquals('1.1', $response->getVersion());

            $response->setVersion('1.0');
            $this->assertEquals('1.0', $response->getVersion());
        }

        public function testSetStatusCode()
        {
            $response = $this->object;

            $response->setStatusCode(200, 'Right!');

            $parts = explode("\r\n", $response);

            $this->assertContains('HTTP/1.1 200 Right!', $parts[0]);
        }

        public function testSendHeader()
        {
            $response = $this->object;

            $response->setHeader('key', 'value');

            $this->assertTrue($response->sendHeader());
            $this->assertFalse($response->sendHeader());
        }

        public function testSendStatus()
        {
            $response = $this->object;

            $response->setSentStatus(true);
            $this->assertTrue($response->isSent());

            $response->setSentStatus(false);
            $this->assertFalse($response->isSent());
        }

        public function testDownload()
        {
            ob_start();
            $this->response->download(__DIR__ . '/Fixtures/view.php');
            $content = ob_get_clean();

            $this->assertContains('<?php $this->layout(\'layout.php\') ?>', $content);
        }

        /**
         * @expectedException \RuntimeException
         */
        public function testFileNotFoundException()
        {
            $this->response->download('not found file');
        }

        public function testFlush()
        {
            $this->assertInstanceOf('\Wei\Response', $this->response->flush());

            $this->assertEquals('1', ini_get('implicit_flush'));

            $this->assertEquals(0, ob_get_level());

            /**
             * @link https://github.com/symfony/symfony/issues/2531
             * @link https://github.com/sebastianbergmann/phpunit/issues/390
             */
            if(ob_get_level() === 0) {
                ob_start();
            }
        }

        public function testJson()
        {
            $this->expectOutputString('{"code":-1,"message":"error"}');

            $this->object->json(array('code' => -1, 'message' => 'error'));

            $this->assertEquals('application/json', $this->object->getHeader('Content-Type'));
        }

        public function testJsonp()
        {
            $this->request->set('callback', 'callback');

            $this->expectOutputString('callback({"code":-1,"message":"error"})');

            $this->object->jsonp(array('code' => -1, 'message' => 'error'));

            $this->assertEquals('application/javascript', $this->object->getHeader('Content-Type'));
        }
    }
}

namespace
{
    if (!function_exists('apache_setenv')) {
        function apache_setenv(){}
    }
}<commit_msg>added test for send array as json<commit_after><?php

namespace WeiTest
{
    class ResponseTest extends TestCase
    {
        /**
         * @var \Wei\Response
         */
        protected $object;

        public function testFromCreateToSend()
        {
            $response = $this->object;

            // Prepare
            $response->setStatusCode(200);
            $response->setContent('body');
            $response->setHeader(array(
                'Key' => 'Value',
                'Key1' => 'Value1'
            ));
            $response->setCookie('key', 'value');

            // Send
            $output = $this->getOutput($response);

            $this->assertEquals('body', $output);
            $this->assertEquals(200, $response->getStatusCode());
            $this->assertEquals('body', $response->getContent());
            $this->assertTrue($response->isHeaderSent());
            $this->assertEquals('value', $response->getCookie('key'));
        }

        public function testSend()
        {
            $response = $this->object;

            $this->assertFalse($response->isSent());
            $this->assertEquals('content', $this->getOutput($response, 'content', 304));
            $this->assertEquals(304, $response->getStatusCode());
            $this->assertTrue($response->isSent());
        }

        public function testToString()
        {
            $response = $this->object;

            $response->setHeader(array(
                'Key' => 'Value',
                'Key1' => 'Value1'
            ));

            $this->assertEquals("HTTP/1.1 200 OK\r\nKey: Value\r\nKey1: Value1\r\n\r\n", (string)$response);
        }

        public function getOutput(\Wei\Response $response, $content = null, $statusCode = null)
        {
            ob_start();
            // Equals to $response->send($content, $statusCode);
            $response($content, $statusCode);
            return ob_get_clean();
        }

        public function testVersion()
        {
            $response = $this->object;

            $response->setVersion('1.1');
            $this->assertEquals('1.1', $response->getVersion());

            $response->setVersion('1.0');
            $this->assertEquals('1.0', $response->getVersion());
        }

        public function testSetStatusCode()
        {
            $response = $this->object;

            $response->setStatusCode(200, 'Right!');

            $parts = explode("\r\n", $response);

            $this->assertContains('HTTP/1.1 200 Right!', $parts[0]);
        }

        public function testSendHeader()
        {
            $response = $this->object;

            $response->setHeader('key', 'value');

            $this->assertTrue($response->sendHeader());
            $this->assertFalse($response->sendHeader());
        }

        public function testSendStatus()
        {
            $response = $this->object;

            $response->setSentStatus(true);
            $this->assertTrue($response->isSent());

            $response->setSentStatus(false);
            $this->assertFalse($response->isSent());
        }

        public function testDownload()
        {
            ob_start();
            $this->response->download(__DIR__ . '/Fixtures/view.php');
            $content = ob_get_clean();

            $this->assertContains('<?php $this->layout(\'layout.php\') ?>', $content);
        }

        /**
         * @expectedException \RuntimeException
         */
        public function testFileNotFoundException()
        {
            $this->response->download('not found file');
        }

        public function testFlush()
        {
            $this->assertInstanceOf('\Wei\Response', $this->response->flush());

            $this->assertEquals('1', ini_get('implicit_flush'));

            $this->assertEquals(0, ob_get_level());

            /**
             * @link https://github.com/symfony/symfony/issues/2531
             * @link https://github.com/sebastianbergmann/phpunit/issues/390
             */
            if(ob_get_level() === 0) {
                ob_start();
            }
        }

        public function testJson()
        {
            $this->expectOutputString('{"code":-1,"message":"error"}');

            $this->object->json(array('code' => -1, 'message' => 'error'));

            $this->assertEquals('application/json', $this->object->getHeader('Content-Type'));
        }

        public function testJsonp()
        {
            $this->request->set('callback', 'callback');

            $this->expectOutputString('callback({"code":-1,"message":"error"})');

            $this->object->jsonp(array('code' => -1, 'message' => 'error'));

            $this->assertEquals('application/javascript', $this->object->getHeader('Content-Type'));
        }

        public function testSendArrayAsJson()
        {
            $this->expectOutputString('{"code":1,"message":"success"}');

            $this->object->send(array('code' => 1, 'message' => 'success'));

            $this->assertEquals('application/json', $this->object->getHeader('Content-Type'));
        }
    }
}

namespace
{
    if (!function_exists('apache_setenv')) {
        function apache_setenv(){}
    }
}
{'fraction_non_alphanumeric': 0.16759222530741769, 'fraction_numerical': 0.01090836969456565, 'mean_word_length': 1.8585600907029478, 'pattern_counts': {'":': 10, '<': 7, '<?xml version=': 0, '>': 181, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6250703', 'n_tokens_mistral': 2744, 'n_tokens_neox': 2624, 'n_words': 394}