mdmunir
9 years ago
committed by
Alexander Makarov
10 changed files with 353 additions and 5 deletions
@ -0,0 +1,86 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\console; |
||||
|
||||
use Yii; |
||||
use yiiunit\TestCase; |
||||
use yiiunit\framework\di\stubs\Qux; |
||||
use yiiunit\framework\web\stubs\Bar; |
||||
use yiiunit\framework\web\stubs\OtherQux; |
||||
|
||||
/** |
||||
* @group console |
||||
*/ |
||||
class ControllerTest extends TestCase |
||||
{ |
||||
|
||||
public function testBindActionParams() |
||||
{ |
||||
$this->mockApplication([ |
||||
'components' => [ |
||||
'barBelongApp' => [ |
||||
'class' => Bar::className(), |
||||
'foo' => 'belong_app' |
||||
], |
||||
'quxApp' => [ |
||||
'class' => OtherQux::className(), |
||||
'b' => 'belong_app' |
||||
] |
||||
] |
||||
]); |
||||
|
||||
$controller = new FakeController('fake', Yii::$app); |
||||
|
||||
Yii::$container->set('yiiunit\framework\di\stubs\QuxInterface', [ |
||||
'class' => Qux::className(), |
||||
'a' => 'D426' |
||||
]); |
||||
Yii::$container->set(Bar::className(), [ |
||||
'foo' => 'independent' |
||||
]); |
||||
|
||||
$params = ['from params']; |
||||
list($bar, $fromParam, $other) = $controller->run('aksi1', $params); |
||||
$this->assertTrue($bar instanceof Bar); |
||||
$this->assertNotEquals($bar, Yii::$app->barBelongApp); |
||||
$this->assertEquals('independent', $bar->foo); |
||||
$this->assertEquals('from params', $fromParam); |
||||
$this->assertEquals('default', $other); |
||||
|
||||
$params = []; |
||||
list($barBelongApp, $qux) = $controller->run('aksi2', $params); |
||||
$this->assertTrue($barBelongApp instanceof Bar); |
||||
$this->assertEquals($barBelongApp, Yii::$app->barBelongApp); |
||||
$this->assertEquals('belong_app', $barBelongApp->foo); |
||||
$this->assertTrue($qux instanceof Qux); |
||||
$this->assertEquals('D426', $qux->a); |
||||
|
||||
$params = []; |
||||
list($quxApp) = $controller->run('aksi3', $params); |
||||
$this->assertTrue($quxApp instanceof OtherQux); |
||||
$this->assertEquals($quxApp, Yii::$app->quxApp); |
||||
$this->assertEquals('belong_app', $quxApp->b); |
||||
|
||||
$params = ['d426,mdmunir', 'single']; |
||||
$result = $controller->runAction('aksi4', $params); |
||||
$this->assertEquals(['independent', 'other_qux', ['d426', 'mdmunir'], 'single'], $result); |
||||
|
||||
$params = ['d426']; |
||||
$result = $controller->runAction('aksi5', $params); |
||||
$this->assertEquals(['d426', 'independent', 'other_qux'], $result); |
||||
|
||||
$params = ['mdmunir']; |
||||
$result = $controller->runAction('aksi6', $params); |
||||
$this->assertEquals(['mdmunir', false, true], $result); |
||||
|
||||
$params = ['avaliable']; |
||||
$message = Yii::t('yii', 'Missing required arguments: {params}', ['params' => implode(', ', ['missing'])]); |
||||
$this->setExpectedException('yii\console\Exception', $message); |
||||
$result = $controller->runAction('aksi7', $params); |
||||
} |
||||
} |
@ -0,0 +1,56 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\console; |
||||
|
||||
use yii\console\Controller; |
||||
use yiiunit\framework\di\stubs\QuxInterface; |
||||
use yiiunit\framework\web\stubs\Bar; |
||||
use yii\validators\EmailValidator; |
||||
|
||||
/** |
||||
* @author Misbahul D Munir <misbahuldmunir@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class FakeController extends Controller |
||||
{ |
||||
|
||||
public function actionAksi1(Bar $bar, $fromParam, $other = 'default') |
||||
{ |
||||
return[$bar, $fromParam, $other]; |
||||
} |
||||
|
||||
public function actionAksi2(Bar $barBelongApp, QuxInterface $qux) |
||||
{ |
||||
return[$barBelongApp, $qux]; |
||||
} |
||||
|
||||
public function actionAksi3(QuxInterface $quxApp) |
||||
{ |
||||
return[$quxApp]; |
||||
} |
||||
|
||||
public function actionAksi4(Bar $bar, QuxInterface $quxApp, array $values, $value) |
||||
{ |
||||
return [$bar->foo, $quxApp->quxMethod(), $values, $value]; |
||||
} |
||||
|
||||
public function actionAksi5($q, Bar $bar, QuxInterface $quxApp) |
||||
{ |
||||
return [$q, $bar->foo, $quxApp->quxMethod()]; |
||||
} |
||||
|
||||
public function actionAksi6($q, EmailValidator $validator) |
||||
{ |
||||
return [$q, $validator->validate($q), $validator->validate('misbahuldmunir@gmail.com')]; |
||||
} |
||||
|
||||
public function actionAksi7(Bar $bar, $avaliable, $missing) |
||||
{ |
||||
|
||||
} |
||||
} |
@ -0,0 +1,81 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\web; |
||||
|
||||
use Yii; |
||||
use yiiunit\TestCase; |
||||
use yiiunit\framework\di\stubs\Qux; |
||||
use yiiunit\framework\web\stubs\Bar; |
||||
use yiiunit\framework\web\stubs\OtherQux; |
||||
use yii\base\InlineAction; |
||||
|
||||
/** |
||||
* @group web |
||||
*/ |
||||
class ControllerTest extends TestCase |
||||
{ |
||||
|
||||
public function testBindActionParams() |
||||
{ |
||||
$this->mockApplication([ |
||||
'components'=>[ |
||||
'barBelongApp'=>[ |
||||
'class'=> Bar::className(), |
||||
'foo'=>'belong_app' |
||||
], |
||||
'quxApp'=>[ |
||||
'class' => OtherQux::className(), |
||||
'b' => 'belong_app' |
||||
] |
||||
] |
||||
]); |
||||
|
||||
$controller = new FakeController('fake', Yii::$app); |
||||
$aksi1 = new InlineAction('aksi1', $controller, 'actionAksi1'); |
||||
$aksi2 = new InlineAction('aksi2', $controller, 'actionAksi2'); |
||||
$aksi3 = new InlineAction('aksi3', $controller, 'actionAksi3'); |
||||
|
||||
Yii::$container->set('yiiunit\framework\di\stubs\QuxInterface', [ |
||||
'class' => Qux::className(), |
||||
'a' => 'D426' |
||||
]); |
||||
Yii::$container->set(Bar::className(),[ |
||||
'foo' => 'independent' |
||||
]); |
||||
|
||||
$params = ['fromGet'=>'from query params','q'=>'d426','validator'=>'avaliable']; |
||||
|
||||
list($bar, $fromGet, $other) = $controller->bindActionParams($aksi1, $params); |
||||
$this->assertTrue($bar instanceof Bar); |
||||
$this->assertNotEquals($bar, Yii::$app->barBelongApp); |
||||
$this->assertEquals('independent', $bar->foo); |
||||
$this->assertEquals('from query params', $fromGet); |
||||
$this->assertEquals('default', $other); |
||||
|
||||
list($barBelongApp, $qux) = $controller->bindActionParams($aksi2, $params); |
||||
$this->assertTrue($barBelongApp instanceof Bar); |
||||
$this->assertEquals($barBelongApp, Yii::$app->barBelongApp); |
||||
$this->assertEquals('belong_app', $barBelongApp->foo); |
||||
$this->assertTrue($qux instanceof Qux); |
||||
$this->assertEquals('D426', $qux->a); |
||||
|
||||
list($quxApp) = $controller->bindActionParams($aksi3, $params); |
||||
$this->assertTrue($quxApp instanceof OtherQux); |
||||
$this->assertEquals($quxApp, Yii::$app->quxApp); |
||||
$this->assertEquals('belong_app', $quxApp->b); |
||||
|
||||
$result = $controller->runAction('aksi4', $params); |
||||
$this->assertEquals(['independent', 'other_qux', 'd426'], $result); |
||||
|
||||
$result = $controller->runAction('aksi5', $params); |
||||
$this->assertEquals(['d426', 'independent', 'other_qux'], $result); |
||||
|
||||
$result = $controller->runAction('aksi6', $params); |
||||
$this->assertEquals(['d426', false, true], $result); |
||||
} |
||||
} |
@ -0,0 +1,49 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\web; |
||||
|
||||
use yii\web\Controller; |
||||
use yiiunit\framework\di\stubs\QuxInterface; |
||||
use yiiunit\framework\web\stubs\Bar; |
||||
use yii\validators\EmailValidator; |
||||
|
||||
/** |
||||
* @author Misbahul D Munir <misbahuldmunir@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class FakeController extends Controller |
||||
{ |
||||
public $enableCsrfValidation = false; |
||||
|
||||
public function actionAksi1(Bar $bar, $fromGet, $other = 'default') |
||||
{ |
||||
} |
||||
|
||||
public function actionAksi2(Bar $barBelongApp, QuxInterface $qux) |
||||
{ |
||||
} |
||||
|
||||
public function actionAksi3(QuxInterface $quxApp) |
||||
{ |
||||
} |
||||
|
||||
public function actionAksi4(Bar $bar, QuxInterface $quxApp, $q) |
||||
{ |
||||
return [$bar->foo, $quxApp->quxMethod(), $q]; |
||||
} |
||||
|
||||
public function actionAksi5($q, Bar $bar, QuxInterface $quxApp) |
||||
{ |
||||
return [$q, $bar->foo, $quxApp->quxMethod()]; |
||||
} |
||||
|
||||
public function actionAksi6($q, EmailValidator $validator) |
||||
{ |
||||
return [$q, $validator->validate($q), $validator->validate('misbahuldmunir@gmail.com')]; |
||||
} |
||||
} |
@ -0,0 +1,19 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\web\stubs; |
||||
|
||||
use yii\base\Object; |
||||
|
||||
/** |
||||
* @author Misbahul D Munir <misbahuldmunir@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class Bar extends Object |
||||
{ |
||||
public $foo; |
||||
} |
@ -0,0 +1,24 @@
|
||||
<?php |
||||
/** |
||||
* @link http://www.yiiframework.com/ |
||||
* @copyright Copyright (c) 2008 Yii Software LLC |
||||
* @license http://www.yiiframework.com/license/ |
||||
*/ |
||||
|
||||
namespace yiiunit\framework\web\stubs; |
||||
|
||||
use yii\base\Object; |
||||
use yiiunit\framework\di\stubs\QuxInterface; |
||||
|
||||
/** |
||||
* @author Misbahul D Munir <misbahuldmunir@gmail.com> |
||||
* @since 2.0 |
||||
*/ |
||||
class OtherQux extends Object implements QuxInterface |
||||
{ |
||||
public $b; |
||||
public function quxMethod() |
||||
{ |
||||
return 'other_qux'; |
||||
} |
||||
} |
Loading…
Reference in new issue