Skip to content

Commit 37756d3

Browse files
committed
minor symfony#32432 [DependencyInjection] Added tests to cover the possibility of having scalars as services (derrabus)
This PR was merged into the 3.4 branch. Discussion ---------- [DependencyInjection] Added tests to cover the possibility of having scalars as services | Q | A | ------------- | --- | Branch? | 3.4 | Bug fix? | yes | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | symfony#32411 | License | MIT | Doc PR | N/A Commits ------- 60939d9 Added tests to cover the possibility of having scalars as services.
2 parents 5328c4b + 60939d9 commit 37756d3

File tree

4 files changed

+59
-0
lines changed

4 files changed

+59
-0
lines changed

src/Symfony/Component/DependencyInjection/Tests/ContainerBuilderTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use Symfony\Component\DependencyInjection\ServiceLocator;
3939
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;
4040
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
41+
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
4142
use Symfony\Component\DependencyInjection\Tests\Fixtures\SimilarArgumentsDummy;
4243
use Symfony\Component\DependencyInjection\TypedReference;
4344
use Symfony\Component\ExpressionLanguage\Expression;
@@ -1532,6 +1533,20 @@ public function testDecoratedSelfReferenceInvolvingPrivateServices()
15321533

15331534
$this->assertSame(['service_container'], array_keys($container->getDefinitions()));
15341535
}
1536+
1537+
public function testScalarService()
1538+
{
1539+
$c = new ContainerBuilder();
1540+
$c->register('foo', 'string')
1541+
->setPublic(true)
1542+
->setFactory([ScalarFactory::class, 'getSomeValue'])
1543+
;
1544+
1545+
$c->compile();
1546+
1547+
$this->assertTrue($c->has('foo'));
1548+
$this->assertSame('some value', $c->get('foo'));
1549+
}
15351550
}
15361551

15371552
class FooClass

src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ public function testLegacyHas()
386386
$this->assertTrue($sc->has('foo\\baz'), '->has() returns true if a get*Method() is defined');
387387
}
388388

389+
public function testScalarService()
390+
{
391+
$c = new Container();
392+
393+
$c->set('foo', 'some value');
394+
395+
$this->assertTrue($c->has('foo'));
396+
$this->assertSame('some value', $c->get('foo'));
397+
}
398+
389399
public function testInitialized()
390400
{
391401
$sc = new ProjectServiceContainer();

src/Symfony/Component/DependencyInjection/Tests/Dumper/PhpDumperTest.php

+20
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\DependencyInjection\Reference;
3131
use Symfony\Component\DependencyInjection\ServiceLocator;
3232
use Symfony\Component\DependencyInjection\Tests\Fixtures\CustomDefinition;
33+
use Symfony\Component\DependencyInjection\Tests\Fixtures\ScalarFactory;
3334
use Symfony\Component\DependencyInjection\Tests\Fixtures\StubbedTranslator;
3435
use Symfony\Component\DependencyInjection\Tests\Fixtures\TestServiceSubscriber;
3536
use Symfony\Component\DependencyInjection\TypedReference;
@@ -1115,6 +1116,25 @@ public function testReferenceWithLowerCaseId()
11151116

11161117
$this->assertEquals((object) ['foo' => (object) []], $container->get('Bar'));
11171118
}
1119+
1120+
public function testScalarService()
1121+
{
1122+
$container = new ContainerBuilder();
1123+
$container->register('foo', 'string')
1124+
->setPublic(true)
1125+
->setFactory([ScalarFactory::class, 'getSomeValue'])
1126+
;
1127+
1128+
$container->compile();
1129+
1130+
$dumper = new PhpDumper($container);
1131+
eval('?>'.$dumper->dump(['class' => 'Symfony_DI_PhpDumper_Test_Scalar_Service']));
1132+
1133+
$container = new \Symfony_DI_PhpDumper_Test_Scalar_Service();
1134+
1135+
$this->assertTrue($container->has('foo'));
1136+
$this->assertSame('some value', $container->get('foo'));
1137+
}
11181138
}
11191139

11201140
class Rot13EnvVarProcessor implements EnvVarProcessorInterface
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace Symfony\Component\DependencyInjection\Tests\Fixtures;
4+
5+
final class ScalarFactory
6+
{
7+
/**
8+
* @return string
9+
*/
10+
public static function getSomeValue()
11+
{
12+
return 'some value';
13+
}
14+
}

0 commit comments

Comments
 (0)