-
Notifications
You must be signed in to change notification settings - Fork 101
/
UrlGeneratorFactoryTest.php
63 lines (55 loc) · 2.42 KB
/
UrlGeneratorFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
<?php
namespace Plank\Mediable\Tests\Integration\UrlGenerators;
use League\Flysystem\PathPrefixing\PathPrefixedAdapter;
use Plank\Mediable\Exceptions\MediaUrlException;
use Plank\Mediable\Media;
use Plank\Mediable\Tests\TestCase;
use Plank\Mediable\UrlGenerators\LocalUrlGenerator;
use Plank\Mediable\UrlGenerators\UrlGeneratorFactory;
use Plank\Mediable\UrlGenerators\UrlGeneratorInterface;
use stdClass;
class UrlGeneratorFactoryTest extends TestCase
{
public function test_it_sets_generator_for_driver()
{
$factory = new UrlGeneratorFactory;
$generator = $this->createMock(UrlGeneratorInterface::class);
$class = get_class($generator);
$media = factory(Media::class)->make(['disk' => 'uploads']);
$factory->setGeneratorForFilesystemDriver($class, 'local');
$result = $factory->create($media);
$this->assertInstanceOf($class, $result);
}
public function test_it_throws_exception_for_invalid_generator()
{
$factory = new UrlGeneratorFactory;
$class = get_class($this->createMock(stdClass::class));
$this->expectException(MediaUrlException::class);
$factory->setGeneratorForFilesystemDriver($class, 'foo');
}
public function test_it_throws_exception_if_cant_map_to_driver()
{
$factory = new UrlGeneratorFactory;
$media = factory(Media::class)->make();
$this->expectException(MediaUrlException::class);
$factory->create($media);
}
public function test_it_follows_scoped_prefix()
{
if (version_compare($this->app->version(), '9.30.0', '<')) {
$this->markTestSkipped("scoped disk prefixes are only supported in laravel 9.30.0+");
}
// TODO: league/flysystem-path-prefixing requires PHP 8, we still support 7.4
// so can't include it in composer.json yet. To be fixed in next major version
if (!class_exists(PathPrefixedAdapter::class)) {
$this->markTestSkipped("path prefixing not installed");
}
$factory = new UrlGeneratorFactory;
$factory->setGeneratorForFilesystemDriver(LocalUrlGenerator::class, 'local');
/** @var Media $media */
$media = factory(Media::class)->make(['disk' => 'scoped']);
$result = $factory->create($media);
$this->assertInstanceOf(LocalUrlGenerator::class, $result);
$this->assertEquals('/storage/scoped/' . $media->getDiskPath(), $result->getUrl());
}
}