Skip to content
Browse files

Move controller code to separate file

  • Loading branch information...
1 parent 7eef3b2 commit e250e091573bbf65fc0d4bce84de816aea262dd8 @davedevelopment committed Oct 3, 2012
View
15 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;
View
4 composer.json
@@ -3,6 +3,10 @@
"silex/silex": "*@dev"
},
+ "require-dev": {
+ "mockery/mockery": "*"
+ },
+
"autoload": {
"psr-0": {
"Demo": "src"
View
58 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": [
],
View
26 phpunit.xml.dist
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+
+<!-- http://www.phpunit.de/manual/current/en/appendixes.configuration.html -->
+<phpunit
+ backupGlobals = "false"
+ backupStaticAttributes = "false"
+ colors = "true"
+ convertErrorsToExceptions = "true"
+ convertNoticesToExceptions = "true"
+ convertWarningsToExceptions = "true"
+ processIsolation = "false"
+ stopOnFailure = "false"
+ syntaxCheck = "false"
+ bootstrap = "vendor/autoload.php" >
+
+ <testsuites>
+ <testsuite name="Project Test Suite">
+ <directory>tests/</directory>
+ </testsuite>
+ </testsuites>
+
+ <listeners>
+ <listener class="\Mockery\Adapter\Phpunit\TestListener" file="vendor/mockery/mockery/library/Mockery/Adapter/Phpunit/TestListener.php"></listener>
+ </listeners>
+
+</phpunit>
View
27 src/Demo/Controller/ControllerResolver.php
@@ -0,0 +1,27 @@
+<?php
+
+namespace Demo\Controller;
+
+use Silex\ControllerResolver as BaseControllerResolver;
+
+class ControllerResolver extends BaseControllerResolver
+{
+ protected function createController($controller)
+ {
+ if (false !== strpos($controller, '::')) {
+ return parent::createController($controller);
+ }
+
+ if (false === strpos($controller, ':')) {
+ throw new \LogicException(sprintf('Unable to parse the controller name "%s".', $controller));
+ }
+
+ list($service, $method) = explode(':', $controller, 2);
+
+ if (!isset($this->app[$service])) {
+ throw new \InvalidArgumentException(sprintf('Service "%s" does not exist.', $controller));
+ }
+
+ return array($this->app[$service], $method);
+ }
+}
View
21 src/Demo/Controller/PostController.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Demo\Controller;
+
+use Demo\Repository\PostRepository;
+use Symfony\Component\HttpFoundation\JsonResponse;
+
+class PostController
+{
+ protected $repo;
+
+ public function __construct(PostRepository $repo)
+ {
+ $this->repo = $repo;
+ }
+
+ public function indexJson()
+ {
+ return new JsonResponse($this->repo->findAll());
+ }
+}
View
21 tests/Demo/Test/Controller/PostControllerTest.php
@@ -0,0 +1,21 @@
+<?php
+
+namespace Demo\Test\Controller;
+
+use Demo\Controller\PostController;
+
+use Mockery as m;
+
+class PostControllerTest extends \PHPUnit_Framework_Testcase
+{
+ public function testIndexJson()
+ {
+ $repo = m::mock("Demo\Repository\PostRepository")
+ ->shouldReceive("findAll")
+ ->andReturn($posts = array(array("title" => "Dave")))
+ ->mock();
+
+ $controller = new PostController($repo);
+ $this->assertInstanceOf("Symfony\Component\HttpFoundation\JsonResponse", $controller->indexJson());
+ }
+}

0 comments on commit e250e09

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