1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mouf\Html\Utils\WebLibraryManager; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ServiceProviderInterface; |
6
|
|
|
use PHPUnit\Framework\TestCase; |
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Simplex\Container; |
9
|
|
|
use TheCodingMachine\Discovery\Discovery; |
10
|
|
|
use TheCodingMachine\Funky\Annotations\Factory; |
11
|
|
|
use TheCodingMachine\Funky\Annotations\Tag; |
12
|
|
|
use TheCodingMachine\Funky\ServiceProvider; |
13
|
|
|
|
14
|
|
|
class WebLibraryManagerTest extends TestCase |
15
|
|
|
{ |
16
|
|
|
public function testToHtml() |
17
|
|
|
{ |
18
|
|
|
$webLibraryServiceProvider = new class extends ServiceProvider { |
19
|
|
|
/** |
20
|
|
|
* @Factory(name="myWebLibrary", tags={@Tag(name="webLibraries")}) |
21
|
|
|
*/ |
22
|
|
|
public static function createWebLibrary(ContainerInterface $container): WebLibrary |
23
|
|
|
{ |
24
|
|
|
return new WebLibrary(['foo/bar.js', 'http://exemple.com/foo.js'], |
25
|
|
|
['foo/bar.css', 'http://exemple.com/foo.css'], $container->get('ROOT_URL')); |
26
|
|
|
} |
27
|
|
|
}; |
28
|
|
|
|
29
|
|
|
$serviceProvidersNames = Discovery::getInstance()->get(ServiceProviderInterface::class); |
30
|
|
|
$serviceProviders = \array_map(function(string $className) { |
31
|
|
|
return new $className(); |
32
|
|
|
}, $serviceProvidersNames); |
33
|
|
|
$serviceProviders[] = $webLibraryServiceProvider; |
34
|
|
|
|
35
|
|
|
$container = new Container($serviceProviders); |
36
|
|
|
$container->set('ROOT_URL', '/foo/bar/'); |
37
|
|
|
|
38
|
|
|
/* @var WebLibraryManager $webLibraryManager */ |
39
|
|
|
$webLibraryManager = $container->get(WebLibraryManager::class); |
40
|
|
|
\ob_start(); |
41
|
|
|
$webLibraryManager->toHtml(); |
42
|
|
|
$content = \ob_get_clean(); |
43
|
|
|
|
44
|
|
|
$this->assertSame('<link href=\'/foo/bar/foo/bar.css\' rel=\'stylesheet\' type=\'text/css\' /> |
45
|
|
|
<link href=\'http://exemple.com/foo.css\' rel=\'stylesheet\' type=\'text/css\' /> |
46
|
|
|
<script type="text/javascript"> |
47
|
|
|
window.rootUrl = "\/foo\/bar\/"</script> |
48
|
|
|
<script type="text/javascript" src="/foo/bar/foo/bar.js" ></script> |
49
|
|
|
<script type="text/javascript" src="http://exemple.com/foo.js" ></script> |
50
|
|
|
', $content); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|