<?php namespace tests\aint\http_test; \aint\test\require_mock('aint/http.php', ['namespace aint\\http' => 'namespace ' . __NAMESPACE__, '\'header\'' => '\'\\' . __NAMESPACE__ . '\\header\'', ' header(' => ' \\' . __NAMESPACE__ . '\\header(']); function header($string) { \tests\aint\http_test\http_test::$header_called_with[] = $string; } use tests\aint\http_test as http; class http_test extends \PHPUnit_Framework_TestCase { public function test_build_request_from_globals() { // emulating the global environment $_SERVER['REQUEST_URI'] = '/albums/edit/id/1/?a=3&b=4'; $_SERVER['REQUEST_METHOD'] = 'POST'; $_GET['a'] = 3; $_GET['b'] = 4; $_POST['x'] = 4; $_POST['y'] = 5; $_SERVER['HTTP_CONTENT_TYPE'] = 'application/xml'; $_SERVER['HTTP_ACCEPT'] = 'application/json'; $request = http\build_request_from_globals(); $expected = [http\request_scheme => 'http', http\request_body => '', http\request_path => 'albums/edit/id/1', http\request_params => ['a' => 3, 'b' => 4, 'x' => 4, 'y' => 5], http\request_method => 'POST', http\request_headers => array('Content-Type' => 'application/xml', 'Accept' => 'application/json')]; $this->assertEquals($expected, $request); } public function test_is_post_get_delete_put() { // post $request = [http\request_method => http\request_method_post];
<?php namespace tests\aint\mvc\dispatching_dispatch_http_default_router_test; \aint\test\require_mock('aint/mvc/dispatching.php', ['namespace aint\\mvc\\dispatching' => 'namespace ' . __NAMESPACE__, 'function dispatch_http(' => 'function run_not_needed(', ' routing\\route_root(' => ' \\' . __NAMESPACE__ . '\\routing_route_root(']); function routing_route_root($request, $default_index_action) { return [$request, $default_index_action]; } function dispatch_http($routers, $actions_namespace, $error_handler) { dispatching_dispatch_http_default_router_test::$run_params = [$routers, $actions_namespace, $error_handler]; } use tests\aint\mvc\dispatching_dispatch_http_default_router_test as dispatching; class dispatching_dispatch_http_default_router_test extends \PHPUnit_Framework_TestCase { public static $run_params; public function test_run_default() { // testing that run_default function creates a callable wrapping route_root // using default_index_action // also testing that the function calls "run" function from the same package // with parameters required $error_handler = function () { }; dispatching\dispatch_http_default_router('actions_namespace', $error_handler); $this->assertEquals('actions_namespace', self::$run_params[1]); $this->assertEquals($error_handler, self::$run_params[2]); $routers = self::$run_params[0]; $this->assertEquals('\\aint\\mvc\\routing\\route_segment', $routers[0]); }
<?php // Stubbing functions namespace tests\aint\mvc\dispatching_dispatch_http_test; \aint\test\require_mock('aint/mvc/dispatching.php', ['namespace aint\\mvc\\dispatching' => 'namespace ' . __NAMESPACE__, 'function dispatch_request(' => 'function dispatch_not_needed(', ' http\\send_response(' => ' \\' . __NAMESPACE__ . '\\http_send_response(', ' http\\build_request_from_globals(' => ' \\' . __NAMESPACE__ . '\\http_build_request_from_globals(']); function dispatch_request($request, $routers, $actions_namespace, $error_handler) { dispatching_dispatch_http_test::$dispatch_params = [$request, $routers, $actions_namespace, $error_handler]; return 'response to be sent'; } function http_build_request_from_globals() { return 'request built from globals'; } function http_send_response($response) { dispatching_dispatch_http_test::$send_response_param = $response; } use tests\aint\mvc\dispatching_dispatch_http_test as dispatching; class dispatching_dispatch_http_test extends \PHPUnit_Framework_TestCase { public static $dispatch_params; public static $send_response_param; public function test_run() { // testing that dispatch method is called with http\build_request_from_globals // and that the result is sent via http\send_response function // we're stubbing all these functions for the test $error_handler = function () { };