Skip to content

Commit

Permalink
added urlgenerator tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Fraser committed Jul 8, 2016
1 parent 22fd539 commit 6bfe289
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 19 deletions.
1 change: 0 additions & 1 deletion phpunit.xml
Expand Up @@ -13,7 +13,6 @@
<testsuites>
<testsuite name="Package Test Suite">
<directory suffix=".php">./tests/integration/</directory>
<directory suffix=".php">./tests/unit/</directory>
</testsuite>
</testsuites>
<filter>
Expand Down
5 changes: 0 additions & 5 deletions src/Exceptions/MediaUrlException.php
Expand Up @@ -23,9 +23,4 @@ public static function mediaNotPubliclyAccessible($path, $public_path)
{
return new static("Media file `{$path}` is not part of the public path `{$public_path}`");
}

public static function cannotGetAbsolutePath($disk)
{
return new static("Cannot get absolute path. Disk `{$disk}` is not on the local filesystem");
}
}
12 changes: 2 additions & 10 deletions src/UrlGenerators/LocalUrlGenerator.php
Expand Up @@ -56,6 +56,7 @@ public function getPublicPath()

/**
* {@inheritDoc}
* @throws MediaUrlException If media's disk is not publicly accessible
*/
public function getUrl()
{
Expand All @@ -67,7 +68,7 @@ public function getUrl()
*/
public function getAbsolutePath()
{
return $this->diskRoot() . DIRECTORY_SEPARATOR . $this->media->getDiskPath();
return $this->getDiskConfig('root') . DIRECTORY_SEPARATOR . $this->media->getDiskPath();
}


Expand All @@ -83,13 +84,4 @@ protected function cleanDirectorySeparators($path)
}
return $path;
}

/**
* Get the absolute path to the root of the storage disk
* @return string
*/
private function diskRoot()
{
return $this->getDiskConfig('root');
}
}
2 changes: 1 addition & 1 deletion src/UrlGenerators/S3UrlGenerator.php
Expand Up @@ -30,7 +30,7 @@ public function __construct(Config $config, FilesystemManager $filesystem)
*/
public function getAbsolutePath()
{
throw MediaUrlException::cannotGetAbsolutePath($this->media->disk);
return $this->getUrl();
}

/**
Expand Down
3 changes: 3 additions & 0 deletions src/UrlGenerators/UrlGenerator.php
Expand Up @@ -19,6 +19,9 @@ public function setMedia(Media $media);

/**
* Retrieve the absolute path to the file
*
* For local files this should return a path
* For remote files this should return a url
* @return string
*/
public function getAbsolutePath();
Expand Down
1 change: 0 additions & 1 deletion tests/integration/MediableTest.php
Expand Up @@ -32,7 +32,6 @@ public function test_it_can_attach_and_retrieve_media_by_a_tag()
$this->assertEquals([2], $result->pluck('id')->toArray());
}


public function test_it_can_attach_one_media_to_multiple_tags()
{
$mediable = factory(SampleMediable::class)->create();
Expand Down
40 changes: 40 additions & 0 deletions tests/integration/UrlGenerators/LocalUrlGeneratorTest.php
@@ -0,0 +1,40 @@
<?php

use Frasmage\Mediable\Exceptions\MediaUrlException;
use Frasmage\Mediable\UrlGenerators\LocalUrlGenerator;
use Frasmage\Mediable\Media;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Routing\UrlGenerator as Url;

class LocalUrlGeneratorTest extends TestCase
{
public function test_it_generates_absolute_path(){
$generator = $this->setupGenerator();
$this->assertEquals(public_path('uploads/foo/bar.jpg'), $generator->getAbsolutePath());
}

public function test_it_generates_url(){
$generator = $this->setupGenerator();
$this->assertEquals('http://localhost/uploads/foo/bar.jpg', $generator->getUrl());
}

public function test_it_throws_exception_for_non_public_disk(){
$generator = $this->setupGenerator('tmp');
$this->expectException(MediaUrlException::class);
$generator->getPublicPath();
}

protected function setupGenerator($disk = 'uploads'){
$media = factory(Media::class)->make([
'disk' => $disk,
'directory' => 'foo',
'filename' => 'bar',
'extension' => 'jpg'
]);
$this->seedFileForMedia($media);
$generator = new LocalUrlGenerator(config(), url());
$generator->setMedia($media);
return $generator;
}

}
33 changes: 33 additions & 0 deletions tests/integration/UrlGenerators/S3UrlGeneratorTest.php
@@ -0,0 +1,33 @@
<?php

use Frasmage\Mediable\Exceptions\MediaUrlException;
use Frasmage\Mediable\UrlGenerators\S3UrlGenerator;
use Frasmage\Mediable\Media;
use Illuminate\Contracts\Config\Repository as Config;
use Illuminate\Routing\UrlGenerator as Url;

class S3UrlGeneratorTest extends TestCase
{
public function test_it_generates_absolute_path(){
$generator = $this->setupGenerator();
$this->assertEquals('https://s3.amazonaws.com/' . env('S3_BUCKET') . '/foo/bar.jpg', $generator->getAbsolutePath());
}

public function test_it_generates_url(){
$generator = $this->setupGenerator();
$this->assertEquals('https://s3.amazonaws.com/' . env('S3_BUCKET') . '/foo/bar.jpg', $generator->getUrl());
}

protected function setupGenerator(){
$media = factory(Media::class)->make([
'disk' => 's3',
'directory' => 'foo',
'filename' => 'bar',
'extension' => 'jpg'
]);
$generator = new S3UrlGenerator(config(), app(Illuminate\Filesystem\FilesystemManager::class));
$generator->setMedia($media);
return $generator;
}

}
Expand Up @@ -5,7 +5,7 @@
use Frasmage\Mediable\UrlGenerators\UrlGenerator;
use Frasmage\Mediable\Media;

class UrlGeneratorTest extends TestCase
class UrlGeneratorFactoryTest extends TestCase
{

public function test_it_sets_generator_for_driver()
Expand Down

0 comments on commit 6bfe289

Please sign in to comment.