Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed tests environment #60

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/limonade.php
Original file line number Diff line number Diff line change
Expand Up @@ -905,13 +905,12 @@ function error_http_status($errno)
function request_method($env = null)
{
if(is_null($env)) $env = env();
$m = array_key_exists('REQUEST_METHOD', $env['SERVER']) ? $env['SERVER']['REQUEST_METHOD'] : null;
if($m == "POST" && array_key_exists('_method', $env['POST']))
$m = strtoupper($env['POST']['_method']);
if(!in_array(strtoupper($m), request_methods()))
$m = isset($env['SERVER']['REQUEST_METHOD']) ? $env['SERVER']['REQUEST_METHOD'] : null;

if($m && !in_array(strtoupper($m), request_methods()))
{
trigger_error("'$m' request method is unknown or unavailable.", E_USER_WARNING);
$m = false;
$m = null;
}
return $m;
}
Expand Down Expand Up @@ -2466,7 +2465,7 @@ function file_mime_content_type($filename)
{
if($finfo = finfo_open(FILEINFO_MIME))
{
if($mime = finfo_file($finfo, $filename))
if($mime = @finfo_file($finfo, $filename))
{
finfo_close($finfo);
return $mime;
Expand Down
11 changes: 10 additions & 1 deletion lib/limonade/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* Constants and globals
*/
if(!defined('DS')) define("DS", DIRECTORY_SEPARATOR);
if(!defined('TESTS_DOC_ROOT')) define('TESTS_DOC_ROOT', 'http://localhost/');

if(!array_key_exists("limonade", $GLOBALS))
$GLOBALS["limonade"] = array();
Expand Down Expand Up @@ -82,6 +83,9 @@ function end_test_suite()
echo " Passes ".$passed_tests."/".$tests.", ";
echo " {$failures} failures for {$assertions} assertions.\n";
echo test_cli_format("===========================================================\n", 'white');
if ($failures > 0) {
exit(1);
}
}

/**
Expand Down Expand Up @@ -139,6 +143,10 @@ function end_test_case()
echo " {$test['failures']} failures for {$test['assertions']} assertions.\n";

echo "-----------------------------------------------------------\n";

if ($test['failures'] > 0) {
exit(1);
}
}
$GLOBALS["limonade"]["test_case_current"] = null;
}
Expand All @@ -164,7 +172,7 @@ function test_case_describe($msg = NULL)
* Returns all user test case functions
*
* @access private
* @return void
* @return array
*/
function test_case_all_func()
{
Expand Down Expand Up @@ -195,6 +203,7 @@ function test_case_execute_current()
while($func = array_shift($tests))
{
test_call_func(test_before_func_name());
echo '# ' . $func . PHP_EOL;
call_user_func($func);
}
}
Expand Down
104 changes: 3 additions & 101 deletions tests/all.php
Original file line number Diff line number Diff line change
@@ -1,106 +1,8 @@
<?php
require_once dirname(dirname(__FILE__)).'/lib/limonade.php';
require_once dirname(dirname(__FILE__)).'/lib/limonade/tests.php';

$basedir = dirname(__FILE__).DS;

if(!defined('TESTS_DOC_ROOT'))
{
# 1. CONFIG file is required
$config_file = dirname(__FILE__).'/config/config.php';
if(file_exists($config_file))
{
include $config_file;
$doc_root = $config['limonade_base_url']."tests/apps/";
}
else
{
echo <<<OUTPUT

ERROR: MISSING CONFIG FILE FOR TESTS
====================================

In order to run test, you must have a valid tests/config/config.php file.
Please copy tests/config/config.php.dist into tests/config/config.php and
set required values.

The \$config['limonade_base_url'] is required to run functional tests.

NOTE: the Limonade source code must be somewhere in your HTTP server public
folder in order to call testing limonade apps.

---

OUTPUT;
exit;
}

# 2. HTTP+CURL requirements
if(function_exists('curl_version'))
{
$url = $doc_root.'index.php';
$response = test_request($url, 'GET');
if($response)
{
$v = phpversion();
$curl_v = curl_version();
$cv = $curl_v['version'];
if($response == $v)
{

echo <<<OUTPUT

==== RUNNING LIMONADE TESTS [PHP $v — cURL $cv] =====

OUTPUT;
} else {
echo <<<OUTPUT

ERROR: Wrong response to HTTP request test
==========================================

Requesting $url
must return '$v' but returns this response:

$response

---

Your \$config['limonade_base_url'] might be wrong or maybe it's your HTTP
server configuration and/or php installation.
Please fix it in order to run tests.

---

OUTPUT;
exit;
}
}
else
{
exit;
}

}
else
{
echo <<<OUTPUT

ERROR: cURL Library is required
===============================

Please install PHP cURL library in order to run Limonade tests.


---

OUTPUT;
}


define('TESTS_DOC_ROOT', $doc_root);
}
require_once dirname(__DIR__).'/lib/limonade.php';
require_once dirname(__DIR__).'/lib/limonade/tests.php';

$basedir = __DIR__ . DS;

test_suite('Limonade');
require $basedir."tests.php";
Expand Down
3 changes: 2 additions & 1 deletion tests/file.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if(!defined('LIMONADE')){$h="HTTP/1.0 401 Unauthorized";header($h);die($h);}// Security check
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("File");
test_case_describe("Testing limonade file functions.");
Expand Down
5 changes: 3 additions & 2 deletions tests/functional.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if(!defined('LIMONADE')){$h="HTTP/1.0 401 Unauthorized";header($h);die($h);}// Security check
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("Functional");
test_case_describe("Functional tests");
Expand All @@ -13,7 +14,7 @@ function test_functional_request()
{
$response = test_request(TESTS_DOC_ROOT.'01-hello_world.php', 'GET', true);
//echo $response;
assert_header($response, 'Content-type', 'text/html');
assert_header($response, 'Content-type', 'text/html; charset=UTF-8');

$response = test_request(TESTS_DOC_ROOT.'03-routing.php/route0', 'GET', true);
assert_header($response, 'X-Limonade', LIM_NAME);
Expand Down
3 changes: 2 additions & 1 deletion tests/http.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if(!defined('LIMONADE')){$h="HTTP/1.0 401 Unauthorized";header($h);die($h);}// Security check
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("HTTP");
test_case_describe("Testing limonade HTTP utils functions.");
Expand Down
16 changes: 8 additions & 8 deletions tests/main.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?php
if(!defined('LIMONADE')){$h="HTTP/1.0 401 Unauthorized";header($h);die($h);}// Security check
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("Main");
test_case_describe("Testing limonade main functions.");
Expand Down Expand Up @@ -69,21 +70,20 @@ function test_main_env()
assert_true(array_key_exists($var, $env));
assert_true(is_array($env[$var]));
}

$_POST['_method'] = "PUT";
$_SERVER['REQUEST_METHOD'] = 'POST';
$_POST['my_var1'] = "value1";
$_POST['my_var2'] = "value2";

$env = env(null);
assert_equal($env['PUT']['my_var1'], "value1");
assert_equal($env['PUT']['my_var2'], "value2");
assert_equal($env['POST']['my_var1'], "value1");
assert_equal($env['POST']['my_var2'], "value2");
}

function test_main_app_file()
{
$app_file = strtolower(app_file());
$env = env();
assert_equal($app_file, strtolower($env['SERVER']['PWD'].'/'.$env['SERVER']['PHP_SELF']));
$app_file = strtolower(app_file());
$env = env();
assert_equal($app_file, strtolower(file_path(getcwd().'/'.$env['SERVER']['PHP_SELF'])));
}

function test_main_call_if_exists()
Expand Down
2 changes: 2 additions & 0 deletions tests/output.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("Output");
test_case_describe("Testing limonade output functions.");
Expand Down
21 changes: 10 additions & 11 deletions tests/request.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("Request");
test_case_describe("Testing limonade request functions.");
Expand Down Expand Up @@ -28,7 +30,7 @@ function test_request_method_is_allowed()
function test_request_method()
{
$env = env();
$env['SERVER']['REQUEST_METHOD'] = null;
$env['SERVER']['REQUEST_METHOD'] = 'UNKOWN';

assert_trigger_error("request_method");

Expand All @@ -40,20 +42,17 @@ function test_request_method()
assert_equal(request_method($env), $method);
}

$env['SERVER']['REQUEST_METHOD'] = "POST";

$env['POST']['_method'] = "PUT";
$env['SERVER']['REQUEST_METHOD'] = "PUT";
assert_equal(request_method($env), "PUT");
$env['POST']['_method'] = "DELETE";

$env['SERVER']['REQUEST_METHOD'] = "DELETE";
assert_equal(request_method($env), "DELETE");
$env['POST']['_method'] = "PATCH";

$env['SERVER']['REQUEST_METHOD'] = "PATCH";
assert_equal(request_method($env), "PATCH");

$env['POST']['_method'] = "UNKOWN";
assert_trigger_error('request_method', array($env));
assert_false(request_method());
$env['SERVER']['REQUEST_METHOD'] = "UNKOWN";
assert_null(request_method());
}

