Commit d53aeed3 authored by nepski's avatar nepski

merge with qcc4

parents 8f444e4f d6336284
/tmp
app/config.php
.idea/
.htaccess
\ No newline at end of file
tests/tmp
<?php
/*
* Static class for REST API communication
*/
class Api {
// Generic get function for REST API
static function get($url) {
$curl = curl_init();
$url = API_BASE . $url;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// Generic post function for REST API
static function post($url, $data) {
$curl = curl_init();
$url = API_BASE . $url;
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
// Asking for campaign informations
static function get_campaign() {
$json = Api::get("campaign/" . CAMPAIGN_ID);
return json_decode($json, true);
}
// Searching a random or specific contact
static function get_contact($contact_id='') {
if ($contact_id!='') {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/contact/" . $contact_id);
} else {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/contact");
}
return json_decode($json, true);
}
// Asking for complete list of contacts
static function get_contacts() {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/contacts");
return json_decode($json, true);
}
// Asking for complete list of groups
static function get_groups() {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/groups");
return json_decode($json, true);
}
// Asking for complete list of feedback categories
static function get_feedback_categories() {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/feedback/categories");
return json_decode($json, true);
}
// Asking for arguments by language
static function get_arguments($language='en') {
$json = Api::get("campaign/" . CAMPAIGN_ID . "/arguments/" . $language);
return json_decode($json, true);
}
// Posting a feedback
static function post_feedback($contact_id, $comment, $feedback_category='') {
$data = array(
"contact_id"=>$contact_id,
"comment"=>$comment,
"feedback_category"=>$feedback_category
);
$json = Api::post("campaign/" . CAMPAIGN_ID . "/feedback/add", $data);
return json_decode($json, true);
}
};
......@@ -7,6 +7,15 @@ class Controller {
// Function called before routing
function beforeRoute($f3, $args) {
// Include configuration
require_once('config.php');
// Languages available
$languages = array(
array("iso" => "fr", "name"=>"Français"),
array("iso" => "en", "name"=>"English"),
);
$f3->set('languages', $languages);
}
// Function called after routing
......
......@@ -2,9 +2,12 @@
class Main extends Controller {
/*
Main page
*/
function show($f3,$args) {
* Home page
* Presents campaign information and a random Contact
*/
function home($f3,$args) {
$f3->set('campaign', Api::get_campaign());
$f3->set('contact', Api::get_contact());
$f3->set('block_content','home.html');
}
......
<?php
// Fake REST API from Campaign module
class RestApi {
function campaign($f3,$args) {
echo '{
"title": "Piphone Demo\'s campaign",
"description": "This is a demo campaign used to illustrate how the piphone works. This campaign does not grants you the possibility to contact anyone, it\'s just an ilustration of how it works.",
"start_date": "2014-01-01",
"end_date": "2014-12-31",
"default_language": "en"
}';
}
function contacts($f3,$args) {
// List of contacts
}
function contact($f3,$args) {
if ($f3->exists('PARAMS.contact')) {
// Specified contact
} else {
// Random contact
echo '{
"id": "4123",
"last_name": "ANDERSDOTTER",
"first_name": "Amelia",
"tel": "+322 28 45922",
"groups": [
{
"type": "Political group",
"name": "Greens/EFA",
"media": ""
},
{
"type": "National party",
"name": "Piratpartiet",
"media": ""
},
{
"type": "Country",
"name": "SE",
"media": ""
},
{
"type": "Committee",
"name": "ITRE",
"media": ""
},
{
"type": "Committee",
"name": "INTA",
"media": ""
},
{
"type": "Committee",
"name": "CONT",
"media": ""
}
]
}';
}
}
function groups($f3,$args) {
// List of groups available
}
function categories($f3,$args) {
// List of feedback categories
}
function feedback_add($f3,$args) {
// List of contacts
}
function arguments($f3,$args) {
// List of arguments
}
};
[routes]
; Main site
GET @home:/=Main->show
GET @home:/=Main->home
; Fake REST API
GET /restapi/campaign/@id=RestApi->campaign
GET /restapi/campaign/@id/contacts=RestApi->contacts
GET /restapi/campaign/@id/contact=RestApi->contact
GET /restapi/campaign/@id/contact/@contact=RestApi->contact
GET /restapi/campaign/@id/groups=RestApi->groups
GET /restapi/campaign/@id/feedback/categories=RestApi->categories
POST /restapi/campaign/@id/feedback/add=RestApi->feedback_add
GET /restapi/campaign/@id/arguments/@lang=RestApi->arguments
GET|POST @feedback:/feedback=Main->feedbackform
<!-- contact page -->
<p class="left"><img src="http://www.europarl.europa.eu/mepphoto/108570.jpg" alt="mep"></p>
<p id="name">{{ @contact.first_name }} {{ @contact.last_name }}</p>
<ul id="resume">
<li id="phone">Phone number: <a href="tel:+322 28 45922 ">{{ @contact.tel }}</a></li>
<repeat group="{{ @contact.groups }}" value="{{ @group }}">
<check if="{{ @group.type!='Committee' }}">
<li id="group"><span>{{ @group.type }}:</span><a href="https://memopol.lqdn.fr/europe/parliament/group/Greens/EFA/"><img src="https://memopol.lqdn.fr/static/img/groups/eu/GreensEFA.png" height="24" alt="Greens/EFA" /></a> - {{ @group.name }}</li>
</check>
</repeat>
</ul>
<ul id="committee">
<repeat group="{{ @contact.groups }}" value="{{ @group }}">
<check if="{{ @group.type=='Committee' }}">
<li title="Committee on Industry, Research and Energy"><a href="https://memopol.lqdn.fr/europe/parliament/committee/ITRE/">{{ @group.name }}</a></li>
</check>
</repeat>
</ul>
<p id="info"><a href="https://memopol.lqdn.fr/europe/parliament/deputy/AmeliaAndersdotter/">Get more info…</a></p>
<!-- contact page -->
<!-- content -->
Hello world!
<h1>{{ _("PiPhone") }}</h1>
<h2>{{ _("Call Contact for free - and make yourself heard") }}</h2>
<section>
<repeat group="{{ @languages }}" value="{{ @language }}">
<a href="/lang/{{ @language.iso }}">{{ @language.name }}</a> |
</repeat>
</section>
<h3>{{ @campaign.title }}</h3>
<section id="abstract">{{ @campaign.description }}</section>
<section id="contact">
<include href="contact.html" />
</section>
<!-- /content -->
<?php
/*
* Base class for testing
*
* Sub classes could set their own "set up" / "tear down" methods but should
* always call the parents one by using parent::setup() / parent::teardown()
*
*/
// Framework instance
require_once('../lib/base.php');
class BaseTest {
protected $test;
protected $f3;
function __construct() {
$this->f3 = Base::instance();
$this->f3->config('config.ini');
$this->f3->config('../app/routes.ini');
}
function setup() {
$this->test = new Test;
}
function teardown() {
// Nothing yet
}
function run($output="text") {
// Set Up
$this->setup();
// Adding all tests
foreach (get_class_methods($this) as $method) {
if (substr($method, 0, 4)=='test') {
try {
$this->$method();
} catch (Exception $e) {
//echo $e;
}
}
}
// Tear Down
$this->teardown();
// Getting tests results
$this->results = $this->test->results();
// Show results
switch ($output) {
default:
$this->output_text();
}
}
function sanitize_output($string) {
return str_replace('_', ' ', substr($string,5));
}
// Outputs results in raw text
function output_text() {
$failed = array();
echo "Testing " . $this->sanitize_output(get_class($this)) . "\n";
echo "================================================================\n";
echo "Launching " . count($this->results) . " tests:\n";
foreach ($this->results as $result) {
if ($result['status']) {
echo "P";
} else {
echo "F";
$failed[] = $result;
}
}
echo "\n----------------------------------------------------------------\n";
echo count($failed) . " on " . count($this->results) . " test(s) failed\n";
foreach ($failed as $result) {
echo "On " . $result['source'] . "\n";
echo " " . $result['text'] . "\n";
}
}
}
<?php
/*
* Launching tests for each test file
*/
$directory = opendir('.');
$file = readdir($directory);
while ($file) {
if (substr($file,0,4)=="test") {
include($file);
}
$file = readdir();
}
[globals]
; Folder containing classes
AUTOLOAD=../app/
; Folder containing templates
UI=../templates/
; Debug level
DEBUG=3
; Default language
LANGUAGE='fr'
; Global variables of website
<?php
/*
* Tests for Main class of piphone
*/
include('BaseTest.php');
class Test_Main_Class extends BaseTest {
function setup() {
parent::setup();
$this->f3->set('QUIET',TRUE);
}
function teardown() {
parent::teardown();
$this->f3->set('QUIET',FALSE);
}
function test_main_existence() {
$this->test->expect(
class_exists('Main'),
'Main class is not defined'
);
}
function test_main_get_campaign_return_array() {
$this->f3->mock('GET /');
$this->test->expect(
is_array($this->f3->get('campaign')),
'Do not get a campaign array'
);
$this->f3->clear('ERROR'); // clear any errors
}
function test_main_get_campaign_contains_known_fields() {
$fields = array("title", "description", "start_date", "end_date", "default_language");
$this->f3->mock('GET /');
foreach ($fields as $field) {
$this->test->expect(
array_key_exists($field, $this->f3->get('campaign')),
"Campaign should have a " . $field ." field"
);
}
$this->f3->clear('ERROR'); // clear any errors
}
}
// Launching tests
$tests = new Test_Main_Class();
$tests->run();
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