Projekt

Obecné

Profil

« Předchozí | Další » 

Revize 2383

Přidáno uživatelem Ondřej Fibich před téměř 10 roky(ů)

Novinky:
- Httpful HTTP klient - http://phphttpclient.com/
- abstraktni trida pro snadne testovani API end pointu s Httpful
- unit test pro network link API end point

Zobrazit rozdíly:

freenetis/branches/1.2/application/controllers/api_endpoints/Network_Link.php
*/
class Network_Link_Api
{
/**
* @GET
*/
public function get_all()
{
return ORM::factory('link')->find_all()->as_arrays();
}
/**
* @GET
*/
public function get_all()
{
return ORM::factory('link')->find_all()->as_arrays();
}
/**
* @GET
......
*/
public function get_list_of_type($medium)
{
$mediums = array();
switch (strtolower($medium))
{
case 'roaming':
$mediums[] = Link_Model::MEDIUM_ROAMING;
break;
case 'air':
$mediums[] = Link_Model::MEDIUM_AIR;
break;
case 'fiber':
$mediums[] = Link_Model::MEDIUM_SINGLE_FIBER;
$mediums[] = Link_Model::MEDIUM_MULTI_FIBER;
break;
case 'cable':
$mediums[] = Link_Model::MEDIUM_CABLE;
break;
default:
return ResponseBuilder::not_found();
}
$mediums = array();
switch (strtolower($medium))
{
case 'roaming':
$mediums[] = Link_Model::MEDIUM_ROAMING;
break;
case 'air':
$mediums[] = Link_Model::MEDIUM_AIR;
break;
case 'fiber':
$mediums[] = Link_Model::MEDIUM_SINGLE_FIBER;
$mediums[] = Link_Model::MEDIUM_MULTI_FIBER;
break;
case 'cable':
$mediums[] = Link_Model::MEDIUM_CABLE;
break;
default:
return ResponseBuilder::not_found();
}
return ORM::factory('link')
->in('medium', $mediums)
->select_list();
->in('medium', $mediums)
->select_list();
}
/**
......
// hateoas links
$alink['_links'] = array
(
'app_view' => array
(
'href' => url::base() . '{lang}/links/show/' . $link->id,
'parameters' => array('lang')
)
'app_view' => array
(
'href' => url::base() . '{lang}/links/show/' . $link->id,
'parameters' => array('lang')
)
);
return $alink;
}
......
*/
public function get_items_subnets()
{
$ids = filter_input(INPUT_GET, 'ids');
if (empty($ids))
{
return ResponseBuilder::bad_request('empty ids argument');
}
$ids_pars = array_map('intval', explode(',', $ids));
if (count($ids) == 1)
{
$link = ORM::factory('link', $ids_pars[0]);
if ($link->id)
{
return $link->get_subnets_on_link()->as_array();
}
return ResponseBuilder::not_found();
}
$ids = filter_input(INPUT_GET, 'ids');
if (empty($ids))
{
return ResponseBuilder::bad_request('empty ids argument');
}
$ids_pars = array_map('intval', explode(',', $ids));
return ORM::factory('link')->get_subnets_on_link($ids_pars)->as_array();
}
......
*/
public function get_items_vlans()
{
$ids = filter_input(INPUT_GET, 'ids');
if (empty($ids))
{
return ResponseBuilder::bad_request('empty ids argument');
}
$ids_pars = array_map('intval', explode(',', $ids));
if (count($ids) == 1)
{
$link = ORM::factory('link', $ids_pars[0]);
if ($link->id)
{
return $link->get_vlans_on_link()->as_array();
}
return ResponseBuilder::not_found();
}
$ids = filter_input(INPUT_GET, 'ids');
if (empty($ids))
{
return ResponseBuilder::bad_request('empty ids argument');
}
$ids_pars = array_map('intval', explode(',', $ids));
return ORM::factory('link')->get_vlans_on_link($ids_pars)->as_array();
}
freenetis/branches/1.2/application/vendors/httpful/README.txt
Current version is 0.2.17 builded from https://github.com/nategood/httpful/releases using provided build script.
freenetis/branches/1.2/tests/application/controllers/api_endpoints/AbstractEndPointTestCase.php
<?php
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*/
require_once APPPATH . '/vendors/httpful/httpful.phar';
use \Httpful\Request;
/**
* Abstract test case for easy testing of API endpoints.
*
* @author Ondřej Fibich <fibich@freenetis.org>
*/
abstract class AbstractEndPointTestCase extends PHPUnit_Framework_TestCase
{
/**
* API user account username.
*/
const API_USERNAME = 'test_NL';
/**
* API user account password.
*/
const API_PASSWORD = '12345678901234567890123456789012';
/**
* Base path to FreenetIS API.
*
* @var string
*/
protected $base_path;
/**
* API account that is used for logging to API.
*
* @var Api_account_Model
*/
protected $api_account;
/**
* Defines authentification type that is used for connecting to API.
*
* @var string
*/
protected $auth_method;
/**
* Prepare base path and add API account.
*/
protected function setUp()
{
$this->base_path = Settings::get('protocol') . '://'
. Settings::get('domain') . Settings::get('suffix') . 'cs'
. Api_Controller::API_BASE_PATH;
$this->api_account = new Api_account_Model();
$this->api_account->allowed_paths = '/**'; // allow all
$this->api_account->enabled = TRUE;
$this->api_account->readonly = FALSE;
$this->api_account->username = self::API_USERNAME;
$this->api_account->token = self::API_PASSWORD;
$this->api_account->save_throwable();
$this->auth_method = 'authenticateWith'
. ucfirst(Settings::get('api_auth_type'));
}
/**
* Remove API account.
*/
protected function tearDown()
{
$this->api_account->delete();
}
/**
* Gets resource on given path, with given parameters, than test the response
* code and return the response.
*
* @param string $path path relative to base path
* @params array $params optional request parameters
* @param integer $expected_code optional expected HTTP response code
* @return \Httpful\Response
*/
protected function request_get($path = '', $params = array(),
$expected_code = 0)
{
if (!empty($params))
{
$params_str_array = array();
foreach ($params as $name => $value)
{
$params_str_array[] = urlencode($name) . '=' . urlencode($value);
}
$path .= '?' . implode('&', $params_str_array);
}
$rsp = Request::get($this->base_path . $path)
->{$this->auth_method}(self::API_USERNAME, self::API_PASSWORD)
->send();
$this->assertNotNull($rsp);
if ($expected_code > 0)
{
$this->assertEquals($expected_code, $rsp->code, 'GET failed expected');
}
return $rsp;
}
}
freenetis/branches/1.2/tests/application/controllers/api_endpoints/Network_LinkTest.php
<?php
/*
* This file is part of open source system FreenetIS
* and it is release under GPLv3 licence.
*
* More info about licence can be found:
* http://www.gnu.org/licenses/gpl-3.0.html
*
* More info about project can be found:
* http://www.freenetis.org/
*
*/
require_once __DIR__ . '/AbstractEndPointTestCase.php';
/**
* Generated by PHPUnit_SkeletonGenerator on 2014-11-24 at 11:59:20.
*/
class Network_Link_ApiTest extends AbstractEndPointTestCase
{
/**
* @covers Network_Link_Api::get_all
*/
public function testGet_all()
{
$rsp = $this->request_get('/network/link', NULL, 200);
$this->assertTrue(is_array($rsp->body));
}
/**
* @covers Network_Link_Api::get_list
*/
public function testGet_list()
{
$rsp = $this->request_get('/network/link/list', NULL, 200);
$this->assertTrue(is_array($rsp->body));
}
/**
* @covers Network_Link_Api::get_list_of_type
*/
public function testGet_list_of_type()
{
$this->request_get('/network/link/invalid_type/list', NULL, 404);
$rsp1 = $this->request_get('/network/link/roaming/list', NULL, 200);
$this->assertTrue(is_array($rsp1->body));
$rsp2 = $this->request_get('/network/link/air/list', NULL, 200);
$this->assertTrue(is_array($rsp2->body));
$rsp3 = $this->request_get('/network/link/fiber/list', NULL, 200);
$this->assertTrue(is_array($rsp3->body));
$rsp4 = $this->request_get('/network/link/cable/list', NULL, 200);
$this->assertTrue(is_array($rsp4->body));
}
/**
* @covers Network_Link_Api::get_item
*/
public function testGet_item()
{
$this->request_get('/111111', NULL, 404);
// TODO: add existing
}
/**
* @covers Network_Link_Api::get_items_subnets
*/
public function testGet_items_subnets()
{
$this->request_get('/network/link/subnets', NULL, 400); // ids missing
$this->request_get('/network/link/subnets',
array('ids' => ''), 400); // ids empty
$rsp1 = $this->request_get('/network/link/subnets',
array('ids' => '1111111111'), 200);
$this->assertTrue(is_array($rsp1->body));
$this->assertEquals(0, count($rsp1->body));
$rsp2 = $this->request_get('/network/link/subnets',
array('ids' => '111111111,111111112'), 200);
$this->assertTrue(is_array($rsp2->body));
$this->assertEquals(0, count($rsp2->body));
// TODO: add existing
}
/**
* @covers Network_Link_Api::get_items_vlans
*/
public function testGet_items_vlans()
{
$this->request_get('/network/link/vlans', NULL, 400); // ids missing
$this->request_get('/network/link/vlans?ids=', NULL, 400); // ids empty
$rsp1 = $this->request_get('/network/link/vlans',
array('ids' => '1111111111'), 200);
$this->assertTrue(is_array($rsp1->body));
$this->assertEquals(0, count($rsp1->body));
$rsp2 = $this->request_get('/network/link/vlans',
array('ids' => '111111111,111111112'), 200);
$this->assertTrue(is_array($rsp2->body));
$this->assertEquals(0, count($rsp2->body));
// TODO: add existing
}
/**
* @covers Network_Link_Api::count
*/
public function testCount()
{
$rsp = $this->request_get('/network/link/count', NULL, 200);
$this->assertTrue(isset($rsp->body->count));
}
}

Také k dispozici: Unified diff