8000 [Config] gracefully handle cases when no resolver is set by xabbuh · Pull Request #57069 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Config] gracefully handle cases when no resolver is set #57069

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
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/Symfony/Component/Config/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Config\Exception;

class LogicException extends \LogicException
{
}
7 changes: 6 additions & 1 deletion src/Symfony/Component/Config/Loader/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Symfony\Component\Config\Loader;

use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\Exception\LogicException;

/**
* Loader is the abstract class used by all built-in loaders.
Expand All @@ -20,7 +21,7 @@
*/
abstract class Loader implements LoaderInterface
{
protected LoaderResolverInterface $resolver;
protected ?LoaderResolverInterface $resolver = null;
protected ?string $env;

public function __construct(?string $env = null)
Expand All @@ -30,6 +31,10 @@ public function __construct(?string $env = null)

public function getResolver(): LoaderResolverInterface
{
if (null === $this->resolver) {
throw new LogicException('Cannot get a resolver if none was set.');
}

return $this->resolver;
}

Expand Down
17 changes: 17 additions & 0 deletions src/Symfony/Component/Config/Tests/Loader/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Exception\LoaderLoadException;
use Symfony\Component\Config\Exception\LogicException;
use Symfony\Component\Config\Loader\Loader;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\Loader\LoaderResolverInterface;
Expand All @@ -29,6 +30,14 @@ public function testGetSetResolver()
$this->assertSame($resolver, $loader->getResolver(), '->setResolver() sets the resolver loader');
}

public function testGetResolverWithoutSetResolver()
{
$this->expectException(LogicException::class);

$loader = new ProjectLoader1();
$loader->getResolver();
}

public function testResolve()
{
$resolvedLoader = $this->createMock(LoaderInterface::class);
Expand All @@ -46,6 +55,14 @@ public function testResolve()
$this->assertSame($resolvedLoader, $loader->resolve('foo.xml'), '->resolve() finds a loader');
}

public function testResolveWithoutSetResolver()
{
$this->expectException(LoaderLoadException::class);

$loader = new ProjectLoader1();
$loader->resolve('foo.xml');
}

public function testResolveWhenResolverCannotFindLoader()
{
$resolver = $this->createMock(LoaderResolverInterface::class);
Expand Down
16 changes: 16 additions & 0 deletions src/Symfony/Component/Routing/Exception/LogicException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Routing\Exception;

class LogicException extends \LogicException
{
}
2 changes: 2 additions & 0 deletions src/Symfony/Component/Routing/Loader/AttributeClassLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Config\Loader\LoaderResolverInterface;
use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\Routing\Attribute\Route as RouteAnnotation;
use Symfony\Component\Routing\Exception\LogicException;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;

Expand Down Expand Up @@ -226,6 +227,7 @@ public function setResolver(LoaderResolverInterface $resolver): void

public function getResolver(): LoaderResolverInterface
{
throw new LogicException(sprintf('The "%s()" method must not be called.', __METHOD__));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Routing\Alias;
use Symfony\Component\Routing\Exception\LogicException;
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\AbstractClassController;
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\ActionPathController;
use Symfony\Component\Routing\Tests\Fixtures\AttributeFixtures\BazClass;
Expand Down Expand Up @@ -54,6 +55,14 @@ protected function setUp(?string $env = null): void
$this->loader = new TraceableAttributeClassLoader($env);
}

public function testGetResolver()
{
$this->expectException(LogicException::class);

$loader = new TraceableAttributeClassLoader();
$loader->getResolver();
}

/**
* @dataProvider provideTestSupportsChecksResource
*/
Expand Down
Loading
0