From e250e091573bbf65fc0d4bce84de816aea262dd8 Mon Sep 17 00:00:00 2001 From: Dave Marshall Date: Wed, 3 Oct 2012 00:31:07 +0100 Subject: [PATCH] Move controller code to separate file --- app/bootstrap.php | 15 ++++- composer.json | 4 ++ composer.lock | 58 ++++++++++++++++++- phpunit.xml.dist | 26 +++++++++ src/Demo/Controller/ControllerResolver.php | 27 +++++++++ src/Demo/Controller/PostController.php | 21 +++++++ .../Test/Controller/PostControllerTest.php | 21 +++++++ 7 files changed, 168 insertions(+), 4 deletions(-) create mode 100644 phpunit.xml.dist create mode 100644 src/Demo/Controller/ControllerResolver.php create mode 100644 src/Demo/Controller/PostController.php create mode 100644 tests/Demo/Test/Controller/PostControllerTest.php diff --git a/app/bootstrap.php b/app/bootstrap.php index 12815f6..44dc4cd 100644 --- a/app/bootstrap.php +++ b/app/bootstrap.php @@ -2,15 +2,26 @@ use Silex\Application; use Demo\Repository\PostRepository; +use Demo\Controller\ControllerResolver; +use Demo\Controller\PostController; $app = new Application; +/** + * Custom controller resolver + */ +$app['resolver'] = $app->share(function () use ($app) { + return new ControllerResolver($app, $app['logger']); +}); + $app['posts.repository'] = $app->share(function() { return new PostRepository; }); -$app->get('/posts.json', function() use ($app) { - return $app->json($app['posts.repository']->findAll()); +$app['posts.controller'] = $app->share(function() use ($app) { + return new PostController($app['posts.repository'], $app); }); +$app->get('/posts.json', "posts.controller:indexJson"); + return $app; diff --git a/composer.json b/composer.json index 6c71a78..502aa46 100644 --- a/composer.json +++ b/composer.json @@ -3,6 +3,10 @@ "silex/silex": "*@dev" }, + "require-dev": { + "mockery/mockery": "*" + }, + "autoload": { "psr-0": { "Demo": "src" diff --git a/composer.lock b/composer.lock index 82d8cbc..cea0628 100644 --- a/composer.lock +++ b/composer.lock @@ -1,5 +1,5 @@ { - "hash": "624fedf235cce3b7fca702b8609b8f8c", + "hash": "35504242625e525f30939be51bb0ca33", "packages": [ { "name": "pimple/pimple", @@ -353,7 +353,61 @@ "homepage": "http://symfony.com" } ], - "packages-dev": null, + "packages-dev": [ + { + "name": "mockery/mockery", + "version": "0.7.2", + "source": { + "type": "git", + "url": "git://github.com/padraic/mockery.git", + "reference": "0.7.2" + }, + "dist": { + "type": "zip", + "url": "https://github.com/padraic/mockery/zipball/0.7.2", + "reference": "0.7.2", + "shasum": "" + }, + "require": { + "php": ">=5.3.2" + }, + "suggest": { + "Hamcrest": "1.0.0" + }, + "time": "2012-01-24 20:22:39", + "type": "library", + "installation-source": "dist", + "autoload": { + "psr-0": { + "Mockery": "library/" + } + }, + "license": [ + "New BSD" + ], + "authors": [ + { + "name": "Pádraic Brady", + "email": "padraic.brady@gmail.com", + "homepage": "http://blog.astrumfutura.com" + } + ], + "description": "Mockery is a simple yet flexible PHP mock object framework for use in unit testing with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a test double framework with a succint API capable of clearly defining all possible object operations and interactions using a human readable Domain Specific Language (DSL). Designed as a drop in alternative to PHPUnit's phpunit-mock-objects library, Mockery is easy to integrate with PHPUnit and can operate alongside phpunit-mock-objects without the World ending.", + "homepage": "http://github.com/padraic/mockery", + "keywords": [ + "testing", + "library", + "BDD", + "TDD", + "test", + "mockery", + "mock", + "stub", + "test double", + "mock objects" + ] + } + ], "aliases": [ ], diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..59352ec --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,26 @@ + + + + + + + + tests/ + + + + + + + + diff --git a/src/Demo/Controller/ControllerResolver.php b/src/Demo/Controller/ControllerResolver.php new file mode 100644 index 0000000..cb236ae --- /dev/null +++ b/src/Demo/Controller/ControllerResolver.php @@ -0,0 +1,27 @@ +app[$service])) { + throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $controller)); + } + + return array($this->app[$service], $method); + } +} diff --git a/src/Demo/Controller/PostController.php b/src/Demo/Controller/PostController.php new file mode 100644 index 0000000..8ca731b --- /dev/null +++ b/src/Demo/Controller/PostController.php @@ -0,0 +1,21 @@ +repo = $repo; + } + + public function indexJson() + { + return new JsonResponse($this->repo->findAll()); + } +} diff --git a/tests/Demo/Test/Controller/PostControllerTest.php b/tests/Demo/Test/Controller/PostControllerTest.php new file mode 100644 index 0000000..8c034eb --- /dev/null +++ b/tests/Demo/Test/Controller/PostControllerTest.php @@ -0,0 +1,21 @@ +shouldReceive("findAll") + ->andReturn($posts = array(array("title" => "Dave"))) + ->mock(); + + $controller = new PostController($repo); + $this->assertInstanceOf("Symfony\Component\HttpFoundation\JsonResponse", $controller->indexJson()); + } +}