function test_request_uri()
Expand Down
2 changes: 2 additions & 0 deletions tests/router.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
require_once __DIR__ . '/../lib/limonade/tests.php';
require_once __DIR__ . '/../lib/limonade.php';

test_case("Router");
test_case_describe("Testing limonade router functions.");
Expand Down
5 changes: 5 additions & 0 deletions tests/startTestServer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

BASEDIR=$(dirname "$0")
cd $BASEDIR
php -S localhost:80 -t apps
5 changes: 3 additions & 2 deletions tests/tests.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php
require_once __DIR__ . '/../lib/limonade/tests.php';

test_case("Test");

Expand Down Expand Up @@ -61,9 +62,9 @@ function test_test_assert_request()
assert_true(defined('TESTS_DOC_ROOT'), "Undefined 'TESTS_DOC_ROOT' constant");
$response = test_request(TESTS_DOC_ROOT.'00-empty.php', 'GET', true);
assert_header($response, 'Content-type');
assert_header($response, 'Content-type', 'text/html');
assert_header($response, 'Content-type', 'text/html;charset=UTF-8');
assert_header($response, 'Content-Type');
assert_header($response, 'Content-Type', 'text/html');
assert_header($response, 'Content-Type', 'text/html;charset=UTF-8');
}

end_test_case();