mirror of
https://github.com/FH-Complete/FHC-Core.git
synced 2026-07-20 00:12:15 +00:00
- Fixed codeception test
- Updated the api test suite - Added the script generate.php to automatically generate the test cases for the controllers
This commit is contained in:
@@ -1,150 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* @file
|
||||
* This is an example of a few basic user interaction methods you could use
|
||||
* all done with a hardcoded array
|
||||
*
|
||||
* @package CodeIgniter
|
||||
* @subpackage Rest Server
|
||||
* @category Controller
|
||||
* @author Phil Sturgeon, Chris Kacerguis
|
||||
* @license MIT
|
||||
* @link https://github.com/chriskacerguis/codeigniter-restserver
|
||||
*/
|
||||
if (! defined('BASEPATH'))
|
||||
exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH.'/libraries/REST_Controller.php';
|
||||
|
||||
class Example extends REST_Controller
|
||||
{
|
||||
/**
|
||||
* @copydoc REST_Controller::__construct()
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
// Construct the parent class
|
||||
parent::__construct();
|
||||
|
||||
// Configure limits on our controller methods
|
||||
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
|
||||
$this->methods['user_get']['limit'] = 500; // 500 requests per hour per user/key
|
||||
$this->methods['user_post']['limit'] = 100; // 100 requests per hour per user/key
|
||||
$this->methods['user_delete']['limit'] = 50; // 50 requests per hour per user/key
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_get()
|
||||
{
|
||||
// Users from a data store e.g. database
|
||||
$users = [
|
||||
['id' => 1, 'name' => 'John', 'email' => 'john@example.com', 'fact' => 'Loves coding'],
|
||||
['id' => 2, 'name' => 'Jim', 'email' => 'jim@example.com', 'fact' => 'Developed on CodeIgniter'],
|
||||
['id' => 3, 'name' => 'Jane', 'email' => 'jane@example.com', 'fact' => 'Lives in the USA', ['hobbies' => ['guitar', 'cycling']]],
|
||||
];
|
||||
|
||||
$id = $this->get('id');
|
||||
|
||||
// If the id parameter doesn't exist return all the users
|
||||
|
||||
if ($id === null)
|
||||
{
|
||||
// Check if the users data store contains users (in case the database result returns null)
|
||||
if ($users)
|
||||
{
|
||||
// Set the response and exit
|
||||
$this->response($users, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
// Set the response and exit
|
||||
$this->response([
|
||||
'status' => false,
|
||||
'message' => 'No users were found'
|
||||
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
|
||||
}
|
||||
}
|
||||
|
||||
// Find and return a single record for a particular user.
|
||||
|
||||
$id = (int)$id;
|
||||
|
||||
// Validate the id.
|
||||
if ($id <= 0)
|
||||
{
|
||||
// Invalid id, set the response and exit.
|
||||
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
|
||||
// Get the user from the array, using the id as key for retreival.
|
||||
// Usually a model is to be used for this.
|
||||
|
||||
$user = null;
|
||||
|
||||
if (!empty($users))
|
||||
{
|
||||
foreach ($users as $key => $value)
|
||||
{
|
||||
if (isset($value['id']) && $value['id'] === $id)
|
||||
{
|
||||
$user = $value;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!empty($user))
|
||||
{
|
||||
$this->set_response($user, REST_Controller::HTTP_OK); // OK (200) being the HTTP response code
|
||||
}
|
||||
else
|
||||
{
|
||||
$this->set_response([
|
||||
'status' => false,
|
||||
'message' => 'User could not be found'
|
||||
], REST_Controller::HTTP_NOT_FOUND); // NOT_FOUND (404) being the HTTP response code
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_post()
|
||||
{
|
||||
// $this->some_model->update_user( ... );
|
||||
$message = [
|
||||
'id' => 100, // Automatically generated by the model
|
||||
'name' => $this->post('name'),
|
||||
'email' => $this->post('email'),
|
||||
'message' => 'Added a resource'
|
||||
];
|
||||
|
||||
$this->set_response($message, REST_Controller::HTTP_CREATED); // CREATED (201) being the HTTP response code
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function users_delete()
|
||||
{
|
||||
$id = (int)$this->get('id');
|
||||
|
||||
// Validate the id.
|
||||
if ($id <= 0)
|
||||
{
|
||||
// Set the response and exit
|
||||
$this->response(null, REST_Controller::HTTP_BAD_REQUEST); // BAD_REQUEST (400) being the HTTP response code
|
||||
}
|
||||
|
||||
// $this->some_model->delete_something($id);
|
||||
$message = [
|
||||
'id' => $id,
|
||||
'message' => 'Deleted the resource'
|
||||
];
|
||||
|
||||
// NO_CONTENT (204) being the HTTP response code
|
||||
$this->set_response($message, REST_Controller::HTTP_NO_CONTENT);
|
||||
}
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* Whisperocity
|
||||
*
|
||||
* @package Whisperocity
|
||||
* @author WSP-Team
|
||||
* @copyright Copyright (c) 2015, Whisperocity
|
||||
* @license proprietary
|
||||
* @link http://whisperocity.com/
|
||||
* @since Version 1.0
|
||||
* @filesource
|
||||
*/
|
||||
|
||||
// ------------------------------------------------------------------------
|
||||
|
||||
if (! defined('BASEPATH')) exit('No direct script access allowed');
|
||||
|
||||
// This can be removed if you use __autoload() in config.php OR use Modular Extensions
|
||||
require APPPATH . '/libraries/REST_Controller.php';
|
||||
|
||||
/**
|
||||
* Handles ping attempts of applications
|
||||
*/
|
||||
class Ping extends REST_Controller {
|
||||
|
||||
/**
|
||||
* Ping-Controller constructor.
|
||||
* A more elaborate description of the constructor.
|
||||
*/
|
||||
function __construct()
|
||||
{
|
||||
// Construct the parent class
|
||||
parent::__construct();
|
||||
|
||||
// Configure limits on our controller methods
|
||||
// Ensure you have created the 'limits' table and enabled 'limits' within application/config/rest.php
|
||||
$this->methods['ping_get']['limit'] = 500; // 500 requests per hour per user/key
|
||||
}
|
||||
|
||||
/**
|
||||
* Responds to ping attempts of applications
|
||||
* @return string JSON which acknowledges the ping attempt
|
||||
* @example http://wsp.fortyseeds.at/backend/api/ping
|
||||
*/
|
||||
public function index_get()
|
||||
{
|
||||
$payload = [
|
||||
'success' => true,
|
||||
'message' => 'ping received'
|
||||
];
|
||||
|
||||
// Set the response and exit
|
||||
$this->response($payload, REST_Controller::HTTP_OK);
|
||||
}
|
||||
}
|
||||
@@ -25,7 +25,8 @@ class Test extends APIv1_Controller
|
||||
{
|
||||
$payload = [
|
||||
'success' => TRUE,
|
||||
'message' => 'API HTTP GET call test succeed'
|
||||
'message' => 'API HTTP GET call test succeed',
|
||||
'error' => 0
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
$this->response($payload, $httpstatus);
|
||||
@@ -42,7 +43,8 @@ class Test extends APIv1_Controller
|
||||
{
|
||||
$payload = [
|
||||
'success' => TRUE,
|
||||
'message' => 'API HTTP POST call test succeed'
|
||||
'message' => 'API HTTP POST call test succeed',
|
||||
'error' => 0
|
||||
];
|
||||
$httpstatus = REST_Controller::HTTP_OK;
|
||||
$this->response($payload, $httpstatus);
|
||||
|
||||
Reference in New Issue
Block a user