Skip to content

Commit

Permalink
add invalid configuration hint (#2113)
Browse files Browse the repository at this point in the history
* add invalid configuration hint

* add test

* changelog
  • Loading branch information
nadar committed Dec 7, 2021
1 parent ea30dd2 commit 5c13167
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 4 deletions.
5 changes: 5 additions & 0 deletions core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
All notable changes to this project will be documented in this file. This project adheres to [Semantic Versioning](http://semver.org/).
In order to read more about upgrading and BC breaks have a look at the [UPGRADE Document](UPGRADE.md).

## 2.0.2 (7. December 2021)

+ [#2113](https://github.com/luyadev/luya/pull/2113) Improve error handling for expected composition values configuration.
+ New translations for bg, fr, hu, pt and ro

## 2.0.1 (29. July 2021)

+ Forgot to adjust the LUYA Version Constant `luya\Boot::VERSION`.
Expand Down
14 changes: 10 additions & 4 deletions core/web/CompositionResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use luya\web\Request;
use luya\helpers\Url;
use luya\helpers\StringHelper;
use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException;

/**
Expand Down Expand Up @@ -168,11 +169,16 @@ protected function getInternalResolverArray()

// the validation check for validates composition values is enabled
if ($this->composition->expectedValues) {
foreach ($keys as $k => $v) {
$possibleValues = $this->composition->expectedValues[$k];
foreach ($keys as $patternKey => $expectedPatternValue) {

if (!in_array($v, $possibleValues)) {
throw new NotFoundHttpException("The requested composition key \"{$k}\" with value \"{$v}\" is not in the possible values list.");
if (!array_key_exists($patternKey, $this->composition->expectedValues)) {
throw new InvalidConfigException("Invalid configuration, the expectedValues configuration key \"{$patternKey}\" does not exists in the resolved values list.");
}

$possibleValues = $this->composition->expectedValues[$patternKey];

if (!in_array($expectedPatternValue, $possibleValues)) {
throw new NotFoundHttpException("The requested composition key \"{$patternKey}\" with value \"{$expectedPatternValue}\" is not in the possible values list.");
}
}
}
Expand Down
18 changes: 18 additions & 0 deletions tests/core/web/CompositionResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use luya\web\CompositionResolver;
use luya\web\Request;
use luya\web\Composition;
use yii\base\InvalidConfigException;
use yii\web\NotFoundHttpException;

class CompositionResolverTest extends LuyaWebTestCase
Expand Down Expand Up @@ -85,4 +86,21 @@ public function testExpectedResolverWithEmptyValuesButDefaults()
$this->expectException(NotFoundHttpException::class);
$resolver->resolvedValues;
}

public function testInvalidConfigurationForExpectedValues()
{
$request = new Request();
$request->pathInfo = '';

$composition = new Composition($request);
$composition->pattern = '<lang:[a-z]{2}>/<xyz:[a-z]{3}>';
$composition->default = ['lang' => 'en', 'xyz' => 'foo'];
$composition->expectedValues = [
'doesnotexists' => ['en'],
];

$resolver = new CompositionResolver($request, $composition);
$this->expectException(InvalidConfigException::class);
$resolver->resolvedValues;
}
}

0 comments on commit 5c13167

Please sign in to comment.