public function testRoutePathCompilationFailure() { $this->klein_app->respond('/users/[i:id]/friends/[i:id]/', function () { echo 'yup'; }); $exception = null; try { $this->klein_app->dispatch(MockRequestFactory::create('/users/1738197/friends/7828316')); } catch (\Exception $e) { $exception = $e->getPrevious(); } $this->assertTrue($exception instanceof RoutePathCompilationException); $this->assertTrue($exception->getRoute() instanceof Route); }
public function testDeleteAlias() { $this->expectOutputString('1,2,'); // With path $this->klein_app->delete('/', function () { echo '1,'; }); // Without path $this->klein_app->delete(function () { echo '2,'; }); $this->klein_app->dispatch(MockRequestFactory::create('/', 'DELETE')); }
public function testIndexReferences() { $this->hive->configuration['security'] = false; $this->hive->resource('Hive\\Tests\\Models\\User'); $user = new User(); $user->email = '*****@*****.**'; $user->name = 'Test'; $post1 = new Post(); $post1->title = 'Test Title'; $post1->body = 'Test body'; $post1->user = $user; $post2 = new Post(); $post2->title = 'Test Title 2'; $post2->body = 'Test body 2'; $post2->user = $user; $this->hive->dm->persist($user); $this->hive->dm->persist($post1); $this->hive->dm->persist($post2); $this->hive->dm->persist($user); $this->hive->dm->flush(); /** @var Request $request */ $request = MockRequestFactory::create('/users/' . $user->id . '/posts', 'GET'); $request->headers()['content-type'] = 'application/json'; $response = new Response(); $this->hive->api($request, $response, false); $data = json_decode($response->body(), true); $this->assertEquals(200, $response->code()); $this->assertNotEmpty($data); $this->assertEquals($post2->title, $data[0]['title']); $this->assertEquals($post2->body, $data[0]['body']); $this->assertEquals($post1->title, $data[1]['title']); $this->assertEquals($post1->body, $data[1]['body']); $this->hive->dm->remove($post1); $this->hive->dm->remove($post2); $this->hive->dm->remove($user); $this->hive->dm->flush(); }
public function testCustomValidator() { // Add our custom validator $this->klein_app->service()->addValidator('donkey', function ($string, $color) { $regex_str = $color . '[-_]?donkey'; return preg_match('/' . $regex_str . '/', $string); }); $this->klein_app->respond('/[:test_param]', function ($request, $response, $service) { $service->validateParam('test_param')->notNull()->isDonkey('brown'); // We should only get here if we passed our validations echo 'yup!'; }); $this->assertSame('yup!', $this->dispatchAndReturnOutput(MockRequestFactory::create('/browndonkey'))); $this->assertSame('yup!', $this->dispatchAndReturnOutput(MockRequestFactory::create('/brown-donkey'))); $this->assertSame('yup!', $this->dispatchAndReturnOutput(MockRequestFactory::create('/brown_donkey'))); $this->assertSame('fail', $this->dispatchAndReturnOutput(MockRequestFactory::create('/bluedonkey'))); $this->assertSame('fail', $this->dispatchAndReturnOutput(MockRequestFactory::create('/blue-donkey'))); $this->assertSame('fail', $this->dispatchAndReturnOutput(MockRequestFactory::create('/blue_donkey'))); $this->assertSame('fail', $this->dispatchAndReturnOutput(MockRequestFactory::create('/brown_donk'))); }
public function testSendCallsFastCGIFinishRequest() { // Custom apc function implement_custom_apc_cache_functions(); $this->router->respond('/test', function () { }); $this->router->dispatch(MockRequestFactory::create('/test')); $this->assertSame(200, $this->router->response()->code()); }
public function testMockFactory() { // Test data $uri = '/test/uri'; $method = 'OPTIONS'; $params = array('get'); $cookies = array('cookies'); $server = array('server'); $files = array('files'); $body = 'body'; // Create the request $request = MockRequestFactory::create($uri, $method, $params, $cookies, $server, $files, $body); // Make sure our data's the same $this->assertSame($uri, $request->uri()); $this->assertSame($method, $request->method()); $this->assertSame($params, $request->paramsGet()->all()); $this->assertSame(array(), $request->paramsPost()->all()); $this->assertSame(array(), $request->paramsNamed()->all()); $this->assertSame($cookies, $request->cookies()->all()); $this->assertContains($cookies[0], $request->params()); $this->assertContains($server[0], $request->server()->all()); $this->assertSame($files, $request->files()->all()); $this->assertSame($body, $request->body()); }