Skip to content

Commit

Permalink
Default service visibility for different Symfony versions (#62)
Browse files Browse the repository at this point in the history
* Default service visibility for different Symfony versions

* no message

* test fix
  • Loading branch information
seferov authored Jul 28, 2020
1 parent c8cea86 commit d13b6aa
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
8 changes: 7 additions & 1 deletion src/Symfony/ContainerMeta.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Psalm\SymfonyPsalmPlugin\Symfony;

use Psalm\Exception\ConfigException;
use Symfony\Component\HttpKernel\Kernel;

class ContainerMeta
{
Expand Down Expand Up @@ -79,7 +80,12 @@ private function init(array $containerXmlPaths): void
if (isset($serviceAttributes->alias)) {
$service->setAlias((string) $serviceAttributes->alias);
}
$service->setIsPublic('false' !== (string) $serviceAttributes->public);

if (3 < Kernel::MAJOR_VERSION) {
$service->setIsPublic('true' === (string) $serviceAttributes->public);
} else {
$service->setIsPublic('false' !== (string) $serviceAttributes->public);
}

$this->add($service);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/acceptance/container.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
<service id="Foo\Bar" class="Foo\Bar" public="false"/>
<service id="private_service" class="Foo\Bar" public="false"/>
<service id="public_service_wo_public_attr" class="Foo\Bar"/>
<service id="wronglyNamedService" class="Foo\Bar"/>
<service id="wronglyNamedService" class="Foo\Bar" public="true" />
</services>
</container>
49 changes: 48 additions & 1 deletion tests/unit/Symfony/ContainerMetaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Psalm\Exception\ConfigException;
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
use Psalm\SymfonyPsalmPlugin\Symfony\Service;
use Symfony\Component\HttpKernel\Kernel;

/**
* @testdox ContainerMetaTest
Expand All @@ -28,18 +29,64 @@ public function tearDown()
}

/**
* @testdox service attributes
* @testdox service attributes for > Symfony 3
* @dataProvider publicServices
*/
public function testServices($id, string $className, bool $isPublic)
{
if (3 === Kernel::MAJOR_VERSION) {
$this->markTestSkipped('Should run for > Symfony 3');
}

$service = $this->containerMeta->get($id);
$this->assertInstanceOf(Service::class, $service);
$this->assertSame($className, $service->getClassName());
$this->assertSame($isPublic, $service->isPublic());
}

public function publicServices()
{
return [
[
'id' => 'service_container',
'className' => 'Symfony\Component\DependencyInjection\ContainerInterface',
'isPublic' => true,
],
[
'id' => 'Foo\Bar',
'className' => 'Foo\Bar',
'isPublic' => false,
],
[
'id' => 'Symfony\Component\HttpKernel\HttpKernelInterface',
'className' => 'Symfony\Component\HttpKernel\HttpKernel',
'isPublic' => true,
],
[
'id' => 'public_service_wo_public_attr',
'className' => 'Foo\Bar',
'isPublic' => false,
],
];
}

/**
* @testdox service attributes for Symfony 3
* @dataProvider publicServices3
*/
public function testServices3($id, string $className, bool $isPublic)
{
if (Kernel::MAJOR_VERSION > 3) {
$this->markTestSkipped('Should run for Symfony 3');
}

$service = $this->containerMeta->get($id);
$this->assertInstanceOf(Service::class, $service);
$this->assertSame($className, $service->getClassName());
$this->assertSame($isPublic, $service->isPublic());
}

public function publicServices3()
{
return [
[
Expand Down

0 comments on commit d13b6aa

Please sign in to comment.