Commit e94cc6bb by Carsten Brandt

unit test for UrlManager RESTful routing syntax

removed test param 'appClass' as it does not make sense to configure application class for all tests, some explicitly need webapp where some might explicitly need console app.
parent 11923064
...@@ -38,14 +38,13 @@ abstract class TestCase extends \yii\test\TestCase ...@@ -38,14 +38,13 @@ abstract class TestCase extends \yii\test\TestCase
* The application will be destroyed on tearDown() automatically. * The application will be destroyed on tearDown() automatically.
* @param array $config The application configuration, if needed * @param array $config The application configuration, if needed
*/ */
protected function mockApplication($config=array()) protected function mockApplication($config = array(), $appClass = '\yii\console\Application')
{ {
static $defaultConfig = array( static $defaultConfig = array(
'id' => 'testapp', 'id' => 'testapp',
'basePath' => __DIR__, 'basePath' => __DIR__,
); );
$appClass = $this->getParam( 'appClass', '\yii\web\Application' );
new $appClass(array_merge($defaultConfig,$config)); new $appClass(array_merge($defaultConfig,$config));
} }
......
<?php <?php
return array( return array(
//'appClass' => '\yii\web\Application',
'appClass' => '\yii\console\Application',
'databases' => array( 'databases' => array(
'mysql' => array( 'mysql' => array(
'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest', 'dsn' => 'mysql:host=127.0.0.1;dbname=yiitest',
......
...@@ -249,5 +249,57 @@ class UrlManagerTest extends TestCase ...@@ -249,5 +249,57 @@ class UrlManagerTest extends TestCase
$this->assertFalse($result); $this->assertFalse($result);
} }
// TODO test RESTful pattern syntax e.g. 'GET index' => 'site/index' public function testParseRESTRequest()
{
$manager = new UrlManager(array(
'cache' => null,
));
$request = new Request;
// pretty URL rules
$manager = new UrlManager(array(
'enablePrettyUrl' => true,
'cache' => null,
'rules' => array(
'PUT,POST post/<id>/<title>' => 'post/create',
'DELETE post/<id>' => 'post/delete',
'post/<id>/<title>' => 'post/view',
'POST/GET' => 'post/get',
),
));
// matching pathinfo GET request
$_SERVER['REQUEST_METHOD'] = 'GET';
$request->pathInfo = 'post/123/this+is+sample';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/view', array('id' => '123', 'title' => 'this+is+sample')), $result);
// matching pathinfo PUT/POST request
$_SERVER['REQUEST_METHOD'] = 'PUT';
$request->pathInfo = 'post/123/this+is+sample';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/create', array('id' => '123', 'title' => 'this+is+sample')), $result);
$_SERVER['REQUEST_METHOD'] = 'POST';
$request->pathInfo = 'post/123/this+is+sample';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/create', array('id' => '123', 'title' => 'this+is+sample')), $result);
// no wrong matching
$_SERVER['REQUEST_METHOD'] = 'POST';
$request->pathInfo = 'POST/GET';
$result = $manager->parseRequest($request);
$this->assertEquals(array('post/get', array()), $result);
// createUrl should ignore REST rules
$this->mockApplication(array(
'components' => array(
'request' => array(
'hostInfo' => 'http://localhost/',
'baseUrl' => '/app'
)
)
), \yii\web\Application::className());
$this->assertEquals('/app/post/delete?id=123', $manager->createUrl('post/delete', array('id' => 123)));
$this->destroyApplication();
unset($_SERVER['REQUEST_METHOD']);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment