Skip to content
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

Accessing private services #18

Closed
umpirsky opened this issue Mar 14, 2018 · 15 comments
Closed

Accessing private services #18

umpirsky opened this issue Mar 14, 2018 · 15 comments

Comments

@umpirsky
Copy link
Contributor

This means that in tests accessing private services will not be caught as an error? I find this dangerous.

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

Oh, very true. Can you update the documentation to warn about this?

umpirsky added a commit to umpirsky/symfony-bundle-test that referenced this issue Mar 14, 2018
@umpirsky
Copy link
Contributor Author

Done #19.

Is there a way to detect if service is private and skip it in the smoke test?

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

Not sure what you mean. You decide yourself what services you will "force make" public. And you decide what services you want to test.

@umpirsky
Copy link
Contributor Author

Yes, but when you write smoke test, like this for example https://gist.github.com/Maff-/c9d2c1592a746da85d5b, you iterate Symfony private services and vendors private services (e.g. argument_metadata_factory, annotation_reader... a number of them). For all this services $container->has($id) is true, but $container->get($id) throws deprecation that service is private and you should not access it (Symfony 3.4).

I don't see elegant solution for this.

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

@umpirsky
Copy link
Contributor Author

It's not generic. I have a smoke test that iterates $container->getServiceIds().

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

Hm... So you want to test AppBundle. Or all services in the kernel. And you just want to instantiate the services, right?

@umpirsky
Copy link
Contributor Author

umpirsky commented Mar 14, 2018

Yes, all public services in the kernel. But I get private too, and can't distinguish them.

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

For that you just need to build the container, right?

Upon building the container Symfony will try to resolve all dependencies.

@umpirsky umpirsky changed the title Accessing private srvices Accessing private services Mar 14, 2018
@umpirsky
Copy link
Contributor Author

The test is as simple as:

class GenericServicesTest extends TestCase
{
    public function testServiceInstantiation()
    {
        foreach ($container->getServiceIds() as $serviceId) {
            $this->assertNotNull($client->getContainer()->get($serviceId));
        }
    }
}

Container is already built and everything works fine until you enable deprecations.

Then you get:

Remaining deprecation triggered by GenericServicesTest::testServiceInstantiation:
    The "argument_metadata_factory" service is private, getting it from the container is deprecated since Symfony 3.2 and will fail in 4.0. You should either make the service public, or stop using the container directly and use dependency injection instead.

I was trying with:

class GenericServicesTest extends TestCase
{
    public function testServiceInstantiation()
    {
        foreach ($container->getServiceIds() as $serviceId) {
+            if (!$container->has($serviceId)) {
+                continue;
+            }
+            
            $this->assertNotNull($client->getContainer()->get($serviceId));
        }
    }
}

But has() returns true to private services as well.

@Nyholm
Copy link
Member

Nyholm commented Mar 14, 2018

Interesting.
I just looked at the source. Maybe you could create your own container that extends Container. That would allow you to extract all public services and aliases.

@umpirsky
Copy link
Contributor Author

Yes, I was thinking same, do that only in test env.

@umpirsky
Copy link
Contributor Author

Fixed with:

-        foreach ($container->getServiceIds() as $serviceId) {
+        foreach (array_diff($container->getServiceIds(), array_keys($container->getRemovedIds())) as $serviceId) {

@Nyholm
Copy link
Member

Nyholm commented Mar 15, 2018

Great!

@Nyholm
Copy link
Member

Nyholm commented Mar 26, 2018

On other news: symfony/symfony#26499

@Nyholm Nyholm closed this as completed Mar 26, 2018
Nyholm pushed a commit that referenced this issue Feb 2, 2019
* Fixes issue #18

* Update Readme.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants