-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from adriansuter/patch-container-controllers
Patch container controllers
- Loading branch information
Showing
13 changed files
with
279 additions
and
44 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
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,38 @@ | ||
<?php | ||
|
||
namespace App\Controllers; | ||
|
||
use Psr\Http\Message\ResponseInterface as Response; | ||
use Slim\Views\Twig; | ||
|
||
abstract class AbstractTwigController extends AbstractController | ||
{ | ||
/** | ||
* @var Twig | ||
*/ | ||
protected $twig; | ||
|
||
/** | ||
* AbstractController constructor. | ||
* | ||
* @param Twig $twig | ||
*/ | ||
public function __construct(Twig $twig) | ||
{ | ||
$this->twig = $twig; | ||
} | ||
|
||
/** | ||
* Render the template and write it to the response. | ||
* | ||
* @param Response $response | ||
* @param string $template | ||
* @param array $renderData | ||
* | ||
* @return Response | ||
*/ | ||
protected function render(Response $response, string $template, array $renderData = []): Response | ||
{ | ||
return $this->twig->render($response, $template, $renderData); | ||
} | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
use App\Controllers\ExceptionDemoController; | ||
use App\Controllers\HelloController; | ||
use App\Controllers\HomeController; | ||
use App\Preferences; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Log\LoggerInterface; | ||
|
||
return [ | ||
ExceptionDemoController::class => function (ContainerInterface $container): ExceptionDemoController { | ||
return new ExceptionDemoController(); | ||
}, | ||
HelloController::class => function (ContainerInterface $container): HelloController { | ||
return new HelloController($container->get('view'), $container->get(LoggerInterface::class)); | ||
}, | ||
HomeController::class => function (ContainerInterface $container): HomeController { | ||
return new HomeController($container->get('view'), $container->get(Preferences::class)); | ||
} | ||
]; |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests; | ||
|
||
use App\Controllers\ExceptionDemoController; | ||
use ErrorException; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
|
||
class ExceptionDemoControllerTest extends TestCase | ||
{ | ||
/** | ||
* @expectedException ErrorException | ||
*/ | ||
public function testInvoke() | ||
{ | ||
$exceptionDemoController = new ExceptionDemoController(); | ||
|
||
$serverRequest = $this->createMock(ServerRequestInterface::class); | ||
$response = $this->createMock(ResponseInterface::class); | ||
|
||
$exceptionDemoController($serverRequest, $response, []); | ||
} | ||
} |
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,74 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests; | ||
|
||
use App\Controllers\HelloController; | ||
use Prophecy\Argument; | ||
use Prophecy\Prophecy\MethodProphecy; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\ServerRequestInterface; | ||
use Psr\Log\LoggerInterface; | ||
use Slim\Views\Twig; | ||
|
||
class HelloControllerTest extends TestCase | ||
{ | ||
/** | ||
* @return array | ||
*/ | ||
public function argsDataProvider(): array | ||
{ | ||
return [ | ||
['Slim'], | ||
['World'] | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider argsDataProvider | ||
* | ||
* @param string $argName | ||
*/ | ||
public function testInvoke(string $argName) | ||
{ | ||
$self = $this; | ||
$twigProphecy = $this->prophesize(Twig::class); | ||
$twigProphecy->addMethodProphecy( | ||
(new MethodProphecy( | ||
$twigProphecy, | ||
'render', | ||
[Argument::type(ResponseInterface::class), Argument::type('string'), Argument::type('array')] | ||
))->will(function ($arguments) use ($self, $argName) { | ||
$self->assertSame('hello.twig', $arguments[1]); | ||
$self->assertSame([ | ||
'pageTitle' => 'Hello ' . $argName, | ||
'name' => $argName | ||
], $arguments[2]); | ||
|
||
return $arguments[0]; | ||
}) | ||
); | ||
|
||
$loggerProphecy = $this->prophesize(LoggerInterface::class); | ||
$loggerProphecy->addMethodProphecy( | ||
(new MethodProphecy($loggerProphecy, 'debug', [Argument::type('string')])) | ||
->will(function ($arguments) use ($self, $argName) { | ||
$self->assertSame('Hello "' . $argName . '"', $arguments[0]); | ||
}) | ||
); | ||
|
||
/** @var Twig $twig */ | ||
$twig = $twigProphecy->reveal(); | ||
|
||
/** @var LoggerInterface $logger */ | ||
$logger = $loggerProphecy->reveal(); | ||
|
||
$helloController = new HelloController($twig, $logger); | ||
|
||
$serverRequest = $this->createMock(ServerRequestInterface::class); | ||
$response = $this->createMock(ResponseInterface::class); | ||
|
||
$helloController($serverRequest, $response, ['name' => $argName]); | ||
} | ||
} |
Oops, something went wrong.