/** * @covers Respect\Rest\Router::dispatchRequest * @covers Respect\Rest\Router::isRoutelessDispatch * @covers Respect\Rest\Router::hasDispatchedOverridenMethod */ public function testDeveloperCanOverridePostMethodWithQueryStringParameter() { $_REQUEST['_method'] = 'PUT'; $router = new Router(); $router->methodOverriding = true; $router->put('/bulbs', 'Some Bulbs Put Response'); $router->post('/bulbs', 'Some Bulbs Post Response'); $result = (string) $router->dispatch('POST', '/bulbs')->response(); $this->assertSame('Some Bulbs Put Response', $result, 'Router should dispatch to PUT (overriden) instead of POST'); $this->assertNotSame('Some Bulbs Post Response', $result, 'Router NOT dispatch to POST when method is overriden'); return $router; }
$book = $db->books()->where('id', $id); if ($data = $book->fetch()) { return array('id' => $data['id'], 'title' => $data['title'], 'author' => $data['author'], 'summary' => $data['summary']); } else { return array('status' => false, 'message' => sprintf('Book ID %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode')); $r3->post('/book', function () use($db) { $book = array('title' => $_POST['title'], 'author' => $_POST['author'], 'summary' => $_POST['summary']); $result = $db->books->insert($book); return array('id' => $result['id']); })->accept(array('application/json' => 'json_encode')); $r3->put('/book/*', function ($id) use($db) { $book = $db->books()->where('id', $id); if ($book->fetch()) { parse_str(file_get_contents('php://input'), $put); $result = $book->update($put); return array('status' => (bool) $result, 'message' => 'Book updated successfully'); } else { return array('status' => false, 'message' => sprintf('Book id %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode')); $r3->delete('/book/*', function ($id) use($db) { $book = $db->books()->where('id', $id); if ($book->fetch()) { $result = $book->delete(); return array('status' => true, 'message' => 'Book deleted successfully'); } else { return array('status' => false, 'message' => sprintf('Book id %s does not exist', $id)); } })->accept(array('application/json' => 'json_encode'));