Skip to content

Commit

Permalink
Fix merge conflicts for PATCH verb support
Browse files Browse the repository at this point in the history
  • Loading branch information
= committed Jun 2, 2013
2 parents 11f4de0 + d0bc6ce commit 12a5257
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 3 deletions.
20 changes: 20 additions & 0 deletions Slim/Http/Request.php
Expand Up @@ -48,6 +48,7 @@ class Request
const METHOD_GET = 'GET';
const METHOD_POST = 'POST';
const METHOD_PUT = 'PUT';
const METHOD_PATCH = 'PATCH';
const METHOD_DELETE = 'DELETE';
const METHOD_OPTIONS = 'OPTIONS';
const METHOD_OVERRIDE = '_METHOD';
Expand Down Expand Up @@ -124,6 +125,15 @@ public function isPut()
return $this->getMethod() === self::METHOD_PUT;
}

/**
* Is this a PATCH request?
* @return bool
*/
public function isPatch()
{
return $this->getMethod() === self::METHOD_PATCH;
}

/**
* Is this a DELETE request?
* @return bool
Expand Down Expand Up @@ -275,6 +285,16 @@ public function put($key = null)
return $this->post($key);
}

/**
* Fetch PATCH data (alias for \Slim\Http\Request::post)
* @param string $key
* @return array|mixed|null
*/
public function patch($key = null)
{
return $this->post($key);
}

/**
* Fetch DELETE data (alias for \Slim\Http\Request::post)
* @param string $key
Expand Down
16 changes: 14 additions & 2 deletions Slim/Slim.php
Expand Up @@ -370,7 +370,7 @@ public function getLog()
*******************************************************************************/

/**
* Add GET|POST|PUT|DELETE route
* Add GET|POST|PUT|PATCH|DELETE route
*
* Adds a new route to the router with associated callable. This
* route will only be invoked when the HTTP request's method matches
Expand Down Expand Up @@ -459,6 +459,18 @@ public function put()
return $this->mapRoute($args)->via(\Slim\Http\Request::METHOD_PUT);
}

/**
* Add PATCH route
* @see mapRoute()
* @return \Slim\Route
*/
public function patch()
{
$args = func_get_args();

return $this->mapRoute($args)->via(\Slim\Http\Request::METHOD_PATCH);
}

/**
* Add DELETE route
* @see mapRoute()
Expand Down Expand Up @@ -689,7 +701,7 @@ public function view($viewClass = null)
/**
* Render a template
*
* Call this method within a GET, POST, PUT, DELETE, NOT FOUND, or ERROR
* Call this method within a GET, POST, PUT, PATCH, DELETE, NOT FOUND, or ERROR
* callable to render a template whose output is appended to the
* current HTTP response body. How the template is rendered is
* delegated to the current View.
Expand Down
7 changes: 6 additions & 1 deletion index.php
Expand Up @@ -26,7 +26,7 @@
*
* Here we define several Slim application routes that respond
* to appropriate HTTP request methods. In this example, the second
* argument for `Slim::get`, `Slim::post`, `Slim::put`, and `Slim::delete`
* argument for `Slim::get`, `Slim::post`, `Slim::put`, `Slim::patch`, and `Slim::delete`
* is an anonymous function.
*/

Expand Down Expand Up @@ -147,6 +147,11 @@ function () {
}
);

// PATCH route
$app->patch('/patch', function () {
echo 'This is a PATCH route';
});

// DELETE route
$app->delete(
'/delete',
Expand Down
30 changes: 30 additions & 0 deletions tests/Http/RequestTest.php
Expand Up @@ -116,6 +116,18 @@ public function testIsHead()
$this->assertTrue($req->isHead());
}

/**
* Test HTTP PATCH method detection
*/
public function testIsPatch()
{
$env = \Slim\Environment::mock(array(
'REQUEST_METHOD' => 'PATCH',
));
$req = new \Slim\Http\Request($env);
$this->assertTrue($req->isPatch());
}

/**
* Test AJAX method detection w/ header
*/
Expand Down Expand Up @@ -309,6 +321,24 @@ public function testPut()
$this->assertNull($req->put('xyz'));
}

/**
* Test fetch PATCH params
*/
public function testPatch()
{
$env = \Slim\Environment::mock(array(
'REQUEST_METHOD' => 'PATCH',
'slim.input' => 'foo=bar&abc=123',
'CONTENT_TYPE' => 'application/x-www-form-urlencoded',
'CONTENT_LENGTH' => 15
));
$req = new \Slim\Http\Request($env);
$this->assertEquals(2, count($req->patch()));
$this->assertEquals('bar', $req->patch('foo'));
$this->assertEquals('bar', $req->params('foo'));
$this->assertNull($req->patch('xyz'));
}

/**
* Test fetch DELETE params
*/
Expand Down
21 changes: 21 additions & 0 deletions tests/SlimTest.php
Expand Up @@ -371,6 +371,27 @@ public function testPutRoute()
$this->assertSame($callable, $route->getCallable());
}

/**
* Test PATCH route
*/
public function testPatchRoute()
{
\Slim\Environment::mock(array(
'REQUEST_METHOD' => 'PATCH',
'SCRIPT_NAME' => '/foo', //<-- Physical
'PATH_INFO' => '/bar', //<-- Virtual
));
$s = new \Slim\Slim();
$mw1 = function () { echo "foo"; };
$mw2 = function () { echo "bar"; };
$callable = function () { echo "xyz"; };
$route = $s->patch('/bar', $mw1, $mw2, $callable);
$s->call();
$this->assertEquals('foobarxyz', $s->response()->body());
$this->assertEquals('/bar', $route->getPattern());
$this->assertSame($callable, $route->getCallable());
}

/**
* Test DELETE route
*/
Expand Down

0 comments on commit 12a5257

Please sign in to comment.