<commit_before><?php

/*
 * This file is part of the KGWeinreBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace KG\WeinreBundle\Tests\EventListener;

use KG\WeinreBundle\EventListener\WeinreListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class WeinreListenerTest extends \PHPUnit_Framework_TestCase
{
    public function testSubscribingMethodsActuallyExist()
    {
        $listener = new WeinreListener();

        $methods = WeinreListener::getSubscribedEvents();

        $this->assertGreaterThan(0, $methods);

        foreach ($methods as $method) {
            if (is_array($method)) {
                $method = $method[0];
            }

            $this->assertTrue(method_exists($listener, $method));
        }
    }

    public function testScriptNotAddedForSubRequests()
    {
        $response = new Response($content = '<html><body></body></html>');

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $this->getMockRequest());
        $this->mockGetRequestType($event, HttpKernelInterface::SUB_REQUEST);

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $this->assertEquals($content, $response->getContent());
    }

    public function testScriptAddedToMasterRequest()
    {
        $response = new Response($content = "<html><body></body></html>");

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request = new Request());
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $request->server->set('SERVER_NAME', 'localhost');

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $expected = <<<EOT
<html>
<body>
<script src="localhost/target/target-script-min.js"></script></body>
</html>
EOT;

        $this->assertEquals(
            preg_replace('/\s+/', '', $expected),
            preg_replace('/\s+/', '', $response->getContent())
        );
    }

    public function testConstructorHostAndPortOverrideGuesses()
    {
        $response = new Response($content = '<html><body></body></html>');

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request = new Request());
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $request->server->set('SERVER_NAME', 'localhost');

        $listener = new WeinreListener('127.0.0.1', '8080');
        $listener->onKernelResponse($event);

        $expected = '<html><body><script src="127.0.0.1:8080/target/target-script-min.js"></script></body></html>';

        $this->assertEquals($expected, $response->getContent());
    }

    public function testScriptNotAddedToXmlHttpRequests()
    {
        $response = new Response($content = '{id: 23}');
        $request = $this->getMockRequest();

        $request
            ->expects($this->once())
            ->method('isXmlHttpRequest')
            ->will($this->returnValue(true))
        ;

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request);
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $this->assertEquals($content, $response->getContent());
    }

    /**
     * @return \Symfony\Component\HttpKernel\Event\FilterResponseEvent|PHPUnit_Framework_MockObject_MockObject
     */
    private function getMockEvent()
    {
        return $this
            ->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
            ->disableOriginalConstructor()
            ->getMock()
        ;
    }

    /**
     * @return \Symfony\Component\HttpFoundation\Request|PHPUnit_Framework_MockObject_MockObject
     */
    private function getMockRequest()
    {
        return $this->getMock('Symfony\Component\HttpFoundation\Request');
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject   $mock
     * @param \Symfony\Component\HttpFoundation\Response $response
     */
    private function mockGetResponse(\PHPUnit_Framework_MockObject_MockObject $mock, $response)
    {
        $mock
            ->expects($this->any())
            ->method('getResponse')
            ->will($this->returnValue($response))
        ;
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject  $mock
     * @param \Symfony\Component\HttpFoundation\Request $request
     */
    private function mockGetRequest(\PHPUnit_Framework_MockObject_MockObject $mock, $request)
    {
        $mock
            ->expects($this->any())
            ->method('getRequest')
            ->will($this->returnValue($request))
        ;
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject $mock
     * @param mixed                                    $requestType
     */
    private function mockGetRequestType(\PHPUnit_Framework_MockObject_MockObject $mock, $requestType)
    {
        $mock
            ->expects($this->once())
            ->method('getRequestType')
            ->will($this->returnValue($requestType))
        ;
    }
}
<commit_msg>Shorten WeinreListener tests slightly<commit_after><?php

/*
 * This file is part of the KGWeinreBundle package.
 *
 * (c) <NAME> <<EMAIL>>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace KG\WeinreBundle\Tests\EventListener;

use KG\WeinreBundle\EventListener\WeinreListener;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
 * @author <NAME> <<EMAIL>>
 */
class WeinreListenerTest extends \PHPUnit_Framework_TestCase
{
    public function testSubscribingMethodsActuallyExist()
    {
        $listener = new WeinreListener();

        $methods = WeinreListener::getSubscribedEvents();

        $this->assertGreaterThan(0, $methods);

        foreach ($methods as $method) {
            if (is_array($method)) {
                $method = $method[0];
            }

            $this->assertTrue(method_exists($listener, $method));
        }
    }

    public function testScriptNotAddedForSubRequests()
    {
        $response = new Response($content = '<html><body></body></html>');

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $this->getMockRequest());
        $this->mockGetRequestType($event, HttpKernelInterface::SUB_REQUEST);

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $this->assertEquals($content, $response->getContent());
    }

    public function testScriptAddedToMasterRequest()
    {
        $response = new Response($content = "</body>");

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request = new Request());
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $request->server->set('SERVER_NAME', 'localhost');

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $expected = "<script src=\"localhost/target/target-script-min.js\"></script></body>";

        $this->assertEquals($expected, $response->getContent());
    }

    public function testConstructorHostAndPortOverrideGuesses()
    {
        $response = new Response($content = '</body>');

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request = new Request());
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $request->server->set('SERVER_NAME', 'localhost');

        $listener = new WeinreListener('127.0.0.1', '8080');
        $listener->onKernelResponse($event);

        $expected = '<script src="127.0.0.1:8080/target/target-script-min.js"></script></body>';

        $this->assertEquals($expected, $response->getContent());
    }

    public function testScriptNotAddedToXmlHttpRequests()
    {
        $response = new Response($content = '{id: 23}');
        $request = $this->getMockRequest();

        $request
            ->expects($this->once())
            ->method('isXmlHttpRequest')
            ->will($this->returnValue(true))
        ;

        $event = $this->getMockEvent();
        $this->mockGetResponse($event, $response);
        $this->mockGetRequest($event, $request);
        $this->mockGetRequestType($event, HttpKernelInterface::MASTER_REQUEST);

        $listener = new WeinreListener();
        $listener->onKernelResponse($event);

        $this->assertEquals($content, $response->getContent());
    }

    /**
     * @return \Symfony\Component\HttpKernel\Event\FilterResponseEvent|PHPUnit_Framework_MockObject_MockObject
     */
    private function getMockEvent()
    {
        return $this
            ->getMockBuilder('Symfony\Component\HttpKernel\Event\FilterResponseEvent')
            ->disableOriginalConstructor()
            ->getMock()
        ;
    }

    /**
     * @return \Symfony\Component\HttpFoundation\Request|PHPUnit_Framework_MockObject_MockObject
     */
    private function getMockRequest()
    {
        return $this->getMock('Symfony\Component\HttpFoundation\Request');
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject   $mock
     * @param \Symfony\Component\HttpFoundation\Response $response
     */
    private function mockGetResponse(\PHPUnit_Framework_MockObject_MockObject $mock, $response)
    {
        $mock
            ->expects($this->any())
            ->method('getResponse')
            ->will($this->returnValue($response))
        ;
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject  $mock
     * @param \Symfony\Component\HttpFoundation\Request $request
     */
    private function mockGetRequest(\PHPUnit_Framework_MockObject_MockObject $mock, $request)
    {
        $mock
            ->expects($this->any())
            ->method('getRequest')
            ->will($this->returnValue($request))
        ;
    }

    /**
     * @param \PHPUnit_Framework_MockObject_MockObject $mock
     * @param mixed                                    $requestType
     */
    private function mockGetRequestType(\PHPUnit_Framework_MockObject_MockObject $mock, $requestType)
    {
        $mock
            ->expects($this->once())
            ->method('getRequestType')
            ->will($this->returnValue($requestType))
        ;
    }
}

{'fraction_non_alphanumeric': 0.14395584509591025, 'fraction_numerical': 0.004343105320304018, 'mean_word_length': 2.844521739130435, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 171, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 4, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4544846', 'n_tokens_mistral': 3092, 'n_tokens_neox': 3007, 'n_words': 483}