2020-03-10 17:00:55 +01:00
|
|
|
<?php
|
|
|
|
|
2020-03-13 15:51:18 +01:00
|
|
|
namespace Psalm\SymfonyPsalmPlugin\Tests\Symfony;
|
2020-03-10 17:00:55 +01:00
|
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
use Psalm\Exception\ConfigException;
|
2020-03-13 15:51:18 +01:00
|
|
|
use Psalm\SymfonyPsalmPlugin\Symfony\ContainerMeta;
|
2021-08-22 07:27:44 +02:00
|
|
|
use Symfony\Component\DependencyInjection\Definition;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\ParameterNotFoundException;
|
|
|
|
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
2020-07-28 20:21:03 +02:00
|
|
|
use Symfony\Component\HttpKernel\Kernel;
|
2020-03-10 17:00:55 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox ContainerMetaTest
|
|
|
|
*/
|
|
|
|
class ContainerMetaTest extends TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var ContainerMeta
|
|
|
|
*/
|
|
|
|
private $containerMeta;
|
|
|
|
|
2020-12-08 10:45:11 +01:00
|
|
|
public function setUp(): void
|
2020-03-10 17:00:55 +01:00
|
|
|
{
|
2020-06-19 15:18:31 +02:00
|
|
|
$this->containerMeta = new ContainerMeta([__DIR__.'/../../acceptance/container.xml']);
|
2020-03-10 17:00:55 +01:00
|
|
|
}
|
|
|
|
|
2020-12-08 10:45:11 +01:00
|
|
|
public function tearDown(): void
|
2020-03-10 17:00:55 +01:00
|
|
|
{
|
|
|
|
unset($this->containerMeta);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2021-08-22 07:27:44 +02:00
|
|
|
* @testdox service attributes
|
2020-03-10 17:00:55 +01:00
|
|
|
* @dataProvider publicServices
|
|
|
|
*/
|
|
|
|
public function testServices($id, string $className, bool $isPublic)
|
|
|
|
{
|
2021-08-22 07:27:44 +02:00
|
|
|
$serviceDefinition = $this->containerMeta->get($id);
|
|
|
|
$this->assertInstanceOf(Definition::class, $serviceDefinition);
|
|
|
|
$this->assertSame($className, $serviceDefinition->getClass());
|
|
|
|
$this->assertSame($isPublic, $serviceDefinition->isPublic());
|
2020-03-10 17:00:55 +01:00
|
|
|
}
|
|
|
|
|
2021-08-22 07:27:44 +02:00
|
|
|
public function publicServices(): iterable
|
2020-07-28 20:21:03 +02:00
|
|
|
{
|
2021-08-22 07:27:44 +02:00
|
|
|
yield [
|
|
|
|
'id' => 'service_container',
|
|
|
|
'className' => 'Symfony\Component\DependencyInjection\ContainerInterface',
|
|
|
|
'isPublic' => true,
|
2020-07-28 20:21:03 +02:00
|
|
|
];
|
2021-08-22 07:27:44 +02:00
|
|
|
yield [
|
|
|
|
'id' => 'Foo\Bar',
|
|
|
|
'className' => 'Foo\Bar',
|
|
|
|
'isPublic' => false,
|
|
|
|
];
|
|
|
|
yield [
|
|
|
|
'id' => 'public_service_wo_public_attr',
|
|
|
|
'className' => 'Foo\Bar',
|
|
|
|
'isPublic' => Kernel::MAJOR_VERSION < 5,
|
|
|
|
];
|
|
|
|
yield [
|
|
|
|
'id' => 'doctrine.orm.entity_manager',
|
|
|
|
'className' => 'Doctrine\ORM\EntityManager',
|
|
|
|
'isPublic' => true,
|
2020-03-10 17:00:55 +01:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox with non existent xml file
|
|
|
|
*/
|
|
|
|
public function testInvalidFile()
|
|
|
|
{
|
|
|
|
$this->expectException(ConfigException::class);
|
2020-06-19 15:18:31 +02:00
|
|
|
$this->containerMeta = new ContainerMeta(['non-existent-file.xml']);
|
2020-03-10 17:00:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox get non existent service
|
|
|
|
*/
|
|
|
|
public function testNonExistentService()
|
|
|
|
{
|
2021-08-22 07:27:44 +02:00
|
|
|
$this->expectException(ServiceNotFoundException::class);
|
2020-03-10 17:00:55 +01:00
|
|
|
$this->assertNull($this->containerMeta->get('non-existent-service'));
|
|
|
|
}
|
2020-06-19 15:18:31 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @testdox one valid, one invalid file should not raise an issue
|
|
|
|
*/
|
|
|
|
public function testBothValidAndInvalidArray()
|
|
|
|
{
|
|
|
|
$containerMeta = new ContainerMeta(['non-existent-file.xml', __DIR__.'/../../acceptance/container.xml']);
|
|
|
|
$service = $containerMeta->get('service_container');
|
2021-08-22 07:27:44 +02:00
|
|
|
$this->assertSame('Symfony\Component\DependencyInjection\ContainerInterface', $service->getClass());
|
2020-06-19 15:18:31 +02:00
|
|
|
}
|
2021-06-18 09:59:41 +02:00
|
|
|
|
|
|
|
public function testGetParameter(): void
|
|
|
|
{
|
|
|
|
$this->assertSame('dev', $this->containerMeta->getParameter('kernel.environment'));
|
|
|
|
$this->assertSame(true, $this->containerMeta->getParameter('debug_enabled'));
|
|
|
|
$this->assertSame('1', $this->containerMeta->getParameter('version'));
|
|
|
|
$this->assertSame(1, $this->containerMeta->getParameter('integer_one'));
|
|
|
|
$this->assertSame(3.14, $this->containerMeta->getParameter('pi'));
|
|
|
|
$this->assertSame([
|
|
|
|
'key1' => 'val1',
|
|
|
|
'key2' => 'val2',
|
|
|
|
], $this->containerMeta->getParameter('collection1'));
|
|
|
|
$this->assertSame([
|
|
|
|
'key' => 'val',
|
|
|
|
'child_collection' => [
|
|
|
|
'boolean' => true,
|
|
|
|
'float' => 2.18,
|
|
|
|
'grandchild_collection' => [
|
|
|
|
'string' => 'something',
|
|
|
|
],
|
|
|
|
]
|
|
|
|
], $this->containerMeta->getParameter('nested_collection'));
|
|
|
|
}
|
2021-08-22 07:27:44 +02:00
|
|
|
|
|
|
|
public function testGetParameterP(): void
|
|
|
|
{
|
|
|
|
$this->expectException(ParameterNotFoundException::class);
|
|
|
|
$this->containerMeta->getParameter('non_existent');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider serviceLocatorProvider
|
|
|
|
*/
|
|
|
|
public function testGetServiceWithContext(string $id, string $contextClass, string $expectedClass): void
|
|
|
|
{
|
|
|
|
$serviceDefinition = $this->containerMeta->get($id, $contextClass);
|
|
|
|
$this->assertSame($expectedClass, $serviceDefinition->getClass());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function serviceLocatorProvider(): iterable
|
|
|
|
{
|
|
|
|
yield [
|
|
|
|
'dummy_service_with_locator2',
|
|
|
|
'App\Controller\DummyController',
|
|
|
|
'Psalm\SymfonyPsalmPlugin\Tests\Fixture\DummyPrivateService'
|
|
|
|
];
|
|
|
|
yield [
|
|
|
|
'dummy_service_with_locator3',
|
|
|
|
'App\Controller\DummyController',
|
|
|
|
'Psalm\SymfonyPsalmPlugin\Tests\Fixture\DummyPrivateService'
|
|
|
|
];
|
|
|
|
}
|
2020-03-10 17:00:55 +01:00
|
|
|
}
|