Commit af53bcb4 authored by Fabien Potencier's avatar Fabien Potencier

renamed Framework to Application

parent 744af92b
...@@ -4,9 +4,9 @@ Silex is a simple web framework to develop simple websites: ...@@ -4,9 +4,9 @@ Silex is a simple web framework to develop simple websites:
require_once __DIR__.'/silex.phar'; require_once __DIR__.'/silex.phar';
use Silex\Framework; use Silex\Application;
$app = new Framework(); $app = new Application();
$app->get('/hello/{name}', function($name) { $app->get('/hello/{name}', function($name) {
return "Hello $name"; return "Hello $name";
......
...@@ -29,7 +29,7 @@ use Symfony\Component\Routing\Matcher\UrlMatcher; ...@@ -29,7 +29,7 @@ use Symfony\Component\Routing\Matcher\UrlMatcher;
* *
* @author Fabien Potencier <fabien@symfony.com> * @author Fabien Potencier <fabien@symfony.com>
*/ */
class Framework extends HttpKernel implements EventSubscriberInterface class Application extends HttpKernel implements EventSubscriberInterface
{ {
private $dispatcher; private $dispatcher;
private $routes; private $routes;
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
/**
* Application test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class ApplicationTest extends \PHPUnit_Framework_TestCase
{
public function testFluidInterface()
{
$application = new Application();
$returnValue = $application->match('/foo', function() {});
$this->assertSame($application, $returnValue, '->match() should return $this');
$returnValue = $application->get('/foo', function() {});
$this->assertSame($application, $returnValue, '->get() should return $this');
$returnValue = $application->post('/foo', function() {});
$this->assertSame($application, $returnValue, '->post() should return $this');
$returnValue = $application->put('/foo', function() {});
$this->assertSame($application, $returnValue, '->put() should return $this');
$returnValue = $application->delete('/foo', function() {});
$this->assertSame($application, $returnValue, '->delete() should return $this');
$returnValue = $application->before(function() {});
$this->assertSame($application, $returnValue, '->before() should return $this');
$returnValue = $application->after(function() {});
$this->assertSame($application, $returnValue, '->after() should return $this');
$returnValue = $application->error(function() {});
$this->assertSame($application, $returnValue, '->error() should return $this');
}
public function testGetRequest()
{
$application = new Application();
$application->get('/', function() {
return 'root';
});
$request = Request::create('/');
$application->handle($request);
$this->assertEquals($request, $application->getRequest());
}
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Framework; use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -29,25 +29,25 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -29,25 +29,25 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this; $test = $this;
$framework = new Framework(); $application = new Application();
$framework->before(function() use(&$i, $test) { $application->before(function() use(&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; $i++;
}); });
$framework->match('/foo', function() use(&$i, $test) { $application->match('/foo', function() use(&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $application->after(function() use(&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; $i++;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$framework->handle($request); $application->handle($request);
$this->assertEquals(3, $i); $this->assertEquals(3, $i);
} }
...@@ -56,19 +56,19 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -56,19 +56,19 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{ {
$i = 0; $i = 0;
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() use (&$i) { $application->match('/foo', function() use (&$i) {
$i++; $i++;
return new Response('foo'); return new Response('foo');
}); });
$framework->after(function() use(&$i) { $application->after(function() use(&$i) {
$i++; $i++;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$framework->handle($request); $application->handle($request);
$this->assertEquals(2, $i); $this->assertEquals(2, $i);
} }
...@@ -79,35 +79,35 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase ...@@ -79,35 +79,35 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$test = $this; $test = $this;
$framework = new Framework(); $application = new Application();
$framework->before(function() use(&$i, $test) { $application->before(function() use(&$i, $test) {
$test->assertEquals(0, $i); $test->assertEquals(0, $i);
$i++; $i++;
}); });
$framework->before(function() use(&$i, $test) { $application->before(function() use(&$i, $test) {
$test->assertEquals(1, $i); $test->assertEquals(1, $i);
$i++; $i++;
}); });
$framework->match('/foo', function() use(&$i, $test) { $application->match('/foo', function() use(&$i, $test) {
$test->assertEquals(2, $i); $test->assertEquals(2, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $application->after(function() use(&$i, $test) {
$test->assertEquals(3, $i); $test->assertEquals(3, $i);
$i++; $i++;
}); });
$framework->after(function() use(&$i, $test) { $application->after(function() use(&$i, $test) {
$test->assertEquals(4, $i); $test->assertEquals(4, $i);
$i++; $i++;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$framework->handle($request); $application->handle($request);
$this->assertEquals(5, $i); $this->assertEquals(5, $i);
} }
......
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Framework; use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -25,15 +25,15 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -25,15 +25,15 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{ {
public function testNoErrorHandler() public function testNoErrorHandler()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
try { try {
$request = Request::create('/foo'); $request = Request::create('/foo');
$framework->handle($request); $application->handle($request);
$this->fail('->handle() should not catch exceptions where no error handler was supplied'); $this->fail('->handle() should not catch exceptions where no error handler was supplied');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->assertEquals('foo exception', $e->getMessage()); $this->assertEquals('foo exception', $e->getMessage());
...@@ -42,71 +42,71 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -42,71 +42,71 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testOneErrorHandler() public function testOneErrorHandler()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$framework->error(function($e) { $application->error(function($e) {
return new Response('foo exception handler'); return new Response('foo exception handler');
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$this->checkRouteResponse($framework, '/foo', 'foo exception handler'); $this->checkRouteResponse($application, '/foo', 'foo exception handler');
} }
public function testMultipleErrorHandlers() public function testMultipleErrorHandlers()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$errors = 0; $errors = 0;
$framework->error(function($e) use (&$errors) { $application->error(function($e) use (&$errors) {
$errors++; $errors++;
}); });
$framework->error(function($e) use (&$errors) { $application->error(function($e) use (&$errors) {
$errors++; $errors++;
}); });
$framework->error(function($e) use (&$errors) { $application->error(function($e) use (&$errors) {
$errors++; $errors++;
return new Response('foo exception handler'); return new Response('foo exception handler');
}); });
$framework->error(function($e) use (&$errors) { $application->error(function($e) use (&$errors) {
// should not execute // should not execute
$errors++; $errors++;
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$this->checkRouteResponse($framework, '/foo', 'foo exception handler', 'should return the first response returned by an exception handler'); $this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should return the first response returned by an exception handler');
$this->assertEquals(3, $errors, 'should execute error handlers until a response is returned'); $this->assertEquals(3, $errors, 'should execute error handlers until a response is returned');
} }
public function testNoResponseErrorHandler() public function testNoResponseErrorHandler()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$errors = 0; $errors = 0;
$framework->error(function($e) use (&$errors) { $application->error(function($e) use (&$errors) {
$errors++; $errors++;
}); });
try { try {
$request = Request::create('/foo'); $request = Request::create('/foo');
$framework->handle($request); $application->handle($request);
$this->fail('->handle() should not catch exceptions where an empty error handler was supplied'); $this->fail('->handle() should not catch exceptions where an empty error handler was supplied');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->assertEquals('foo exception', $e->getMessage()); $this->assertEquals('foo exception', $e->getMessage());
...@@ -117,45 +117,45 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase ...@@ -117,45 +117,45 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testStringResponseErrorHandler() public function testStringResponseErrorHandler()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$framework->error(function($e) { $application->error(function($e) {
return 'foo exception handler'; return 'foo exception handler';
}); });
$request = Request::create('/foo'); $request = Request::create('/foo');
$this->checkRouteResponse($framework, '/foo', 'foo exception handler', 'should accept a string response from the error handler'); $this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
} }
public function testErrorHandlerException() public function testErrorHandlerException()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
throw new \RuntimeException('foo exception'); throw new \RuntimeException('foo exception');
}); });
$framework->error(function($e) { $application->error(function($e) {
throw new \RuntimeException('foo exception handler exception'); throw new \RuntimeException('foo exception handler exception');
}); });
try { try {
$request = Request::create('/foo'); $request = Request::create('/foo');
$this->checkRouteResponse($framework, '/foo', 'foo exception handler', 'should accept a string response from the error handler'); $this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
$this->fail('->handle() should not catch exceptions thrown from an error handler'); $this->fail('->handle() should not catch exceptions thrown from an error handler');
} catch (\RuntimeException $e) { } catch (\RuntimeException $e) {
$this->assertEquals('foo exception handler exception', $e->getMessage()); $this->assertEquals('foo exception handler exception', $e->getMessage());
} }
} }
protected function checkRouteResponse($framework, $path, $expectedContent, $method = 'get', $message = null) protected function checkRouteResponse($application, $path, $expectedContent, $method = 'get', $message = null)
{ {
$request = Request::create($path, $method); $request = Request::create($path, $method);
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message); $this->assertEquals($expectedContent, $response->getContent(), $message);
} }
} }
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests;
use Silex\Framework;
use Symfony\Component\HttpFoundation\Request;
/**
* Framework test cases.
*
* @author Igor Wiedler <igor@wiedler.ch>
*/
class FrameworkTest extends \PHPUnit_Framework_TestCase
{
public function testFluidInterface()
{
$framework = new Framework();
$returnValue = $framework->match('/foo', function() {});
$this->assertSame($framework, $returnValue, '->match() should return $this');
$returnValue = $framework->get('/foo', function() {});
$this->assertSame($framework, $returnValue, '->get() should return $this');
$returnValue = $framework->post('/foo', function() {});
$this->assertSame($framework, $returnValue, '->post() should return $this');
$returnValue = $framework->put('/foo', function() {});
$this->assertSame($framework, $returnValue, '->put() should return $this');
$returnValue = $framework->delete('/foo', function() {});
$this->assertSame($framework, $returnValue, '->delete() should return $this');
$returnValue = $framework->before(function() {});
$this->assertSame($framework, $returnValue, '->before() should return $this');
$returnValue = $framework->after(function() {});
$this->assertSame($framework, $returnValue, '->after() should return $this');
$returnValue = $framework->error(function() {});
$this->assertSame($framework, $returnValue, '->error() should return $this');
}
public function testGetRequest()
{
$framework = new Framework();
$framework->get('/', function() {
return 'root';
});
$request = Request::create('/');
$framework->handle($request);
$this->assertEquals($request, $framework->getRequest());
}
}
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Framework; use Silex\Application;
use Symfony\Component\HttpFoundation\Request; use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response; use Symfony\Component\HttpFoundation\Response;
...@@ -27,64 +27,64 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -27,64 +27,64 @@ class RouterTest extends \PHPUnit_Framework_TestCase
{ {
public function testMapRouting() public function testMapRouting()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
return 'foo'; return 'foo';
}); });
$framework->match('/bar', function() { $application->match('/bar', function() {
return 'bar'; return 'bar';
}); });
$framework->match('/', function() { $application->match('/', function() {
return 'root'; return 'root';
}); });
$this->checkRouteResponse($framework, '/foo', 'foo'); $this->checkRouteResponse($application, '/foo', 'foo');
$this->checkRouteResponse($framework, '/bar', 'bar'); $this->checkRouteResponse($application, '/bar', 'bar');
$this->checkRouteResponse($framework, '/', 'root'); $this->checkRouteResponse($application, '/', 'root');
} }
public function testStatusCode() public function testStatusCode()
{ {
$framework = new Framework(); $application = new Application();
$framework->put('/created', function() { $application->put('/created', function() {
return new Response('', 201); return new Response('', 201);
}); });
$framework->match('/forbidden', function() { $application->match('/forbidden', function() {
return new Response('', 403); return new Response('', 403);
}); });
$framework->match('/not_found', function() { $application->match('/not_found', function() {
return new Response('', 404); return new Response('', 404);
}); });
$request = Request::create('/created', 'put'); $request = Request::create('/created', 'put');
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertEquals(201, $response->getStatusCode()); $this->assertEquals(201, $response->getStatusCode());
$request = Request::create('/forbidden'); $request = Request::create('/forbidden');
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertEquals(403, $response->getStatusCode()); $this->assertEquals(403, $response->getStatusCode());
$request = Request::create('/not_found'); $request = Request::create('/not_found');
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertEquals(404, $response->getStatusCode()); $this->assertEquals(404, $response->getStatusCode());
} }
public function testRedirect() public function testRedirect()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/redirect', function() { $application->match('/redirect', function() {
return new RedirectResponse('/target'); return new RedirectResponse('/target');
}); });
$request = Request::create('/redirect'); $request = Request::create('/redirect');
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertTrue($response->isRedirected('/target')); $this->assertTrue($response->isRedirected('/target'));
} }
...@@ -93,68 +93,68 @@ class RouterTest extends \PHPUnit_Framework_TestCase ...@@ -93,68 +93,68 @@ class RouterTest extends \PHPUnit_Framework_TestCase
*/ */
public function testMissingRoute() public function testMissingRoute()
{ {
$framework = new Framework(); $application = new Application();
$request = Request::create('/baz'); $request = Request::create('/baz');
$framework->handle($request); $application->handle($request);
} }
public function testMethodRouting() public function testMethodRouting()
{ {
$framework = new Framework(); $application = new Application();
$framework->match('/foo', function() { $application->match('/foo', function() {
return 'foo'; return 'foo';
}); });
$framework->match('/bar', function() { $application->match('/bar', function() {
return 'bar'; return 'bar';
}, 'GET|POST'); }, 'GET|POST');
$framework->get('/resource', function() { $application->get('/resource', function() {
return 'get resource'; return 'get resource';
}); });
$framework->post('/resource', function() { $application->post('/resource', function() {
return 'post resource'; return 'post resource';
}); });
$framework->put('/resource', function() { $application->put('/resource', function() {
return 'put resource'; return 'put resource';
}); });
$framework->delete('/resource', function() { $application->delete('/resource', function() {
return 'delete resource'; return 'delete resource';
}); });
$this->checkRouteResponse($framework, '/foo', 'foo'); $this->checkRouteResponse($application, '/foo', 'foo');
$this->checkRouteResponse($framework, '/bar', 'bar'); $this->checkRouteResponse($application, '/bar', 'bar');
$this->checkRouteResponse($framework, '/bar', 'bar', 'post'); $this->checkRouteResponse($application, '/bar', 'bar', 'post');
$this->checkRouteResponse($framework, '/resource', 'get resource'); $this->checkRouteResponse($application, '/resource', 'get resource');
$this->checkRouteResponse($framework, '/resource', 'post resource', 'post'); $this->checkRouteResponse($application, '/resource', 'post resource', 'post');
$this->checkRouteResponse($framework, '/resource', 'put resource', 'put'); $this->checkRouteResponse($application, '/resource', 'put resource', 'put');
$this->checkRouteResponse($framework, '/resource', 'delete resource', 'delete'); $this->checkRouteResponse($application, '/resource', 'delete resource', 'delete');
} }
public function testRequestShouldBeStoredRegardlessOfRouting() { public function testRequestShouldBeStoredRegardlessOfRouting() {
$framework = new Framework(); $application = new Application();
$framework->get('/foo', function() use ($framework) { $application->get('/foo', function() use ($application) {
return new Response($framework->getRequest()->getRequestUri()); return new Response($application->getRequest()->getRequestUri());
}); });
$framework->error(function($e) use ($framework) { $application->error(function($e) use ($application) {
return new Response($framework->getRequest()->getRequestUri()); return new Response($application->getRequest()->getRequestUri());
}); });
foreach(array('/foo', '/bar') as $path) { foreach(array('/foo', '/bar') as $path) {
$request = Request::create($path); $request = Request::create($path);
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertContains($path, $response->getContent()); $this->assertContains($path, $response->getContent());
} }
} }
protected function checkRouteResponse($framework, $path, $expectedContent, $method = 'get', $message = null) protected function checkRouteResponse($application, $path, $expectedContent, $method = 'get', $message = null)
{ {
$request = Request::create($path, $method); $request = Request::create($path, $method);
$response = $framework->handle($request); $response = $application->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message); $this->assertEquals($expectedContent, $response->getContent(), $message);
} }
} }
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
namespace Silex\Tests; namespace Silex\Tests;
use Silex\Framework; use Silex\Application;
use Silex\WebTestCase; use Silex\WebTestCase;
/** /**
...@@ -23,7 +23,7 @@ class WebTestCaseTest extends WebTestCase ...@@ -23,7 +23,7 @@ class WebTestCaseTest extends WebTestCase
{ {
public function createApp() public function createApp()
{ {
$app = new Framework(); $app = new Application();
$app->match('/hello', function() { $app->match('/hello', function() {
return 'world'; return 'world';
......
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