<commit_before><?php

use Enlighten\Http\Request;
use Enlighten\Http\Response;
use Enlighten\Routing\RoutingContext;

function sampleFunction($nullMeBro, $bogusParam = 'abc', Request $request = null)
{
    return $request->getRequestUri();
}

class RoutingContextTest extends PHPUnit_Framework_TestCase
{
    public function testRegisterAndPassObjectInstanceWithClosure()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = function ($bogusParam = '123', Request $fillMe = null, Response $response = null) {
            return $fillMe->getRequestUri();
        };

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            null,
            $request,
            null
        ];

        // NB: Unfortunately, default values (e.g. "123" in the example above) in combination with closures are not
        // supported because of the way ReflectionParameter is implemented in PHP . Should that ever change, this test
        // will break.

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }

    public function testFunctionByStringWithDefaultValues()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = 'sampleFunction';

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            null,
            'abc',
            $request
        ];

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }

    public function testFunctionByArrayWithDefaultValues()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = [$this, 'sampleFunction'];

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            null,
            'abc',
            $request
        ];

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }<commit_msg>[#17] RoutingContextTest: HHVM supports determining default values via reflection for closures, adjust tests accordingly<commit_after><?php

use Enlighten\Http\Request;
use Enlighten\Http\Response;
use Enlighten\Routing\RoutingContext;

function sampleFunction($nullMeBro, $bogusParam = 'abc', Request $request = null)
{
    return $request->getRequestUri();
}

class RoutingContextTest extends PHPUnit_Framework_TestCase
{
    /**
     * Determines whether this version of PHP is capable of determining default parameter values for closures via
     * reflection.
     *
     * @return bool
     */
    public function shouldSupportClosureDefaultValues()
    {
        if (defined('HHVM_VERSION')) {
            // Running HHVM; this should work
            return true;
        }

        // Confirmed not to work in PHP 5.5
        // Confirmed not to work in PHP 5.6
        // Confirmed not to work in PHP 7.0.0-dev nightly (tested 18 jul)
        return false;
    }

    public function testInjectionWithClosureFunc()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = function ($bogusParam = '123', Request $fillMe = null, Response $response = null) {
            return $fillMe->getRequestUri();
        };

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            $this->shouldSupportClosureDefaultValues() ? '123' : null,
            $request,
            null
        ];

        // NB: Unfortunately, default values (e.g. "123" in the example above) in combination with closures are not
        // supported because of the way ReflectionParameter is implemented in PHP. Should that ever change, this test
        // will break (see "shouldSupportClosureDefaultValues"). HHVM does implement this.

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }

    public function testInjectionWithStringFunc()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = 'sampleFunction';

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            null,
            'abc',
            $request
        ];

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }

    public function testInjectionWithArrayFunc()
    {
        $request = new Request();
        $request->setRequestUri('/hello');

        $myFunction = [$this, 'sampleFunction'];

        $context = new RoutingContext();
        $context->registerInstance($request);

        $paramList = $context->determineValues($myFunction);

        $expectedParams = [
            null,
            'abc',
            $request
        ];

        $this->assertEquals($expectedParams, $paramList);
        $this->assertEquals('/hello', call_user_func_array($myFunction, $paramList));
    }
{'fraction_non_alphanumeric': 0.11827007943512798, 'fraction_numerical': 0.004589585172109444, 'mean_word_length': 2.6181353767560664, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 38, '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': '3207273', 'n_tokens_mistral': 1509, 'n_tokens_neox': 1440, 'n_words': 353}