Browse Source

Fix #18566: Throw the original exception when `yii\web\Controller::bindInjectedParams()` catches HttpException

bizley-patch-1
Pigo 4 years ago committed by GitHub
parent
commit
14a581562a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      framework/CHANGELOG.md
  2. 2
      framework/web/Controller.php
  3. 32
      tests/framework/web/ControllerTest.php
  4. 6
      tests/framework/web/FakePhp71Controller.php
  5. 15
      tests/framework/web/stubs/ModelBindingStub.php

1
framework/CHANGELOG.md

@ -5,6 +5,7 @@ Yii Framework 2 Change Log
------------------------
- Enh #18534: Added `prepareSearchQuery` property in `yii\rest\IndexAction` (programmis)
- Enh #18566: Throw the original exception when `yii\web\Controller::bindInjectedParams()` catches HttpException (pigochu)
- Bug #18574: Fix `yii\web\DbSession` to use the correct db if strict mode is used (Mignar)
- Bug #17479: Fix `yii\grid\ActionColumn` to render icons when no glyphicons are available (simialbi)
- Bug #18544: Fix `yii\validators\NumberValidator` to disallow values with whitespaces (bizley)

2
framework/web/Controller.php

@ -180,6 +180,8 @@ class Controller extends \yii\base\Controller
} elseif (PHP_VERSION_ID >= 70100 && ($type = $param->getType()) !== null && !$type->isBuiltin()) {
try {
$this->bindInjectedParams($type, $name, $args, $requestedParams);
} catch (HttpException $e) {
throw $e;
} catch (Exception $e) {
throw new ServerErrorHttpException($e->getMessage(), 0, $e);
}

32
tests/framework/web/ControllerTest.php

@ -10,6 +10,8 @@ namespace yiiunit\framework\web;
use RuntimeException;
use Yii;
use yii\base\InlineAction;
use yii\web\HttpException;
use yii\web\NotFoundHttpException;
use yii\web\Response;
use yii\web\ServerErrorHttpException;
use yiiunit\framework\web\stubs\VendorImage;
@ -87,6 +89,36 @@ class ControllerTest extends TestCase
$this->assertNull($args[1]);
}
public function testModelBindingHttpException() {
if (PHP_VERSION_ID < 70100) {
$this->markTestSkipped('Can not be tested on PHP < 7.1');
return;
}
$this->controller = new FakePhp71Controller('fake', new \yii\web\Application([
'id' => 'app',
'basePath' => __DIR__,
'container' => [
'definitions' => [
\yiiunit\framework\web\stubs\ModelBindingStub::className() => [ \yiiunit\framework\web\stubs\ModelBindingStub::className() , "build"],
]
],
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
],
],
]));
Yii::$container->set(VendorImage::className(), VendorImage::className());
$this->mockWebApplication(['controller' => $this->controller]);
$injectionAction = new InlineAction('injection', $this->controller, 'actionModelBindingInjection');
$this->expectException(get_class(new NotFoundHttpException("Not Found Item.")));
$this->expectExceptionMessage('Not Found Item.');
$this->controller->bindActionParams($injectionAction, []);
}
public function testInjectionContainerException()
{
if (PHP_VERSION_ID < 70100) {

6
tests/framework/web/FakePhp71Controller.php

@ -11,6 +11,7 @@ use yii\data\DataProviderInterface;
use yii\web\Controller;
use yii\web\Request;
use yiiunit\framework\web\stubs\VendorImage;
use yiiunit\framework\web\stubs\ModelBindingStub;
/**
* @author Sam Mousa<sam@mousa.nl>
@ -32,4 +33,9 @@ class FakePhp71Controller extends Controller
public function actionModuleServiceInjection(DataProviderInterface $dataProvider)
{
}
public function actionModelBindingInjection(ModelBindingStub $model)
{
}
}

15
tests/framework/web/stubs/ModelBindingStub.php

@ -0,0 +1,15 @@
<?php
namespace yiiunit\framework\web\stubs;
use yii\db\ActiveRecord;
use yii\web\NotFoundHttpException;
class ModelBindingStub extends ActiveRecord {
/**
* @return self;
* @throw NotFoundHttpException
*/
public static function build() {
throw new NotFoundHttpException("Not Found Item.");
}
}
Loading…
Cancel
Save