<commit_before><?php

namespace Http\FactoryTest;

use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ServerRequestInterface;

class ServerRequestFactoryTest extends TestCase
{
    /** @var  ServerRequestInterface */
    private $factory;

    public function setUp()
    {
        $factoryClass = SERVER_REQUEST_FACTORY;
        $this->factory = new $factoryClass();
    }

    private function assertServerRequest($request, $method, $uri)
    {
        $this->assertInstanceOf(ServerRequestInterface::class, $request);
        $this->assertSame($method, $request->getMethod());
        $this->assertSame($uri, (string) $request->getUri());
    }

    public function dataMethods()
    {
        return [
            ['GET'],
            ['POST'],
            ['PUT'],
            ['DELETE'],
            ['OPTIONS'],
            ['HEAD'],
        ];
    }

    /**
     * @dataProvider dataMethods
     */
    public function testCreateServerRequest($method)
    {
        $uri = 'http://example.com/';

        $request = $this->factory->createServerRequest($method, $uri);

        $this->assertServerRequest($request, $method, $uri);
    }

    public function testCreateServerRequestWithUri()
    {
        $factoryClass = URI_FACTORY;
        $uriFactory = new $factoryClass();

        $method = 'GET';
        $uri = 'http://example.com/';

        $request = $this->factory->createServerRequest($method, $uriFactory->createUri($uri));

        $this->assertServerRequest($request, $method, $uri);
    }

    /**
     * @runInSeparateProcess
     * @preserveGlobalState disabled
     */
    public function testCreateServerRequestFromGlobals()
    {
        $_SERVER['REQUEST_METHOD'] = $method = 'GET';
        $_SERVER['REQUEST_URI'] = $path = '/test';
        $_SERVER['QUERY_STRING'] = $qs = 'foo=1&bar=true';
        $_SERVER['HTTP_HOST'] = $host = 'example.org';

        $uri = "http://{$host}{$path}?$qs";

        $request = $this->factory->createServerRequestFromGlobals();

        $this->assertServerRequest($request, $method, $uri);
    }
}<commit_msg>Test modified server request factory<commit_after><?php

namespace Http\FactoryTest;

use PHPUnit_Framework_TestCase as TestCase;
use Psr\Http\Message\ServerRequestInterface;

class ServerRequestFactoryTest extends TestCase
{
    /** @var  ServerRequestInterface */
    private $factory;

    public function setUp()
    {
        $factoryClass = SERVER_REQUEST_FACTORY;
        $this->factory = new $factoryClass();
    }

    private function assertServerRequest($request, $method, $uri)
    {
        $this->assertInstanceOf(ServerRequestInterface::class, $request);
        $this->assertSame($method, $request->getMethod());
        $this->assertSame($uri, (string) $request->getUri());
    }

    public function dataMethods()
    {
        return [
            ['GET'],
            ['POST'],
            ['PUT'],
            ['DELETE'],
            ['OPTIONS'],
            ['HEAD'],
        ];
    }

    public function dataServer()
    {
        $data = [];

        foreach ($this->dataMethods() as $methodData) {
            $data[] = [
                [
                    'REQUEST_METHOD' => $methodData[0],
                    'REQUEST_URI' => '/test',
                    'QUERY_STRING' => 'foo=1&bar=true',
                    'HTTP_HOST' => 'example.org',
                ]
            ];
        }

        return $data;
    }

    /**
     * @dataProvider dataServer
     */
    public function testCreateServerRequest($server)
    {
        $method = $server['REQUEST_METHOD'];
        $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";

        $request = $this->factory->createServerRequest($server);

        $this->assertServerRequest($request, $method, $uri);
    }

    /**
     * @dataProvider dataServer
     */
    public function testCreateServerRequestWithOverridenMethod($server)
    {
        $method = 'OPTIONS';
        $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";

        $request = $this->factory->createServerRequest($server, $method);

        $this->assertServerRequest($request, $method, $uri);
    }

    /**
     * @dataProvider dataServer
     */
    public function testCreateServerRequestWithOverridenUri($server)
    {
        $method = $server['REQUEST_METHOD'];
        $uri = "https://example.com/foobar?bar=2&foo=false";

        $request = $this->factory->createServerRequest($server, null, $uri);

        $this->assertServerRequest($request, $method, $uri);
    }

    /**
     * @dataProvider dataServer
     */
    public function testCreateServerRequestWithUriObject($server)
    {
        $factoryClass = URI_FACTORY;
        $uriFactory = new $factoryClass();

        $method = $server['REQUEST_METHOD'];
        $uri = "http://{$server['HTTP_HOST']}{$server['REQUEST_URI']}?{$server['QUERY_STRING']}";

        $request = $this->factory->createServerRequest(array(), $method, $uriFactory->createUri($uri));

        $this->assertServerRequest($request, $method, $uri);
    }
}
{'fraction_non_alphanumeric': 0.15672079827822344, 'fraction_numerical': 0.000782625709254549, 'mean_word_length': 2.5524669909659488, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 43, 'https://': 1, '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': '950626', 'n_tokens_mistral': 1476, 'n_tokens_neox': 1430, 'n_words': 239}