<commit_before><?php


namespace Silktide\Syringe\Tests;


use PHPUnit\Framework\TestCase;
use Pimple\Container;
use Silktide\Syringe\Exception\ConfigException;
use Silktide\Syringe\ReferenceResolver;
use Silktide\Syringe\Token;

class ReferenceResolverTest extends TestCase
{
    const EXAMPLE_TEST = "example_result";

    /**
     * @var Container
     */
    protected $container;

    /**
     * @var ReferenceResolver
     */
    protected $referenceResolver;

    public function setUp()
    {
        $this->container = new Container();
        $this->referenceResolver = new ReferenceResolver();
    }

    public function testRegularConstantResolve()
    {
        define("foo", "bar");
        
        $value = $this->referenceResolver->resolve(
            $this->container,
            Token::CONSTANT_CHAR . "foo". Token::CONSTANT_CHAR
        );
        $this->assertEquals("bar", $value);
    }

    public function testClassConstantResolve()
    {
        $value = $this->referenceResolver->resolve(
            $this->container,
            Token::CONSTANT_CHAR . "Silktide\\Syringe\\Tests\\ReferenceResolverTest::EXAMPLE_TEST". Token::CONSTANT_CHAR
        );
        $this->assertEquals("example_result", $value);
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testConstantNonExistentResolve()
    {
        $this->referenceResolver->resolve($this->container,Token::CONSTANT_CHAR . "bar". Token::CONSTANT_CHAR);
    }

    public function testEnvironmentResolve()
    {
        $this->setEnvVar("banana", "salad");
        $value = $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "banana" . Token::ENV_CHAR);
        $this->assertEquals("salad", $value);
    }


