Skip to content

Commit

Permalink
minor #32432 [DependencyInjection] Added tests to cover the possibili…
Browse files Browse the repository at this point in the history
…ty 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 | #32411
| License       | MIT
| Doc PR        | N/A

Commits
-------

60939d9 Added tests to cover the possibility of having scalars as services.
  • Loading branch information
fabpot committed Jul 9, 2019
2 parents 5328c4b + 60939d9 commit 37756d3
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 0 deletions.
Expand Up @@ -38,6 +38,7 @@
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;
Expand Down Expand Up @@ -1532,6 +1533,20 @@ public function testDecoratedSelfReferenceInvolvingPrivateServices()

$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
Expand Down
10 changes: 10 additions & 0 deletions src/Symfony/Component/DependencyInjection/Tests/ContainerTest.php
Expand Up @@ -386,6 +386,16 @@ public function testLegacyHas()
$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();
Expand Down
Expand Up @@ -30,6 +30,7 @@
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;
Expand Down Expand Up @@ -1115,6 +1116,25 @@ public function testReferenceWithLowerCaseId()

$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
Expand Down
@@ -0,0 +1,14 @@
<?php

namespace Symfony\Component\DependencyInjection\Tests\Fixtures;

final class ScalarFactory
{
/**
* @return string
*/
public static function getSomeValue()
{
return 'some value';
}
}

0 comments on commit 37756d3

Please sign in to comment.