Skip to content

Commit

Permalink
Quick resource from factory
Browse files Browse the repository at this point in the history
  • Loading branch information
bajb committed Feb 25, 2019
1 parent 94f1b27 commit 3131ba4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/Resources/ResourceFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,15 @@ public static function create(Resource $resource)
$response->setContent($resource->getContent());
return $response;
}

public static function fromFile($fullPath)
{
$resource = self::getExtensionResource(pathinfo($fullPath, PATHINFO_EXTENSION));
if($resource instanceof AbstractResource)
{
$resource->setFilePath($fullPath);
$resource->setContent(file_get_contents($fullPath));
}
return self::create($resource);
}
}
11 changes: 11 additions & 0 deletions tests/Resources/ResourceFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
namespace Packaged\Dispatch\Tests\Resources;

use Packaged\Dispatch\Resources\CssResource;
use Packaged\Dispatch\Resources\Image\IconResource;
use Packaged\Dispatch\Resources\ResourceFactory;
use Packaged\Dispatch\Resources\UnknownResource;
use Packaged\Dispatch\Resources\ZipResource;
use Packaged\Helpers\Path;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Response;

Expand Down Expand Up @@ -40,4 +42,13 @@ public function testCreate()
$this->assertInstanceOf(Response::class, $response);
$this->assertEquals($resource->getContentType(), $response->headers->get('Content-Type'));
}

public function testFavicon()
{
$path = Path::system(dirname(__DIR__), '_root', 'public', 'favicon.ico');
$full = ResourceFactory::create(IconResource::fromFilePath($path));
$fromFile = ResourceFactory::fromFile($path);
$this->assertEquals($full, $fromFile);
$this->assertEquals('"40ed4027e1e5f15c19a2fb287bcc3724"', $fromFile->getEtag());
}
}
Binary file added tests/_root/public/favicon.ico
Binary file not shown.

0 comments on commit 3131ba4

Please sign in to comment.