Skip to content
Browse files

Added more tests to check and make sure things were working.

  • Loading branch information...
1 parent 5185d25 commit 0cd3bb8811ee2dd3789ee854dd589fd69419af1e @jmather jmather committed Oct 4, 2012
View
2 .gitignore
@@ -1,2 +1,4 @@
composer.lock
vendor
+.idea
+*.iml
View
8 Tests/AcceptHeaderRoutingTest.php → Tests/MultipleControllerRoutingTest.php
@@ -12,7 +12,7 @@
use Silex\RedirectableUrlMatcher;
use Pff\ServiceProvider\AcceptHeaderServiceProvider as AcceptHeader;
-class RoutingTest extends WebTestCase
+class MultipleControllerRoutingTest extends WebTestCase
{
/**
* Creates the application.
@@ -23,10 +23,10 @@ public function createApplication()
{
$app = new \Silex\Application();
- $app->register(new AcceptHeader\AcceptHeaderServiceProvider());
+ $app->register(new AcceptHeader\AcceptHeaderServiceProvider());
- /** @var $controllers1 VersionedRestControllerCollection */
+ /** @var $controllers1 ControllerCollection */
$controllers1 = $app['controllers_factory'];
$controllers1->get('/test', function($accept_header) use ($app) {
@@ -39,7 +39,7 @@ public function createApplication()
})->accept(array('application/ven.test.v1+json', 'application/ven.test.v1+xml'));
- /** @var $controllers1 VersionedRestControllerCollection */
+ /** @var $controllers1 ControllerCollection */
$controllers2 = $app['controllers_factory'];
$controllers1->get('/test', function($accept_header) use ($app) {
View
88 Tests/SingleControllerRoutingTest.php
@@ -0,0 +1,88 @@
+<?php
+
+namespace Pff\ServiceProvider\AcceptHeaderServiceProvider\Tests;
+
+use Silex\WebTestCase;
+use Symfony\Component\HttpKernel\HttpKernel;
+use Silex\Controller;
+use Silex\ControllerCollection;
+use Symfony\Component\HttpFoundation\Response;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpKernel\KernelEvents;
+use Silex\RedirectableUrlMatcher;
+use Pff\ServiceProvider\AcceptHeaderServiceProvider as AcceptHeader;
+
+class SingleControllerRoutingTest extends WebTestCase
+{
+ /**
+ * Creates the application.
+ *
+ * @return HttpKernel
+ */
+ public function createApplication()
+ {
+ $app = new \Silex\Application();
+
+ $app->register(new AcceptHeader\AcceptHeaderServiceProvider());
+
+
+ /** @var $controllers1 ControllerCollection */
+ $controllers1 = $app['controllers_factory'];
+
+ $controllers1->get('/test', function($accept_header) use ($app) {
+ if ($accept_header == 'application/ven.test.v1+json')
+ $cont = json_encode(array('content' => 'hello'));
+ else
+ $cont = '<content>hello</content>';
+
+ return new Response($cont, 200, array('Content-Type' => $accept_header));
+ })->accept(array('application/ven.test.v1+json', 'application/ven.test.v1+xml'));
+
+
+ $app->mount('/', $controllers1);
+
+
+ $app['debug'] = true;
+ unset($app['exception_handler']);
+
+ return $app;
+ }
+
+ public function testValidV1XMLCall()
+ {
+ $client = $this->createClient();
+
+ $crawler = $client->request('GET', '/test', array(), array(), array('HTTP_ACCEPT' => 'application/ven.test.v1+xml'));
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+ $result = $client->getResponse()->getContent();
+
+ $this->assertEquals('<content>hello</content>', $result, 'response is correct');
+ }
+
+ public function testValidV1JSONCall()
+ {
+ $client = $this->createClient();
+
+ $crawler = $client->request('GET', '/test', array(), array(), array('HTTP_ACCEPT' => 'application/ven.test.v1+json'));
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+
+ $result = $client->getResponse()->getContent();
+
+ $this->assertEquals('{"content":"hello"}', $result, 'response is correct');
+ }
+
+ /**
+ * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
+ */
+ public function testInvalidV3Call()
+ {
+ $client = $this->createClient();
+
+ $crawler = $client->request('GET', '/test', array(), array(), array('HTTP_ACCEPT' => 'application/ven.test.v3+xml'));
+
+ $this->assertEquals(404, $client->getResponse()->getStatusCode());
+ }
+}

0 comments on commit 0cd3bb8

Please sign in to comment.
Something went wrong with that request. Please try again.