Skip to content

Commit

Permalink
gracefully handle cases when no resolver is set
Browse files Browse the repository at this point in the history
  • Loading branch information
xabbuh committed May 23, 2024
1 parent 5e5456a commit c62556d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
16 changes: 16 additions & 0 deletions 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 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 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

0 comments on commit c62556d

Please sign in to comment.