Skip to content

Commit d13b6aa

Browse files
authored
Default service visibility for different Symfony versions (#62)
* Default service visibility for different Symfony versions * no message * test fix
1 parent c8cea86 commit d13b6aa

File tree

3 files changed

+56
-3
lines changed

3 files changed

+56
-3
lines changed

src/Symfony/ContainerMeta.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Psalm\SymfonyPsalmPlugin\Symfony;
66

77
use Psalm\Exception\ConfigException;
8+
use Symfony\Component\HttpKernel\Kernel;
89

910
class ContainerMeta
1011
{
@@ -79,7 +80,12 @@ private function init(array $containerXmlPaths): void
7980
if (isset($serviceAttributes->alias)) {
8081
$service->setAlias((string) $serviceAttributes->alias);
8182
}
82-
$service->setIsPublic('false' !== (string) $serviceAttributes->public);
83+
84+
if (3 < Kernel::MAJOR_VERSION) {
85+
$service->setIsPublic('true' === (string) $serviceAttributes->public);
86+
} else {
87+
$service->setIsPublic('false' !== (string) $serviceAttributes->public);
88+
}
8389

8490
$this->add($service);
8591
}

tests/acceptance/container.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@
1717
<service id="Foo\Bar" class="Foo\Bar" public="false"/>
1818
<service id="private_service" class="Foo\Bar" public="false"/>
1919
<service id="public_service_wo_public_attr" class="Foo\Bar"/>
20-
<service id="wronglyNamedService" class="Foo\Bar"/>
20+
<service id="wronglyNamedService" class="Foo\Bar" public="true" />
2121
</services>
2222
</container>

tests/unit/Symfony/ContainerMetaTest.php

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Psalm\Exception\ConfigException;
77
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
88
use Psalm\SymfonyPsalmPlugin\Symfony\Service;
9+
use Symfony\Component\HttpKernel\Kernel;
910

1011
/**
1112
* @testdox ContainerMetaTest
@@ -28,18 +29,64 @@ public function tearDown()
2829
}
2930

3031
/**
31-
* @testdox service attributes
32+
* @testdox service attributes for > Symfony 3
3233
* @dataProvider publicServices
3334
*/
3435
public function testServices($id, string $className, bool $isPublic)
3536
{
37+
if (3 === Kernel::MAJOR_VERSION) {
38+
$this->markTestSkipped('Should run for > Symfony 3');
39+
}
40+
3641
$service = $this->containerMeta->get($id);
3742
$this->assertInstanceOf(Service::class, $service);
3843
$this->assertSame($className, $service->getClassName());
3944
$this->assertSame($isPublic, $service->isPublic());
4045
}
4146

4247
public function publicServices()
48+
{
49+
return [
50+
[
51+
'id' => 'service_container',
52+
'className' => 'Symfony\Component\DependencyInjection\ContainerInterface',
53+
'isPublic' => true,
54+
],
55+
[
56+
'id' => 'Foo\Bar',
57+
'className' => 'Foo\Bar',
58+
'isPublic' => false,
59+
],
60+
[
61+
'id' => 'Symfony\Component\HttpKernel\HttpKernelInterface',
62+
'className' => 'Symfony\Component\HttpKernel\HttpKernel',
63+
'isPublic' => true,
64+
],
65+
[
66+
'id' => 'public_service_wo_public_attr',
67+
'className' => 'Foo\Bar',
68+
'isPublic' => false,
69+
],
70+
];
71+
}
72+
73+
/**
74+
* @testdox service attributes for Symfony 3
75+
* @dataProvider publicServices3
76+
*/
77+
public function testServices3($id, string $className, bool $isPublic)
78+
{
79+
if (Kernel::MAJOR_VERSION > 3) {
80+
$this->markTestSkipped('Should run for Symfony 3');
81+
}
82+
83+
$service = $this->containerMeta->get($id);
84+
$this->assertInstanceOf(Service::class, $service);
85+
$this->assertSame($className, $service->getClassName());
86+
$this->assertSame($isPublic, $service->isPublic());
87+
}
88+
89+
public function publicServices3()
4390
{
4491
return [
4592
[

0 commit comments

Comments
 (0)