Skip to content

Commit

Permalink
Merge pull request #12 from ciaranmcnulty/refactor/verbose-exceptions
Browse files Browse the repository at this point in the history
Clarify exception types and move formatting logic out of Loader
  • Loading branch information
ciaranmcnulty authored May 6, 2017
2 parents e7e1103 + 70b0df2 commit 67201b3
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 26 deletions.
14 changes: 14 additions & 0 deletions src/AppFile/FileNotFound.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Cjm\Behat\Psr7Extension\AppFile;

use Cjm\Behat\Psr7Extension\Psr7LoaderException;

final class FileNotFound extends Psr7LoaderException
{
public function __construct(string $path)
{
parent::__construct('File not found at ' . $path);
}
}
14 changes: 14 additions & 0 deletions src/AppFile/InvalidFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
declare(strict_types=1);

namespace Cjm\Behat\Psr7Extension\AppFile;

use Cjm\Behat\Psr7Extension\Psr7LoaderException;

final class InvalidFile extends Psr7LoaderException
{
public function __construct(string $path)
{
parent::__construct('File at ' . $path . ' did not return an application');
}
}
24 changes: 11 additions & 13 deletions src/AppFile/Loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,29 @@ public function __construct(Psr7AppFactory ...$factories)

public function load(string $path): Psr7App
{
if (!file_exists($path)) {
throw new LoaderException('No file found at ' . $path);
}

if (!($type = include $path)) {
throw new LoaderException('File at ' . $path . ' did not return');
}
$type = $this->loadFromFile($path);

foreach ($this->factories as $factory) {
if ($app = $factory->createFrom($type)) {
return $app;
}
}

throw new LoaderException('Do not know how to create an app from ' . $this->type($type));
throw new UnknownType($type);
}

private function type($type) : string
private function loadFromFile(string $path)
{
$str = gettype($type);
if (!file_exists($path)) {
throw new FileNotFound($path);
}

$type = include $path;

if ($str == 'object') {
$str = 'object:' . get_class($type);
if (1 === $type) {
throw new InvalidFile($path);
}

return $str;
return $type;
}
}
8 changes: 0 additions & 8 deletions src/AppFile/LoaderException.php

This file was deleted.

26 changes: 26 additions & 0 deletions src/AppFile/UnknownType.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Cjm\Behat\Psr7Extension\AppFile;

use Cjm\Behat\Psr7Extension\Psr7LoaderException;

final class UnknownType extends Psr7LoaderException
{
public function __construct($type)
{
parent::__construct('Do not know how to create an app from ' . $this->printableType($type));
}

private function printableType($type) : string
{
$str = gettype($type);

if ($str == 'object') {
$str = 'object:' . get_class($type);
}

return $str;
}

}
8 changes: 8 additions & 0 deletions src/Psr7LoaderException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php
declare(strict_types=1);

namespace Cjm\Behat\Psr7Extension;

abstract class Psr7LoaderException extends \InvalidArgumentException
{
}
10 changes: 5 additions & 5 deletions tests/AppFile/LoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@ function setUp()

function testItComplainsIfFileDoesNotExist()
{
$this->expectException(LoaderException::class);
$this->expectException(FileNotFound::class);

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

function testItThrowsExceptionIfFileReturnsNull()
function testItThrowsExceptionIfFileDoesNotReturn()
{
$this->expectException(LoaderException::class);
$this->expectException(InvalidFile::class);

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

$this->loader->load(self::TMP_APP_FILE);
}
Expand All @@ -39,7 +39,7 @@ function testItThrowsExceptionIfNoFactoryCanMakeApp()
{
file_put_contents(self::TMP_APP_FILE, '<?php return "no idea";');

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

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

0 comments on commit 67201b3

Please sign in to comment.