Classes List

Symfony\Component\DependencyInjection\Tests\Loader\IniFileLoaderTest

 1 
 2 
 3 
 4 
 5 
 6 
 7 
 8 
 9 
 10 
 11 
 12 
 13 
 14 
 15 
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 
 24 
 25 
 26 
 27 
 28 
 29 
 30 
 31 
 32 
 33 
 34 
 35 
 36 
 37 
 38 
 39 
 40 
 41 
 42 
 43 
 44 
 45 
 46 
 47 
 48 
 49 
 50 
 51 
 52 
 53 
 54 
 55 
 56 
 57 
 58 
 59 
 60 
 61 
 62 
 63 
 64 
 65 
 66 
 67 
 68 
 69 
 70 
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
 88 
 89 
 90 
 91 
 92 
 93 
 94 
 95 
 96 
 97 
 98 
 99 
 100 
 101 
 102 
 103 
 104 
 105 
 106 
 107 
 108 
 109 
 110 
 111 
 112 
 113 
 114 
 115 
 116 
 117 
 118 
 119 
 120 
 121 
 122 
 123 
 124 
 125 
 126 
 127 
 128 
 129 
 130 
 131 
 132 
 133 
 134 
<?php

/* * This file is part of the Symfony package. * * (c) Fabien Potencier <fabien@symfony.com> * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */


namespace Symfony\Component\DependencyInjection\Tests\Loader;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader\IniFileLoader;
use Symfony\Component\Config\FileLocator;

class IniFileLoaderTest extends TestCase
{
    protected $container;
    protected $loader;

    protected function setUp()
    {
        $this->container = new ContainerBuilder();
        $this->loader = new IniFileLoader($this->containernew FileLocator(realpath(__DIR__.'/../Fixtures/').'/ini'));
    }

    public function testIniFileCanBeLoaded()
    {
        $this->loader->load('parameters.ini');
        $this->assertEquals(array('foo' => 'bar''bar' => '%foo%'), $this->container->getParameterBag()->all(), '->load() takes a single file name as its first argument');
    }

    /** * @dataProvider getTypeConversions */
    public function testTypeConversions($key$value$supported)
    {
        $this->loader->load('types.ini');
        $parameters = $this->container->getParameterBag()->all();
        $this->assertSame($value$parameters[$key], '->load() converts values to PHP types');
    }

    /** * @dataProvider getTypeConversions * @requires PHP 5.6.1 * This test illustrates where our conversions differs from INI_SCANNER_TYPED introduced in PHP 5.6.1 */
    public function testTypeConversionsWithNativePhp($key$value$supported)
    {
        if (defined('HHVM_VERSION_ID')) {
            $this->markTestSkipped();
        }

        if (!$supported) {
            $this->markTestSkipped(sprintf('Converting the value "%s" to "%s" is not supported by the IniFileLoader.'$key$value));
        }

        $this->loader->load('types.ini');
        $expected = parse_ini_file(__DIR__.'/../Fixtures/ini/types.ini'trueINI_SCANNER_TYPED);
        $this->assertSame($value$expected['parameters'][$key], '->load() converts values to PHP types');
    }

    public function getTypeConversions()
    {
        return array(
            array('true_comment'truetrue),
            array('true'truetrue),
            array('false'falsetrue),
            array('on'truetrue),
            array('off'falsetrue),
            array('yes'truetrue),
            array('no'falsetrue),
            array('none'falsetrue),
            array('null'nulltrue),
            array('constant'PHP_VERSIONtrue),
            array('12'12true),
            array('12_string''12'true),
            array('12_comment'12true),
            array('12_string_comment''12'true),
            array('12_string_comment_again''12'true),
            array('-12', -12true),
            array('1'1true),
            array('0'0true),
            array('0b0110'bindec('0b0110'), false), // not supported by INI_SCANNER_TYPED
            array('11112222333344445555''1111,2222,3333,4444,5555'true),
            array('0777'0777false), // not supported by INI_SCANNER_TYPED
            array('255'0xFFfalse), // not supported by INI_SCANNER_TYPED
            array('100.0'1e2false), // not supported by INI_SCANNER_TYPED
            array('-120.0', -1.2E2false), // not supported by INI_SCANNER_TYPED
            array('-10100.1', -10100.1false), // not supported by INI_SCANNER_TYPED
            array('-10,100.1''-10,100.1'true),
        );
    }

    /** * @expectedException \InvalidArgumentException * @expectedExceptionMessage The file "foo.ini" does not exist (in: */
    public function testExceptionIsRaisedWhenIniFileDoesNotExist()
    {
        $this->loader->load('foo.ini');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage The "nonvalid.ini" file is not valid. */
    public function testExceptionIsRaisedWhenIniFileCannotBeParsed()
    {
        @$this->loader->load('nonvalid.ini');
    }

    /** * @expectedException \Symfony\Component\DependencyInjection\Exception\InvalidArgumentException * @expectedExceptionMessage The "almostvalid.ini" file is not valid. */
    public function testExceptionIsRaisedWhenIniFileIsAlmostValid()
    {
        @$this->loader->load('almostvalid.ini');
    }

    public function testSupports()
    {
        $loader = new IniFileLoader(new ContainerBuilder(), new FileLocator());

        $this->assertTrue($loader->supports('foo.ini'), '->supports() returns true if the resource is loadable');
        $this->assertFalse($loader->supports('foo.foo'), '->supports() returns false if the resource is not loadable');
        $this->assertTrue($loader->supports('with_wrong_ext.yml''ini'), '->supports() returns true if the resource with forced type is loadable');
    }
}