Skip to content

Commit

Permalink
Merge pull request #13 from ciaranmcnulty/refactor/loading-extension-…
Browse files Browse the repository at this point in the history
…point

Localise responsibility for app reloading into one place
  • Loading branch information
ciaranmcnulty authored May 6, 2017
2 parents 67201b3 + ca121aa commit 1978028
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 20 deletions.
8 changes: 5 additions & 3 deletions src/AppFile/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
*/
class Loader implements Psr7AppLoader
{
private $path;
private $factories;

public function __construct(Psr7AppFactory ...$factories)
public function __construct(string $path, Psr7AppFactory ...$factories)
{
$this->factories = $factories;
$this->path = $path;
}

public function load(string $path): Psr7App
public function load(): Psr7App
{
$type = $this->loadFromFile($path);
$type = $this->loadFromFile($this->path);

foreach ($this->factories as $factory) {
if ($app = $factory->createFrom($type)) {
Expand Down
19 changes: 19 additions & 0 deletions src/CachingLoader.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Cjm\Behat\Psr7Extension;

class CachingLoader implements Psr7AppLoader
{
private $loader;
private $app;

public function __construct(Psr7AppLoader $loader)
{
$this->loader = $loader;
}

public function load(): Psr7App
{
return $this->app ?: ($this->app = $this->loader->load());
}
}
2 changes: 1 addition & 1 deletion src/Psr7AppLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
*/
interface Psr7AppLoader
{
public function load(string $config) : Psr7App;
public function load() : Psr7App;
}
17 changes: 9 additions & 8 deletions src/ServiceContainer/services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ services:
cjm.behat.psr7.kernel:
class: Cjm\Behat\Psr7Extension\SymfonyKernel
arguments:
- "@cjm.behat.psr7.app"
- "@cjm.behat.psr7.loader"
- "@cjm.behat.psr7.http_message_bridge.converter"

cjm.behat.psr7.app:
class: Cjm\Behat\Psr7Extension\Psr7App
factory: "cjm.behat.psr7.loader:load"
arguments:
- "%cjm.behat.psr7.app%"

cjm.behat.psr7.loader:
class: Cjm\Behat\Psr7Extension\AppFile\Loader
arguments: [] # receives services tagged as cjm.behat.psr7.factory
arguments:
- "%cjm.behat.psr7.app%" # also receives services tagged as cjm.behat.psr7.factory

cjm.behat.psr7.caching_loader:
class: Cjm\Behat\Psr7Extension\CachingLoader
decorates: "cjm.behat.psr7.loader"
arguments:
- "@cjm.behat.psr7.caching_loader.inner"

cjm.behat.psr7.factory.callback:
class: Cjm\Behat\Psr7Extension\Callback\AppFactory
Expand Down
8 changes: 4 additions & 4 deletions src/SymfonyKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/
final class SymfonyKernel implements HttpKernelInterface
{
private $app;
private $converter;
private $loader;

public function __construct(Psr7App $app, SymfonyToPsr7Converter $converter)
public function __construct(Psr7AppLoader $loader, SymfonyToPsr7Converter $converter)
{
$this->app = $app;
$this->converter = $converter;
$this->loader = $loader;
}

/**
Expand All @@ -29,7 +29,7 @@ public function __construct(Psr7App $app, SymfonyToPsr7Converter $converter)
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
return $this->converter->convertResponse(
$this->app->handle(
$this->loader->load()->handle(
$this->converter->convertRequest($request)
)
);
Expand Down
10 changes: 6 additions & 4 deletions tests/AppFile/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class LoaderTest extends TestCase
function setUp()
{
$this->loader = new Loader(
self::TMP_APP_FILE,
$this->createMock(Psr7AppFactory::class)
);
}
Expand All @@ -23,7 +24,7 @@ function testItComplainsIfFileDoesNotExist()
{
$this->expectException(FileNotFound::class);

$this->loader->load(self::TMP_APP_FILE);
$this->loader->load();
}

function testItThrowsExceptionIfFileDoesNotReturn()
Expand All @@ -32,7 +33,7 @@ function testItThrowsExceptionIfFileDoesNotReturn()

file_put_contents(self::TMP_APP_FILE, '<?php ');

$this->loader->load(self::TMP_APP_FILE);
$this->loader->load();
}

function testItThrowsExceptionIfNoFactoryCanMakeApp()
Expand All @@ -41,12 +42,13 @@ function testItThrowsExceptionIfNoFactoryCanMakeApp()

$this->expectException(UnknownType::class);

$this->loader->load(self::TMP_APP_FILE);
$this->loader->load();
}

function testItCreatesAnAppIfAFactoryCan()
{
$this->loader = new Loader(
self::TMP_APP_FILE,
$factory1 = $this->createMock(Psr7AppFactory::class),
$factory2 = $this->createMock(Psr7AppFactory::class)
);
Expand All @@ -56,7 +58,7 @@ function testItCreatesAnAppIfAFactoryCan()

$factory2->method('createFrom')->willReturn($app);

$this->assertSame($app, $this->loader->load(self::TMP_APP_FILE));
$this->assertSame($app, $this->loader->load());
}

function tearDown()
Expand Down
41 changes: 41 additions & 0 deletions tests/CachingLoaderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Cjm\Behat\Psr7Extension;

use PHPUnit\Framework\TestCase;

class CachingLoaderTest extends TestCase
{
private $innerLoader;
private $innerApp;
private $loader;

public function setUp()
{
$this->innerLoader = $this->createMock(Psr7AppLoader::class);
$this->innerApp = $this->createMock(Psr7App::class);

$this->innerLoader->method('load')->willReturn($this->innerApp);

$this->loader = new CachingLoader($this->innerLoader);
}

public function testItLoadsTheApp()
{
$app = $this->loader->load();

$this->assertSame($this->innerApp, $app);
}

public function testItOnlyLoadsTheAppOnce()
{
$this->innerLoader
->expects($this->once())
->method('load');

$this->loader->load();
$app = $this->loader->load();

$this->assertSame($this->innerApp, $app);
}
}

0 comments on commit 1978028

Please sign in to comment.