<commit_before><?php

namespace jtreminio\Container\Tests;

use jtreminio\Container\Container;

class ContainerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Override service container
     *
     * @param string $name    Name of service to replace: "\Namespace\Component\UserIdentity"
     * @param mixed  $service Value to replace service with - usually a PHPUnit mock object
     * @param bool   $final   Whether to mark service as final, prevent overrides
     * @return self
     */
    protected final function setService($name, $service, $final = true)
    {
        Container::set($name, $service, $final);

        return $this;
    }

    public function testGetReturnsService()
    {
        $uniqId = uniqid();
        $string = 'it works!';

        $this->setService($uniqId, $string);

        $this->assertEquals(
            $string,
            Container::get($uniqId),
            "Expected \$container::get() to return {$string}"
        );
    }

    public function testGetReturnsServiceWithConstructorInjection()
    {
        $date = '2000-01-01';

        /** @var $date \DateTime */
        $date = Container::get('\DateTime', array($date));

        $expectedResult = '946706400';

        $this->assertEquals(
            $expectedResult,
            $date->getTimestamp(),
            'Returned timestamp did not match expected'
        );
    }

    public function testGetReturnsInstantiatedObjectWhenNotADefinedService()
    {
        $stdClass = Container::get('\stdClass');

        $this->assertInstanceOf(
            '\stdClass',
            $stdClass,
            'Expected result to be instance of stdClass'
        );
    }

    public function testGetReturnsInstantiatedObjectWithConstructorParametersWhenNotADefinedService()
    {
        /** @var $date \DateTime */
        $date = Container::get('\DateTime', array('2000-01-01'));

        $expectedResult = '946706400';

        $this->assertEquals(
            $expectedResult,
            $date->getTimestamp(),
            'Returned timestamp did not match expected'
        );
    }

    public function testGetReturnsNewObjectsWhenParametersAreDifferent()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        /** @var $date2 \DateTime */
        $date2 = Container::get('\DateTime', array('2005-01-01'));

        /** @var $date3 \DateTime */
        $date3 = Container::get('\DateTime', array('2013-01-01'));

        $expectedResult1 = '946706400';
        $expectedResult2 = '1104559200';
        $expectedResult3 = '1357020000';

        $this->assertEquals(
            $expectedResult1,
            $date1->getTimestamp(),
            'Returned timestamp did not match expected for date1'
        );

        $this->assertEquals(
            $expectedResult2,
            $date2->getTimestamp(),
            'Returned timestamp did not match expected for date2'
        );

        $this->assertEquals(
            $expectedResult3,
            $date3->getTimestamp(),
            'Returned timestamp did not match expected for date3'
        );
    }

    public function testSetOverridesExistingServiceWithNew()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        $dateOverride = new \DateTime('2005-01-01');

        /** @var $date2 \DateTime */
        $this->setService('\DateTime', $dateOverride);

        $date2 = Container::get('\DateTime');

        $expectedResult1 = '946706400';
        $expectedResult2 = '1104559200';

        $this->assertEquals(
            $expectedResult1,
            $date1->getTimestamp(),
            'Returned timestamp did not match expected for date1'
        );

        $this->assertEquals(
            $expectedResult2,
            $date2->getTimestamp(),
            'Returned timestamp did not match expected for date2'
        );
    }

    public function testSetPreventsOverrideOfExistingServiceWhenFlagged()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        $dateOverride = new \DateTime('2005-01-01');

        /** @var $date2 \DateTime */
        Container::set('\DateTime', $dateOverride, true);

        $date2 = Container::get('\DateTime');

        $date3 = Container::get('\DateTime', array('2000-01-01'));

        $this->assertSame(
            $date2,
            $date3,
            'Expecting ::set() to prevent overriding existing service when flagged to prevent overrides'
        );
    }

    public function testGetReturnsPredefinedServiceFromPimple()
    {
        $this->assertInstanceOf(
            '\DateTime',
            Container::get('FooBarDateTime'),
            'Expecting ::get() to return a pre-defined Pimple service'
        );
    }
}<commit_msg>Added ::tearDown(); ::testGetReturnsPredefinedServiceFromPimple() no longer depends on bootstrap; ::testGetReturnsService() tightened up<commit_after><?php

namespace jtreminio\Container\Tests;

use jtreminio\Container\Container;

