Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/autoload/global.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@
'view_manager' => [
'base_path' => '/',
],
];
];
4 changes: 1 addition & 3 deletions config/autoload/mezzio-tooling-factories.global.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,4 @@

declare(strict_types=1);

return [

];
return [];
2 changes: 1 addition & 1 deletion src/App/src/ConfigProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function getDependencies(): array
{
return [
'invokables' => [
Handler\PingHandler::class => Handler\PingHandler::class,
Handler\PingHandler::class => Handler\PingHandler::class,
Container\HttpClientFactoryFactory::class => Container\HttpClientFactoryFactory::class,
],
'factories' => [
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Container/AboutHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class AboutHandlerFactory
{
public function __invoke(ContainerInterface $container) : AboutHandler
public function __invoke(ContainerInterface $container): AboutHandler
{
return new AboutHandler($container->get(TemplateRendererInterface::class));
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Container/CommunityHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class CommunityHandlerFactory
{
public function __invoke(ContainerInterface $container) : CommunityHandler
public function __invoke(ContainerInterface $container): CommunityHandler
{
return new CommunityHandler($container->get(TemplateRendererInterface::class));
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Container/DocsHandlerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DocsHandlerFactory
{
public function __invoke(ContainerInterface $container) : DocsHandler
public function __invoke(ContainerInterface $container): DocsHandler
{
return new DocsHandler($container->get(TemplateRendererInterface::class));
}
Expand Down
2 changes: 1 addition & 1 deletion src/App/src/Container/GithubRepoDataMiddlewareFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class GithubRepoDataMiddlewareFactory
{
public function __invoke(ContainerInterface $container) : GithubRepoDataMiddleware
public function __invoke(ContainerInterface $container): GithubRepoDataMiddleware
{
return new GithubRepoDataMiddleware(
$container->get(HttpClientFactoryFactory::class),
Expand Down
6 changes: 4 additions & 2 deletions src/App/src/Container/HttpClientFactoryFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace App\Container;

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Handler\StreamHandler;
use GuzzleHttp\HandlerStack;

use function array_merge;

final class HttpClientFactoryFactory
{
Expand All @@ -15,7 +17,7 @@ public function __invoke(): callable
return static function (?array $config = null, bool $stream = true): Client {
if ($stream) {
$handler = HandlerStack::create(new StreamHandler());
$config = $config ? array_merge($config, ['handler' => $handler]) : ['handler' => $handler];
$config = $config ? array_merge($config, ['handler' => $handler]) : ['handler' => $handler];
}
return empty($config) ? new Client() : new Client($config);
};
Expand Down
1 change: 0 additions & 1 deletion src/App/src/GithubRequestSpecProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public function __invoke(): array
{
return [
Endpoint::Repo->value => $this->getRepoRequestConfig(),

];
}

Expand Down
10 changes: 4 additions & 6 deletions src/App/src/Handler/AboutHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

namespace App\Handler;

use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;

class AboutHandler implements RequestHandlerInterface
{
/**
* @var TemplateRendererInterface
*/
/** @var TemplateRendererInterface */
private $renderer;

public function __construct(TemplateRendererInterface $renderer)
{
$this->renderer = $renderer;
}

public function handle(ServerRequestInterface $request) : ResponseInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
// Do some work...
// Render and return a response:
Expand Down
10 changes: 4 additions & 6 deletions src/App/src/Handler/CommunityHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

namespace App\Handler;

use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;

class CommunityHandler implements RequestHandlerInterface
{
/**
* @var TemplateRendererInterface
*/
/** @var TemplateRendererInterface */
private $renderer;

public function __construct(TemplateRendererInterface $renderer)
{
$this->renderer = $renderer;
}

public function handle(ServerRequestInterface $request) : ResponseInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
// Do some work...
// Render and return a response:
Expand Down
10 changes: 4 additions & 6 deletions src/App/src/Handler/ProjectsHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,23 @@

namespace App\Handler;

use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Laminas\Diactoros\Response\HtmlResponse;
use Mezzio\Template\TemplateRendererInterface;

class ProjectsHandler implements RequestHandlerInterface
{
/**
* @var TemplateRendererInterface $renderer
*/
/** @var TemplateRendererInterface $renderer */
private $renderer;

public function __construct(TemplateRendererInterface $renderer)
{
$this->renderer = $renderer;
}

public function handle(ServerRequestInterface $request) : ResponseInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
$githubRepoData = $request->getAttribute('github-repo-data');
return new HtmlResponse($this->renderer->render(
Expand Down
11 changes: 10 additions & 1 deletion src/App/src/Iterator/GithubRepoResponseFilterIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FilterIterator;
use Iterator;

use function in_array;
use function str_contains;

final class GithubRepoResponseFilterIterator extends FilterIterator
Expand All @@ -23,6 +24,14 @@ public function accept(): bool
{
$provider = new GithubRequestSpecProvider();
$current = $this->getInnerIterator()->current();
return ! str_contains($current['name'], $this->nameNeedle) && ! in_array($current['name'], $provider->getExcludedRepos(), true);
return ! str_contains(
$current['name'],
$this->nameNeedle
)
&& ! in_array(
$current['name'],
$provider->getExcludedRepos(),
true
);
}
}
18 changes: 12 additions & 6 deletions src/App/src/Middleware/GithubRepoDataMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,20 @@
use App\Endpoint;
use App\Iterator\GithubRepoResponseFilterIterator;
use ArrayIterator;
use GuzzleHttp\Client;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;

use function array_merge;
use function json_decode;

use const JSON_THROW_ON_ERROR;

class GithubRepoDataMiddleware implements MiddlewareInterface
{
/** @var callable $clientFactory */
private $clientFactory;

public function __construct(
Expand All @@ -25,17 +30,18 @@ public function __construct(
$this->clientFactory = $clientFactory;
}

public function process(ServerRequestInterface $request, RequestHandlerInterface $handler) : ResponseInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$clientConfig = array_merge($this->config[Endpoint::Repo->value], ['headers' => $this->config['headers']]);
$client = (($this->clientFactory)())($clientConfig);
$response = $client->get(Endpoint::Repo->value);
$data = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
$filter = new GithubRepoResponseFilterIterator(new ArrayIterator($data));
/** @var Client $client */
$client = (($this->clientFactory)())($clientConfig);
$response = $client->get(Endpoint::Repo->value);
$data = json_decode((string) $response->getBody(), true, 512, JSON_THROW_ON_ERROR);
$filter = new GithubRepoResponseFilterIterator(new ArrayIterator($data));
foreach ($filter as $item) {
$filteredData[] = $item;
}
$request = $request->withAttribute('github-repo-data', $filteredData);
$request = $request->withAttribute('github-repo-data', $filteredData);
return $handler->handle($request);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

namespace AppTest\Handler;

use App\Handler\HomePageHandler;
use App\Handler\HomePageHandlerFactory;
use App\Container\HomeHandlerFactory;
use App\Handler\HomeHandler;
use AppTest\InMemoryContainer;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use PHPUnit\Framework\TestCase;

final class HomePageHandlerFactoryTest extends TestCase
final class HomeHandlerFactoryTest extends TestCase
{
public function testFactoryWithoutTemplate(): void
{
$container = new InMemoryContainer();
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));

$factory = new HomePageHandlerFactory();
$factory = new HomeHandlerFactory();
$homePage = $factory($container);

self::assertInstanceOf(HomePageHandler::class, $homePage);
self::assertInstanceOf(HomeHandler::class, $homePage);
}

public function testFactoryWithTemplate(): void
Expand All @@ -30,9 +30,9 @@ public function testFactoryWithTemplate(): void
$container->setService(RouterInterface::class, $this->createMock(RouterInterface::class));
$container->setService(TemplateRendererInterface::class, $this->createMock(TemplateRendererInterface::class));

$factory = new HomePageHandlerFactory();
$factory = new HomeHandlerFactory();
$homePage = $factory($container);

self::assertInstanceOf(HomePageHandler::class, $homePage);
self::assertInstanceOf(HomeHandler::class, $homePage);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,56 @@

namespace AppTest\Handler;

use App\Handler\HomePageHandler;
use App\Handler\HomeHandler;
use Laminas\Diactoros\Response\HtmlResponse;
use Laminas\Diactoros\Response\JsonResponse;
use Mezzio\Router\RouterInterface;
use Mezzio\Template\TemplateRendererInterface;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Message\ServerRequestInterface;

final class HomePageHandlerTest extends TestCase
final class HomeHandlerTest extends TestCase
{
/** @var ContainerInterface&MockObject */
protected $container;
/** @var ServerRequestInterface&MockObject */
protected $request;

/** @var RouterInterface&MockObject */
protected $router;
/** @var TemplateRendererInterface&MockObject */
protected $renderer;

protected function setUp(): void
{
$this->container = $this->createMock(ContainerInterface::class);
$this->router = $this->createMock(RouterInterface::class);
$this->request = $this->createMock(ServerRequestInterface::class);
$this->renderer = $this->createMock(TemplateRendererInterface::class);
}

public function testReturnsJsonResponseWhenNoTemplateRendererProvided(): void
{
$homePage = new HomePageHandler(
$this->container::class,
$this->router,
$homePage = new HomeHandler(
null
);
$response = $homePage->handle(
$this->createMock(ServerRequestInterface::class)
$this->request
);

self::assertInstanceOf(JsonResponse::class, $response);
}

public function testReturnsHtmlResponseWhenTemplateRendererProvided(): void
{
/** @var TemplateRendererInterface&MockObject $renderer */
$renderer = $this->createMock(TemplateRendererInterface::class);
$renderer
->expects($this->once())
->method('render')
->with('app::home-page', $this->isType('array'))
->with('app::home', $this->isType('array'))
->willReturn('');

$homePage = new HomePageHandler(
$this->container::class,
$this->router,
$homePage = new HomeHandler(
$renderer
);

$response = $homePage->handle(
$this->createMock(ServerRequestInterface::class)
$this->request
);

self::assertInstanceOf(HtmlResponse::class, $response);
Expand Down