8000 [DI][DX] Do not map id to class for global classes by ogizanagi · Pull Request #21453 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DI][DX] Do not map id to class for global classes #21453

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
[DI][DX] Do not map id to class for global classes
  • Loading branch information
ogizanagi committed Jan 29, 2017
commit bb870304f098a6b12ebe376e4cd71e2df5868747
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function process(ContainerBuilder $container)
if ($definition instanceof ChildDefinition || $definition->isSynthetic() || null !== $definition->getClass()) {
continue;
}
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)*+$/', $id)) {
if (preg_match('/^[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+(?:\\\\[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*+)++$/', $id)) {
$this->changes[strtolower($id)] = $id;
$definition->setClass($id);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?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\Compiler;

use Symfony\Component\DependencyInjection\Compiler\ResolveClassPass;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Tests\Fixtures\CaseSensitiveClass;

class ResolveClassPassTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider provideValidClassId
*/
public function testResolveClassFromId($serviceId)
{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);

$pass->process($container);

$this->assertSame($serviceId, $def->getClass());
}

public function provideValidClassId()
{
yield array('Acme\UnknownClass');
yield array(CaseSensitiveClass::class);
}

/**
* @dataProvider provideInvalidClassId
*/
public function testWontResolveClassFromId($serviceId)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it a Symfony convention to put the providers before the test method? (I personally tend to put them all as the last ones)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, the Symfyon "convention" is to put the provider just after the test using it.

Copy link
Contributor Author
@ogizanagi ogizanagi Jan 29, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a personal preference. I prefer showing the dataset before the testcase itself, as I find it more readable like this (the test case might be long, so having the dataset right before the method allows to quickly see the tested data, whereas having it after the method may not fit your screen). It's totally arguable.

Should I change that?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is Symfony code, there is personal preference, but consistency with the code base. So, I would indeed change that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fair enough :)
Done.

{
$pass = new ResolveClassPass();
$container = new ContainerBuilder();
$def = $container->register($serviceId);

$pass->process($container);

$this->assertNull($def->getClass());
}

public function provideInvalidClassId()
{
yield array(\stdClass::class);
yield array('bar');
yield array('\DateTime');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -932,16 +932,26 @@ public function testClassFromId()
{
$container = new ContainerBuilder();

$unknown = $container->register('unknown_class');
$class = $container->register(\stdClass::class);
$unknown = $container->register('Acme\UnknownClass');
$autoloadClass = $container->register(CaseSensitiveClass::class);
$container->compile();

$this->assertSame('unknown_class', $unknown->getClass());
$this->assertEquals(\stdClass::class, $class->getClass());
$this->assertSame('Acme\UnknownClass', $unknown->getClass());
$this->assertEquals(CaseSensitiveClass::class, $autoloadClass->getClass());
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "DateTime" has no class.
*/
public function testNoClassFromGlobalNamespaceClassId()
{
$container = new ContainerBuilder();

$definition = $container->register(\DateTime::class);
$container->compile();
}

/**
* @expectedException \Symfony\Component\DependencyInjection\Exception\RuntimeException
* @expectedExceptionMessage The definition for "123_abc" has no class.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ services:

with_defaults_aliased_short: '@with_defaults'

with_shortcut_args: [foo]
Acme\WithShortCutArgs: [foo]
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@ services:
new_factory2: { class: FooBarClass, factory: ['@baz', getClass]}
new_factory3: { class: FooBarClass, factory: [BazClass, getInstance]}
new_factory4: { class: BazClass, factory: [~, getInstance]}
with_shortcut_args: [foo, '@baz']
Acme\WithShortCutArgs: [foo, '@baz']
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ public function testLoadServices()
$this->assertEquals(array(new Reference('baz'), 'getClass'), $services['new_factory2']->getFactory(), '->load() parses the factory tag');
$this->assertEquals(array('BazClass', 'getInstance'), $services['new_factory3']->getFactory(), '->load() parses the factory tag');
$this->assertSame(array(null, 'getInstance'), $services['new_factory4']->getFactory(), '->load() accepts factory tag without class');
$this->assertEquals(array('foo', new Reference('baz')), $services['with_shortcut_args']->getArguments(), '->load() parses short service definition');
$this->assertEquals(array('foo', new Reference('baz')), $services['Acme\WithShortCutArgs']->getArguments(), '->load() parses short service definition');

$aliases = $container->getAliases();
$this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases');
Expand Down Expand Up @@ -385,9 +385,9 @@ public function testDefaults()
$this->assertArrayNotHasKey('public', $container->getDefinition('no_defaults_child')->getChanges());
$this->assertArrayNotHasKey('autowire', $container->getDefinition('no_defaults_child')->getChanges());

$this->assertFalse($container->getDefinition('with_shortcut_args')->isPublic());
$this->assertSame(array('foo' => array(array())), $container->getDefinition('with_shortcut_args')->getTags());
$this->assertTrue($container->getDefinition('with_shortcut_args')->isAutowired());
$this->assertFalse($container->getDefinition('Acme\WithShortCutArgs')->isPublic());
$this->assertSame(array('foo' => array(array())), $container->getDefinition('Acme\WithShortCutArgs')->getTags());
$this->assertTrue($container->getDefinition('Acme\WithShortCutArgs')->isAutowired());

$container->compile();

Expand Down
0