class ContainerTest extends \PHPUnit_Framework_TestCase
{
    /**
     * Override service container
     *
     * @param string $name    Name of service to replace: "\Namespace\Component\UserIdentity"
     * @param mixed  $service Value to replace service with - usually a PHPUnit mock object
     * @param bool   $final   Whether to mark service as final, prevent overrides
     * @return self
     */
    protected final function setService($name, $service, $final = true)
    {
        Container::set($name, $service, $final);

        return $this;
    }

    public function tearDown()
    {
        Container::clear();
    }

    public function testGetReturnsService()
    {
        $uniqId = uniqid();
        $string = 'it works!';

        $this->setService($uniqId, $string);

        $this->assertEquals(
            $string,
            Container::get($uniqId),
            "Expected \$container::get() to return {$string}"
        );
    }

    public function testGetReturnsServiceWithConstructorInjection()
    {
        $date = '2000-01-01';

        /** @var $date \DateTime */
        $date = Container::get('\DateTime', array($date));

        $expectedResult = '946706400';

        $this->assertEquals(
            $expectedResult,
            $date->getTimestamp(),
            'Returned timestamp did not match expected'
        );
    }

    public function testGetReturnsInstantiatedObjectWhenNotADefinedService()
    {
        $stdClass = Container::get('\stdClass');

        $this->assertInstanceOf(
            '\stdClass',
            $stdClass,
            'Expected result to be instance of stdClass'
        );
    }

    public function testGetReturnsInstantiatedObjectWithConstructorParametersWhenNotADefinedService()
    {
        /** @var $date \DateTime */
        $date = Container::get('\DateTime', array('2000-01-01'));

        $expectedResult = '946706400';

        $this->assertEquals(
            $expectedResult,
            $date->getTimestamp(),
            'Returned timestamp did not match expected'
        );
    }

    public function testGetReturnsNewObjectsWhenParametersAreDifferent()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        /** @var $date2 \DateTime */
        $date2 = Container::get('\DateTime', array('2005-01-01'));

        /** @var $date3 \DateTime */
        $date3 = Container::get('\DateTime', array('2013-01-01'));

        $expectedResult1 = '946706400';
        $expectedResult2 = '1104559200';
        $expectedResult3 = '1357020000';

        $this->assertEquals(
            $expectedResult1,
            $date1->getTimestamp(),
            'Returned timestamp did not match expected for date1'
        );

        $this->assertEquals(
            $expectedResult2,
            $date2->getTimestamp(),
            'Returned timestamp did not match expected for date2'
        );

        $this->assertEquals(
            $expectedResult3,
            $date3->getTimestamp(),
            'Returned timestamp did not match expected for date3'
        );
    }

    public function testSetOverridesExistingServiceWithNew()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        $dateOverride = new \DateTime();

        /** @var $date2 \DateTime */
        $this->setService('\DateTime', $dateOverride);

        $date2 = Container::get('\DateTime');

        $expectedResult1 = '946706400';

        $this->assertEquals(
            $expectedResult1,
            $date1->getTimestamp(),
            'Returned timestamp did not match expected for date1'
        );

        $this->assertEquals(
            $dateOverride->getTimestamp(),
            $date2->getTimestamp(),
            'Returned timestamp did not match expected for date2'
        );
    }

    public function testSetPreventsOverrideOfExistingServiceWhenFlagged()
    {
        /** @var $date1 \DateTime */
        $date1 = Container::get('\DateTime', array('2000-01-01'));

        $dateOverride = new \DateTime('2005-01-01');

        /** @var $date2 \DateTime */
        Container::set('\DateTime', $dateOverride, true);

        $date2 = Container::get('\DateTime');

        $date3 = Container::get('\DateTime', array('2000-01-01'));

        $this->assertSame(
            $date2,
            $date3,
            'Expecting ::set() to prevent overriding existing service when flagged to prevent overrides'
        );
    }

    public function testGetReturnsPredefinedServiceFromPimple()
    {
        Container::clear();

        $pimple = new \Pimple;

        $pimple['FooBarDateTime'] = function () {
            return new \DateTime();
        };

        Container::setPimple($pimple);

        $this->assertInstanceOf(
            '\DateTime',
            Container::get('FooBarDateTime'),
            'Expecting ::get() to return a pre-defined Pimple service'
        );
    }
}
{'fraction_non_alphanumeric': 0.1288782816229117, 'fraction_numerical': 0.034407319013524264, 'mean_word_length': 2.1856192587899903, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, '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': '4223493', 'n_tokens_mistral': 2876, 'n_tokens_neox': 2649, 'n_words': 604}