    public function testFalseEnvironmentResolve()
    {
        $this->setEnvVar("chips", false);
        $value = $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "chips" . Token::ENV_CHAR);
        $this->assertEquals(false, $value);
        // Herein lies the rub, environment variables do not understant the concept of true or false, so it'll change it
        // to in this case, ''
        $this->assertSame('', $value);
    }


    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testFailedEnvironmentResolve()
    {
        $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "chicken" . Token::ENV_CHAR);
    }

    public function testParameterResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $value = $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
        $this->assertEquals("parameter_value", $value);
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testFailedParameterResolve()
    {
        $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
    }

    public function testRecursiveParameterResolve()
    {
        $this->container["parameter_key"] = function(){return Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR;};
        $this->container["parameter_key_2"] = function(){return "parameter_value";};
        $value = $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
        $this->assertEquals("parameter_value", $value);
    }

    public function testParameterArrayResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $this->container["parameter_key_2"] = function(){return "parameter_value_2";};
        $array = $this->referenceResolver->resolveArray($this->container, [
            "foo" => Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR,
            "bar" => Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR
        ]);
        $this->assertSame(["foo" => "parameter_value", "bar" => "parameter_value_2"], $array);
    }

    public function testRecursiveParameterArrayResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $this->container["parameter_key_2"] = function(){return "parameter_value_2";};
        $array = $this->referenceResolver->resolveArray($this->container, [
            "foo" => [
                Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR,
                Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR
            ]
        ]);

        $this->assertSame([
            "foo" => [
                "parameter_value",
                "parameter_value_2"
            ]
        ], $array);
    }

    public function testResolveNull()
    {
        $this->container["parameter_key"] = function(){return null;};
        $array = $this->referenceResolver->resolveArray($this->container, [
            Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR
        ]);

        $this->assertSame([
            null,
        ], $array);
    }


    public function testReferencedParameterArray()
    {
        $this->container["options"] = function(){ return ["foo" => "bar"]; };
        $array = $this->referenceResolver->resolve($this->container, "%options%");
        $this->assertSame([
            "foo" => "bar"
        ], $array);
    }

    public function testReferencedParameterArrayResolve()
    {
        $this->container["options"] = function(){ return ["foo" => "%bar%"]; };
        $this->container["bar"] = "chicken";
        $array = $this->referenceResolver->resolve($this->container, "%options%");
        $this->assertSame([
            "foo" => "chicken"
        ], $array);
    }

    public function parameterEscapedResolveProvider()
    {
        return [
            [
                "%my_key_1%",
                "my_value_1"
            ],
            [
                "%my_key_1%50%%",
                "my_value_150%"<commit_msg>added additional tests for parameter resolution<commit_after><?php


namespace Silktide\Syringe\Tests;


use PHPUnit\Framework\TestCase;
use Pimple\Container;
use Silktide\Syringe\Exception\ConfigException;
use Silktide\Syringe\ReferenceResolver;
use Silktide\Syringe\Token;

class ReferenceResolverTest extends TestCase
{
    const EXAMPLE_TEST = "example_result";

    /**
     * @var Container
     */
    protected $container;

    /**
     * @var ReferenceResolver
     */
    protected $referenceResolver;

    public function setUp()
    {
        $this->container = new Container();
        $this->referenceResolver = new ReferenceResolver();
    }

    public function testRegularConstantResolve()
    {
        define("foo", "bar");
        
        $value = $this->referenceResolver->resolve(
            $this->container,
            Token::CONSTANT_CHAR . "foo". Token::CONSTANT_CHAR
        );
        $this->assertEquals("bar", $value);
    }

    public function testClassConstantResolve()
    {
        $value = $this->referenceResolver->resolve(
            $this->container,
            Token::CONSTANT_CHAR . "Silktide\\Syringe\\Tests\\ReferenceResolverTest::EXAMPLE_TEST". Token::CONSTANT_CHAR
        );
        $this->assertEquals("example_result", $value);
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testConstantNonExistentResolve()
    {
        $this->referenceResolver->resolve($this->container,Token::CONSTANT_CHAR . "bar". Token::CONSTANT_CHAR);
    }

    public function testEnvironmentResolve()
    {
        $this->setEnvVar("banana", "salad");
        $value = $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "banana" . Token::ENV_CHAR);
        $this->assertEquals("salad", $value);
    }


    public function testFalseEnvironmentResolve()
    {
        $this->setEnvVar("chips", false);
        $value = $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "chips" . Token::ENV_CHAR);
        $this->assertEquals(false, $value);
        // Herein lies the rub, environment variables do not understant the concept of true or false, so it'll change it
        // to in this case, ''
        $this->assertSame('', $value);
    }


    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testFailedEnvironmentResolve()
    {
        $this->referenceResolver->resolve($this->container, Token::ENV_CHAR . "chicken" . Token::ENV_CHAR);
    }

    public function testParameterResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $value = $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
        $this->assertEquals("parameter_value", $value);
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testFailedParameterResolve()
    {
        $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
    }

    public function testRecursiveParameterResolve()
    {
        $this->container["parameter_key"] = function(){return Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR;};
        $this->container["parameter_key_2"] = function(){return "parameter_value";};
        $value = $this->referenceResolver->resolve($this->container, Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR);
        $this->assertEquals("parameter_value", $value);
    }

    public function testParameterArrayResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $this->container["parameter_key_2"] = function(){return "parameter_value_2";};
        $array = $this->referenceResolver->resolveArray($this->container, [
            "foo" => Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR,
            "bar" => Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR
        ]);
        $this->assertSame(["foo" => "parameter_value", "bar" => "parameter_value_2"], $array);
    }

    public function testRecursiveParameterArrayResolve()
    {
        $this->container["parameter_key"] = function(){return "parameter_value";};
        $this->container["parameter_key_2"] = function(){return "parameter_value_2";};
        $array = $this->referenceResolver->resolveArray($this->container, [
            "foo" => [
                Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR,
                Token::PARAMETER_CHAR . "parameter_key_2" . Token::PARAMETER_CHAR
            ]
        ]);

        $this->assertSame([
            "foo" => [
                "parameter_value",
                "parameter_value_2"
            ]
        ], $array);
    }

    public function testResolveNull()
    {
        $this->container["parameter_key"] = function(){return null;};
        $array = $this->referenceResolver->resolveArray($this->container, [
            Token::PARAMETER_CHAR . "parameter_key" . Token::PARAMETER_CHAR
        ]);

        $this->assertSame([
            null,
        ], $array);
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testArrayConcatenationFailure()
    {
        $this->container["parameter_key"] = function(){ return ["foo", "bar"]; };
        $this->referenceResolver->resolve($this->container, "foo%parameter_key%");
    }

    /**
     * @expectedException \Silktide\Syringe\Exception\ConfigException
     */
    public function testNullConcatenationFailure()
    {
        $this->container["parameter_key"] = function(){ return null; };
        $this->referenceResolver->resolve($this->container, "foo%parameter_key%");
    }

    public function testReferencedParameterArray()
    {
        $this->container["options"] = function(){ return ["foo" => "bar"]; };
        $array = $this->referenceResolver->resolve($this->container, "%options%");
        $this->assertSame([
            "foo" => "bar"
        ], $array);
    }

    public function testReferencedParameterArrayResolve()
    {
        $this->container["options"] = function(){ return ["foo" => "%bar%"]; };
        $this->container["bar"] = "chicken";
        $array = $this->referenceResolver->resolve($this->container, "%options%");
        $this->assertSame([
            "foo" => "chicken"
        ], $array);
    }

    public function parameterEscapedResolveProvider()
    {
        return [
            [
                "%my_key_1%",
                "my_value_1"
            ],
            [
                "%my_key_1%50%%",
                "my_value_150%"
{'fraction_non_alphanumeric': 0.14755756067205975, 'fraction_numerical': 0.0028002489110143126, 'mean_word_length': 2.806098283007697, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 169, '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': '223053', 'n_tokens_mistral': 3535, 'n_tokens_neox': 3547, 'n_words': 563}