8000 [DependencyInjection] XmlFileLoader: enforce tags to have a name by xabbuh · Pull Request #16956 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[DependencyInjection] XmlFileLoader: enforce tags to have a name #16956

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 28, 2016

Conversation

xabbuh
Copy link
Member
@xabbuh xabbuh commented Dec 10, 2015
Q A
Bug fix? yes
New feature? no
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets #16908
License MIT
Doc PR

{
try {
$container = new ContainerBuilder();
$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
Copy link
Contributor

Choose a reason for hiding this comment

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

this should not be inside the try...catch. or you could also use @expectedException and @expectedExceptionMessage directly

Copy link
Member Author

Choose a reason for hiding this comment

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

We cannot can use it here as we are interested in the message of the wrapped exception.

@Tobion
Copy link
Contributor
Tobion commented Dec 11, 2015

Could you change https://github.com/symfony/symfony/blob/master/src/Symfony/Component/DependencyInjection/Tests/Fixtures/yaml/badtag2.yml#L6 to use

tags:
    - { foo: bar }

because it was confusing me. In theory with a hash structure like in

tags:
    foo_tag:   { foo: bar }

foo_tag could be used as name IMO. It's just not implemented like this yet so far.

$loader = new XmlFileLoader($container, new FileLocator(self::$fixturesPath.'/xml'));
$loader->load('invalid_tag.xml');

$this->fail('The XmlFileLoader is expected to throw an exception when the name attribute is missing or a tag.');
Copy link
Contributor

Choose a reason for hiding this comment

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

... missing or a tag.

Should be ... missiong on a tag.?

@stof
Copy link
Member
stof commented Dec 11, 2015

@Tobion this would be a bad idea, because it is perfectly valid to have multiple tags with the same name on a service

$this->fail('The XmlFileLoader is expected to throw an exception when the name attribute is missing or a tag.');
} catch (InvalidArgumentException $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e->getPrevious());
$this->assertRegExp("/The attribute 'name' is required but missing/", $e->getPrevious()->getMessage());
Copy link
Member

Choose a reason for hiding this comment

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

this is asserting an error message coming from the PHP xml validation, right ? This may be a bad idea as it makes the code fragile

Copy link
Member Author

Choose a reason for hiding this comment

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

@stof Can you think of a reason for the XSD validation to fail?

Copy link
Member

Choose a reason for hiding this comment

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

Well, your change is precisely about making the XSD validation fail when the attribute is not there. What I mean is that we should probably avoid relying on the exact wording of the error message in the testsuite (new versions of PHP may alter the message)

Copy link
Member Author

Choose a reason for hiding this comment

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

Oh indeed, that makes sense.

@Tobion
Copy link
Contributor
Tobion commented Dec 11, 2015

@stof it would not be a bad idea because it would be optional and most of the time you don't have the same tag multiple times.

@xabbuh
Copy link
Member Author
xabbuh commented Dec 11, 2015

We should also add a check to ensure that the loading fails when the tag name is present but an empty string.

Status: Needs work

@sstok
Copy link
Contributor
sstok commented Dec 14, 2015

I would say no to

tags:
    foo_tag:   { foo: bar }

It's a new feature that adds nothing special, and introduces an inconsistency.
If you look at the php and xml loader, then yaml would be only one to support this 'strange' format.


$this->fail('The XmlFileLoader is expected to throw an exception when the name attribute is missing or a tag.');
} catch (InvalidArgumentException $e) {
$this->assertInstanceOf('\InvalidArgumentException', $e->getPrevious());
Copy link
Member

Choose a reason for hiding this comment

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

I propose to throw the previous exception here, and use a phpunit annotation instead of all the assertions here

@xabbuh
Copy link
Member Author
xabbuh commented Dec 19, 2015

Tests are updated and I added some more checks for empty tag names.

@@ -185,6 +185,10 @@ private function parseDefinition($id, $service, $file)
$parameters[$name] = SimpleXMLElement::phpize($value);
}

if ('' === trim($tag['name'])) {
throw new \InvalidArgumentException(sprintf('The tag name for service "%s" must not be an empty string in %s.', $id, $file));
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should throw the components InvalidArgumentException

@Tobion
Copy link
Contributor
Tobion commented Dec 21, 2015

Why this strange naming and numbering badtag2.xml and badtag5.xml for fixtures? Please use more descriptive names.

@fabpot
Copy link
Member
fabpot commented Jan 25, 2016

@xabbuh Can you finish this one?

@xabbuh xabbuh force-pushed the issue-16908-2.3 branch 6 times, most recently from a3c45e3 to 1e78f53 Compare January 27, 2016 19:30
@xabbuh
Copy link
Member Author
xabbuh commented Jan 27, 2016

I addressed all comments.

Status: Needs review

@@ -186,6 +186,10 @@ private function parseDefinition($id, $service, $file)
$parameters[$name] = SimpleXMLElement::phpize($value);
}

if ('' === trim($tag['name'])) {
Copy link
Contributor

Choose a reason for hiding this comment

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

IMO the trim should not be done and is also not tested.
Since we do not trim the tag when it is added, it should also not validate with trimming. Otherwise seems inconsistent.

Copy link
Member Author

Choose a reason for hiding this comment

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

fair enough

@Tobion
Copy link
Contributor
Tobion commented Jan 27, 2016

👍
Status: Reviewed

@fabpot
Copy link
Member
fabpot commented Jan 28, 2016

Thank you @xabbuh.

@fabpot fabpot merged commit d86a3a7 into symfony:2.3 Jan 28, 2016
fabpot added a commit that referenced this pull request Jan 28, 2016
…a name (xabbuh)

This PR was merged into the 2.3 branch.

Discussion
----------

[DependencyInjection] XmlFileLoader: enforce tags to have a name

| Q             | A
| ------------- | ---
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets | #16908
| License       | MIT
| Doc PR        |

Commits
-------

d86a3a7 [DependencyInjection] enforce tags to have a name
@xabbuh xabbuh deleted the issue-16908-2.3 branch January 28, 2016 07:28
@fabpot fabpot mentioned this pull request Feb 3, 2016
This was referenced Feb 28, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants
0