Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move controller code to separate file
- Loading branch information
1 parent
7eef3b2
commit e250e09
Showing
7 changed files
with
168 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |