Skip to content

Commit

Permalink
Merge pull request #15 from adriansuter/patch-container-controllers
Browse files Browse the repository at this point in the history
Patch container controllers
  • Loading branch information
adriansuter authored Aug 12, 2019
2 parents e541c53 + 14e070b commit 6dc394e
Show file tree
Hide file tree
Showing 13 changed files with 279 additions and 44 deletions.
1 change: 1 addition & 0 deletions application/App/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public static function create(string $rootPath): ContainerInterface
Preferences::class => new Preferences($rootPath),
]);
$containerBuilder->addDefinitions($rootPath . '/application/config/container-definitions.php');
$containerBuilder->addDefinitions($rootPath . '/application/config/container-controllers.php');

// Note: In production, you should enable container-compilation.
// $containerBuilder->enableCompilation($rootPath . '/cache');
Expand Down
40 changes: 5 additions & 35 deletions application/App/Controllers/AbstractController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,11 @@

namespace App\Controllers;

use Psr\Container\ContainerInterface;
use Psr\Http\Message\ResponseInterface as Response;
use Slim\Views\Twig;

/**
* This abstract class defines methods and properties used by all controllers.
*
* @package App\Controllers
*/
abstract class AbstractController
{
/**
* @var ContainerInterface
*/
protected $container;

/**
* AbstractController constructor.
*
* @param ContainerInterface $container
*/
public function __construct(ContainerInterface $container)
{
$this->container = $container;
}

/**
* 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
{
/** @var Twig $view */
$view = $this->container->get('view');

return $view->render($response, $template, $renderData);
}
}
38 changes: 38 additions & 0 deletions application/App/Controllers/AbstractTwigController.php
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);
}
}
25 changes: 21 additions & 4 deletions application/App/Controllers/HelloController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,28 @@
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Psr\Log\LoggerInterface;
use Slim\Views\Twig;

class HelloController extends AbstractController
class HelloController extends AbstractTwigController
{
/**
* @var LoggerInterface
*/
private $logger;

/**
* HelloController constructor.
*
* @param Twig $twig
* @param LoggerInterface $logger
*/
public function __construct(Twig $twig, LoggerInterface $logger)
{
parent::__construct($twig);

$this->logger = $logger;
}

/**
* @param Request $request
* @param Response $response
Expand All @@ -19,9 +38,7 @@ class HelloController extends AbstractController
*/
public function __invoke(Request $request, Response $response, array $args = []): Response
{
// Log a message.
$logger = $this->container->get(LoggerInterface::class);
$logger->debug(sprintf('Hello "%s"', $args ['name']));
$this->logger->debug(sprintf('Hello "%s"', $args ['name']));

return $this->render($response, 'hello.twig', [
'pageTitle' => 'Hello ' . $args['name'],
Expand Down
23 changes: 22 additions & 1 deletion application/App/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,31 @@

namespace App\Controllers;

use App\Preferences;
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Views\Twig;

class HomeController extends AbstractController
class HomeController extends AbstractTwigController
{
/**
* @var Preferences
*/
private $preferences;

/**
* HomeController constructor.
*
* @param Twig $twig
* @param Preferences $preferences
*/
public function __construct(Twig $twig, Preferences $preferences)
{
parent::__construct($twig);

$this->preferences = $preferences;
}

/**
* @param Request $request
* @param Response $response
Expand All @@ -20,6 +40,7 @@ public function __invoke(Request $request, Response $response, array $args = [])
{
return $this->render($response, 'home.twig', [
'pageTitle' => 'Home',
'rootPath' => $this->preferences->getRootPath(),
]);
}
}
5 changes: 5 additions & 0 deletions application/App/Preferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

namespace App;

/**
* This class contains the preferences for the application.
*
* @package App
*/
class Preferences
{
/**
Expand Down
22 changes: 22 additions & 0 deletions application/config/container-controllers.php
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));
}
];
2 changes: 1 addition & 1 deletion application/config/container-definitions.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
use Psr\Log\LoggerInterface;

return [
LoggerInterface::class => function (ContainerInterface $container) {
LoggerInterface::class => function (ContainerInterface $container): LoggerInterface {
// Get the preferences from the container.
$preferences = $container->get(Preferences::class);

Expand Down
8 changes: 8 additions & 0 deletions application/templates/home.twig
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,12 @@
</table>
</div>

<h2 class="mt-4">
Filesystem
</h2>

<p>
The root path for this project is <code>{{ rootPath }}</code>.
</p>

{% endblock %}
3 changes: 0 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>./application/App/</directory>
<exclude>
<directory>./application/App/Controllers</directory>
</exclude>
</whitelist>
</filter>
<logging>
Expand Down
26 changes: 26 additions & 0 deletions tests/Controllers/ExceptionDemoControllerTest.php
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, []);
}
}
74 changes: 74 additions & 0 deletions tests/Controllers/HelloControllerTest.php
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]);
}
}
Loading

0 comments on commit 6dc394e

Please sign in to comment.