Added tests to cover the possibility of having scalars as services.

This commit is contained in:
Alexander M. Turek 2019-07-08 15:34:24 +02:00
parent ddaf1becf3
commit 60939d988d
4 changed files with 59 additions and 0 deletions

View File

@ -38,6 +38,7 @@ use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
use Symfony\Component\DependencyInjection\TypedReference;
use Symfony\Component\ExpressionLanguage\Expression;
@ -1532,6 +1533,20 @@ class ContainerBuilderTest extends TestCase
$this->assertSame(['service_container'], array_keys($container->getDefinitions()));
}
public function testScalarService()
{
$c = new ContainerBuilder();
$c->register('foo', 'string')
->setPublic(true)
->setFactory([ScalarFactory::class, 'getSomeValue'])
;
$c->compile();
$this->assertTrue($c->has('foo'));
$this->assertSame('some value', $c->get('foo'));
}
}
class FooClass

View File

@ -386,6 +386,16 @@ class ContainerTest extends TestCase
$this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
}
public function testScalarService()
{
$c = new Container();
$c->set('foo', 'some value');
$this->assertTrue($c->has('foo'));
$this->assertSame('some value', $c->get('foo'));
}
public function testInitialized()
{
$sc = new ProjectServiceContainer();

View File

@ -30,6 +30,7 @@ use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\DependencyInjection\ServiceLocator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
use Symfony\Component\DependencyInjection\TypedReference;
@ -1115,6 +1116,25 @@ class PhpDumperTest extends TestCase
$this->assertEquals((object) ['foo' => (object) []], $container->get('Bar'));
}
public function testScalarService()
{
$container = new ContainerBuilder();
$container->register('foo', 'string')
->setPublic(true)
->setFactory([ScalarFactory::class, 'getSomeValue'])
;
$container->compile();
$dumper = new PhpDumper($container);
eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Scalar_Service']));
$container = new \Symfony_DI_PhpDumper_Test_Scalar_Service();
$this->assertTrue($container->has('foo'));
$this->assertSame('some value', $container->get('foo'));
}
}
class Rot13EnvVarProcessor implements EnvVarProcessorInterface

View File

@ -0,0 +1,14 @@
<?php
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
final class ScalarFactory
{
/**
* @return string
*/
public static function getSomeValue()
{
return 'some value';
}
}