private function request($method, $path, $data = array(), $optionalHeaders = array()) { // Capture STDOUT ob_start(); $options = array('REQUEST_METHOD' => strtoupper($method), 'PATH_INFO' => $path, 'SERVER_NAME' => 'local.dev'); if ($method === 'get') { $options['QUERY_STRING'] = http_build_query($data); } elseif (is_array($data)) { $options['slim.input'] = http_build_query($data); } else { $options['slim.input'] = $data; } // Prepare a mock environment Slim\Environment::mock(array_merge($options, $optionalHeaders)); $env = Slim\Environment::getInstance(); $this->app->router = new NoCacheRouter($this->app->router); $this->app->request = new Slim\Http\Request($env); // Custom headers $this->app->request->headers = new Slim\Http\Headers($env); $this->app->response = new Slim\Http\Response(); // Establish some useful references to the slim app properties $this->request = $this->app->request(); $this->response = $this->app->response(); // Execute our app $this->app->run(); // Return the application output. Also available in `response->body()` return ob_get_clean(); }
public function testDefaultLayoutFromAppConfiguration() { \Slim\Environment::mock(); $this->app = new \Slim\Slim(array('layout' => 'layout_from_app.php')); $output = $this->view->fetch('simple.php'); $this->assertEquals("<div>Hello World\n</div>", trim($output)); }
/** * @dataProvider authenticationDataProvider */ public function testRouteAuthentication($requestMethod, $path, $location, $hasIdentity, $identity, $httpStatus) { \Slim\Environment::mock(array('REQUEST_METHOD' => $requestMethod, 'PATH_INFO' => $path)); $this->auth->expects($this->once())->method('hasIdentity')->will($this->returnValue($hasIdentity)); $this->auth->expects($this->once())->method('getIdentity')->will($this->returnValue($identity)); $app = new \Slim\Slim(array('debug' => false)); $app->error(function (\Exception $e) use($app) { // Example of handling Auth Exceptions if ($e instanceof AuthException) { $app->response->setStatus($e->getCode()); $app->response->setBody($e->getMessage()); } }); $app->get('/', function () { }); $app->get('/member', function () { }); $app->delete('/member/photo/:id', function ($id) { }); $app->get('/admin', function () { }); $app->map('/login', function () { })->via('GET', 'POST')->name('login'); $app->add($this->middleware); ob_start(); $app->run(); ob_end_clean(); $this->assertEquals($httpStatus, $app->response->status()); $this->assertEquals($location, $app->response->header('location')); }
public function testAdminNavigation() { \Slim\Environment::mock(array('SCRIPT_NAME' => '', 'PATH_INFO' => '/admin')); $app = new \Slim\Slim(); $app->view(new \Slim\View()); $app->get('/admin', function () { echo 'Success'; }); $auththenticationService = $this->getMock('Zend\\Authentication\\AuthenticationService'); $auththenticationService->expects($this->once())->method('hasIdentity')->will($this->returnValue(true)); $mw = new Navigation($auththenticationService); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $response = $app->response(); $navigation = $app->view()->getData('navigation'); $this->assertNotNull($navigation); $this->assertInternalType('array', $navigation); $this->assertEquals(4, count($navigation)); $this->assertEquals('Home', $navigation[0]['caption']); $this->assertEquals('/', $navigation[0]['href']); $this->assertEquals('', $navigation[0]['class']); $this->assertEquals('Admin', $navigation[1]['caption']); $this->assertEquals('/admin', $navigation[1]['href']); $this->assertEquals('active', $navigation[1]['class']); $this->assertEquals('Settings', $navigation[2]['caption']); $this->assertEquals('/admin/settings', $navigation[2]['href']); $this->assertEquals('', $navigation[2]['class']); $this->assertEquals('Logout', $navigation[3]['caption']); $this->assertEquals('/logout', $navigation[3]['href']); $this->assertEquals('', $navigation[3]['class']); }
public function testUnknownFeatureGets404() { Environment::mock(array('PATH_INFO' => '/features/unknown')); $response = $this->app->invoke(); $this->assertEquals(json_encode(array("status" => 404, "statusText" => "Not Found", "description" => "Resource /features/unknown using GET method does not exist.")), $response->getBody()); $this->assertEquals(404, $response->getStatus()); }
/** * invalid credentials should be rejected **/ public function test_can_reject_login() { \Slim\Environment::mock(['PATH_INFO' => '/auth/login', 'REQUEST_METHOD' => 'POST', 'slim.input' => 'username=dubious&password=incorrect']); $this->kernel->app->call(); $status = $this->kernel->app->response->getStatus(); $this->assertEquals(401, $status); }
public function testRespond() { Environment::mock(array('REQUEST_METHOD' => 'GET')); $deal = new Deal(); $deal->respond('test'); $this->assertEquals('test', unserialize($deal->response()->body())); }
public function setUp() { parent::setUp(); /** Mock the Environment (taken from the Slim tests...) **/ \Slim\Environment::mock(array('SCRIPT_NAME' => '/foo', 'PATH_INFO' => '/bar', 'QUERY_STRING' => 'one=foo&two=bar', 'SERVER_NAME' => 'slimframework.com')); $this->app = new \Bistro\Swell\App(array('web.path' => '/subdirectory', 'database.dsn' => "sqlite::memory:")); }
protected function setUp() { define('SERVER_SCRIPT', 'unittest'); \Slim\Environment::mock(); \Application\Bootstrap::init(); \Library\SFacebook::init(); }
public function req($path = '/', $method = 'GET', $input = '', $option = []) { // $app->post() が $_POSTにfallbackするので対応 $_POST_OLD = []; if ($method === 'POST') { $_POST_OLD = $_POST; parse_str($input, $_POST); } // create slim mock with settings. \Slim\Environment::mock(array_merge(['REQUEST_METHOD' => $method, 'PATH_INFO' => $path, 'slim.input' => $input, 'SCRIPT_NAME' => '', 'QUERY_STRING' => '', 'SERVER_NAME' => 'localhost', 'SERVER_PORT' => 80, 'ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'ACCEPT_LANGUAGE' => 'ja,en;q=0.7,zh;q=0.3', 'ACCEPT_CHARSET' => 'UTF-8', 'USER_AGENT' => 'PHP UnitTest', 'REMOTE_ADDR' => '127.0.0.1', 'slim.url_scheme' => 'http', 'slim.errors' => @fopen('php://stderr', 'w')], $option)); $app = static::createSlim(); // \Slim\Slim::getInstance() response only FIRST MADE instance now(2014/05/27). // slim constructor DON'T overwrite \Slim\Slim::$apps when new slim instance // bellow code, force overwrite cached instance. // This is important when you use \Slim\Slim::getInstance(). // (I was falling in hole, when use Class controllers(slim>=2.4.0)) $app->setName('default'); // registration route to slim. static::registrationRoute($app); ob_start(); $app->run(); if ($method === 'POST') { $_POST = $_POST_OLD; } return ob_get_clean(); }
protected function setUrl($path, $params = '', $config = array()) { Environment::mock(array('REQUEST_METHOD' => 'GET', 'REMOTE_ADDR' => '127.0.0.1', 'SCRIPT_NAME' => '', 'PATH_INFO' => $path, 'QUERY_STRING' => $params, 'SERVER_NAME' => 'slim', 'SERVER_PORT' => 80, 'slim.url_scheme' => 'http', 'slim.input' => '', 'slim.errors' => fopen('php://stderr', 'w'), 'HTTP_HOST' => 'slim')); $this->env = Environment::getInstance(); $this->req = new Request($this->env); $this->res = new Response(); $this->app = new Slim(array_merge(array('controller.class_prefix' => '\\SlimController\\Tests\\Fixtures\\Controller', 'controller.class_suffix' => 'Controller', 'controller.method_suffix' => 'Action', 'controller.template_suffix' => 'php', 'templates.path' => __DIR__ . '/Fixtures/templates'), $config)); }
/** * * @return array ['status', 'headers', 'body'] */ protected function _getResponse($envArgs) { \Slim\Environment::mock($envArgs); $app = new \Slim\Slim(); new \Voce\Thermal\v1\API($app); $app->call(); return $app->response()->finalize(); }
public function testAuthenticateBad() { Environment::mock(array('PHP_AUTH_USER' => 'foo', 'PHP_AUTH_PW' => 'sizle')); $deal = new Deal(); $storage = new None(); $auth = new HttpAuth($storage, $deal); $auth->generateKeyPair('foo', 'bar'); $this->assertFalse($auth->authenticate()); }
protected function request($method, $path, $options = array()) { ob_start(); \Slim\Environment::mock(array_merge(array('REQUEST_METHOD' => $method, 'PATH_INFO' => $path, 'SERVER_NAME' => 'local.dev'), $options)); $this->request = $this->app->request(); $this->response = $this->app->response(); $this->app->run(); return ob_get_clean(); }
public function request($method, $path, $options = array()) { ob_start(); Environment::mock(array_merge(array('PATH_INFO' => $path, 'SERVER_NAME' => 'slim-test.dev', 'REQUEST_METHOD' => $method), $options)); $app = new Slim(); $this->app = $app; $this->request = $app->request(); $this->response = $app->response(); return ob_get_clean(); }
public function setUp() { //Remove environment mode if set unset($_ENV['SLIM_MODE']); //Reset session $_SESSION = array(); //Prepare default environment variables \Slim\Environment::mock(array('PATH_INFO' => '/bar', 'QUERY_STRING' => 'one=foo&two=bar', 'SERVER_NAME' => 'slimframework.com')); date_default_timezone_set('Asia/Dubai'); }
public function testBestFormatWithPriorities() { \Slim\Environment::mock(array('ACCEPT' => 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'negotiation.priorities' => array('xml'))); $s = new \Slim\Slim(); $s->add(new ContentNegotiation()); $s->run(); $env = \Slim\Environment::getInstance(); $bestFormat = $env['request.best_format']; $this->assertNotNull($bestFormat); $this->assertEquals('xml', $bestFormat); }
public function testUnkownHttpStatusExceptionGenerates500() { $app = new Application(); $app->get('/api/test/undefined-exception', function () { throw new Exception('Exception with unknown HTTP status', 999); }); Environment::mock(array('PATH_INFO' => '/api/test/undefined-exception')); $response = $app->invoke(); $this->assertEquals(json_encode(array('status' => 500, 'statusText' => 'Internal Server Error', 'description' => 'Exception with unknown HTTP status')), $response->getBody()); $this->assertEquals(500, $response->getStatus()); }
public function setUpApp($env = array(), $withCache = false) { \Slim\Environment::mock($env); $this->app = new App(); $this->dataConnectionMock = $this->getMock('\\MABI\\Testing\\MockDataConnection', array('findOneByField', 'query', 'insert', 'save', 'deleteByField', 'clearAll', 'getNewId', 'findAll', 'findAllByField')); $this->app->addDataConnection('default', $this->dataConnectionMock); if ($withCache) { $this->app->addCacheRepository('system', 'file', array('path' => 'TestApp/cache')); } $this->app->getErrorResponseDictionary()->overrideErrorResponses(new Errors()); }
public function testBadRoute() { \Slim\Environment::mock(array('REQUEST_METHOD' => 'GET', 'PATH_INFO' => Voce\Thermal\get_api_base() . 'v1/foobar')); $app = new \Slim\Slim(); $apiTest = new API_Test_v1($app); ob_start(); $apiTest->app->run(); ob_end_clean(); $response = $apiTest->app->response(); $this->assertObjectHasAttribute('error', json_decode($response->body())); }
/** * Test Language middleware with Accept-Language header */ public function testLanguageHeader() { \Slim\Environment::mock(array('HTTP_ACCEPT_LANGUAGE' => 'en-US,en;q=0.8,es;q=0.6')); $app = new \Slim\Slim(array()); $app->get('/', function () { }); $mw = new \Mnlg\Middleware\Language(); $mw->setApplication($app); $mw->setNextMiddleware($app); $mw->call(); $this->assertEquals($app->acceptLanguages, array('en-US' => 1.0, 'en' => 0.8, 'es' => 0.6)); }
/** * Verify basic behavior of register * * @test * @covers ::register * * @return void */ public function register() { $storage = new \OAuth2\Storage\Memory([]); $server = new \OAuth2\Server($storage, [], []); \Slim\Environment::mock(); $slim = new \Slim\Slim(); Token::register($slim, $server); $route = $slim->router()->getNamedRoute('token'); $this->assertInstanceOf('\\Slim\\Route', $route); $this->assertInstanceOf('\\Chadicus\\Slim\\OAuth2\\Routes\\Token', $route->getCallable()); $this->assertSame([\Slim\Http\Request::METHOD_POST], $route->getHttpMethods()); }
public function testSlimRequest() { $env = \Slim\Environment::mock(array('REQUEST_METHOD' => 'GET', 'X_REQUESTED_WITH' => 'foo', 'slim.input' => 'This is the body.', 'PATH_INFO' => '/bar/xyz')); $req = new \Slim\Http\Request($env); $this->assertEquals('GET', $req->getMethod()); $slimRequest = new SlimRequest($req); $this->assertEquals('GET', $slimRequest->getMethod()); $this->assertEquals('/bar/xyz', $slimRequest->getResource()); $this->assertEquals('This is the body.', $slimRequest->getBody()); $this->assertTrue($slimRequest->hasHeader('X_REQUESTED_WITH')); $this->assertEquals('foo', $slimRequest->getHeader('X_REQUESTED_WITH')); }
/** * @param string $path * @return Response */ protected function doRequest($method = "GET", $path = "/", $params = []) { $requestParams = array('PATH_INFO' => $path, 'REQUEST_METHOD' => $method); if ($method == "POST") { $requestParams['QUERY_STRING'] = http_build_query($params); $requestParams['slim.input'] = http_build_query($params); } Environment::mock($requestParams); $this->tiger = TigerApp::run(); $this->slim = $this->tiger->getSlimApp(); $response = $this->tiger->invoke(); return $response; }
public function testPathUtils() { $requestParams = array('PATH_INFO' => "/", 'REQUEST_METHOD' => "GET"); Environment::mock($requestParams); $this->assertEquals(APP_ROOT . "/build/logs/", TigerApp::LogRoot()); $this->assertEquals(APP_ROOT . "/templates/", TigerApp::TemplatesRoot()); // $this->assertEquals(APP_ROOT . "/templates", TigerApp::WebDiskRoot()); $this->assertEquals(APP_ROOT . "/public/", TigerApp::PublicRoot()); $this->assertEquals(APP_ROOT . "/public/cache/", TigerApp::PublicCacheRoot()); $this->assertEquals("localhost", TigerApp::WebHost()); $this->assertEquals(80, TigerApp::WebPort()); $this->assertEquals(false, TigerApp::WebIsSSL()); }
public function request($method, $path, $options = array()) { // Capture STDOUT ob_start(); // Prepare a mock environment Environment::mock(array_merge(array('REQUEST_METHOD' => $method, 'PATH_INFO' => $path, 'SERVER_NAME' => 'slim-test.dev'), $options)); $app = new \Slim\Slim(); $this->app = $app; $this->request = $app->request(); $this->response = $app->response(); // Return STDOUT return ob_get_clean(); }
/** * @param array $config * @return \Slim\Slim */ public function dispatch($config) { \Slim\Environment::mock(array('REQUEST_METHOD' => 'HEAD', 'PATH_INFO' => '/')); $config['debug'] = false; // ignore pretty exceptions $slim = new \Slim\Slim($config); $manager = new \Slim\Middleware\SessionManager($slim); $manager->setDbConnection($this->capsule->getConnection()); $session = new \Slim\Middleware\Session($manager); $slim->add($session); $slim->run(); return $slim; }
private function request($method, $path, $formVars = array(), $optionalHeaders = array()) { // Capture STDOUT ob_start(); // Prepare a mock environment \Slim\Environment::mock(array_merge(array('REQUEST_METHOD' => strtoupper($method), 'PATH_INFO' => $path, 'slim.input' => http_build_query($formVars)), $optionalHeaders)); // Establish some useful references to the slim app properties $this->request = $this->app->request(); $this->response = $this->app->response(); // Execute our app $this->app->run(); // Return the application output. Also available in `response->body()` return ob_get_clean(); }
public function testVisitAdminPageLoggedInSucceeds() { \Slim\Environment::mock(array('SCRIPT_NAME' => '/index.php', 'PATH_INFO' => '/admin')); $app = new \Slim\Slim(); $app->get('/admin', function () { echo 'Success'; }); $this->auththenticationService->expects($this->once())->method('hasIdentity')->will($this->returnValue(true)); $this->middleware->setApplication($app); $this->middleware->setNextMiddleware($app); $this->middleware->call(); $response = $app->response(); $this->assertTrue($response->isOk()); }
public function request($method, $path, $options = array()) { // Capture STDOUT ob_start(); // Prepare a mock environment \Slim\Environment::mock(array_merge(array('REQUEST_METHOD' => $method, 'PATH_INFO' => $path, 'SERVER_NAME' => 'local.dev'), $options)); // Establish some useful references to the slim app properties $this->request = $this->app->request(); $this->response = $this->app->response(); // Execute our app $this->app->run(); // Return the application output. Also available in `response->body()` return ob_get_clean(); }