-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssetBundleTest.php
76 lines (63 loc) · 2.86 KB
/
AssetBundleTest.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
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
use PHPUnit\Framework\TestCase;
use Piko\Tests\mock\TestAssets;
use Piko\Tests\mock\WrongAssets;
use Piko\Application;
class AssetBundleTest extends TestCase
{
const PUBLIC_DIR = __DIR__ . '/www';
/**
* @var Application
*/
protected $application;
public static function setUpBeforeClass(): void
{
mkdir(static::PUBLIC_DIR);
Piko::setAlias('@web', '');
}
public static function tearDownAfterClass(): void
{
exec('rm -rf ' . static::PUBLIC_DIR);
}
public function setUp(): void
{
$this->application = new Application([
'baseUrl' => '',
'components' => [
'Piko\View' => []
]
]);
Piko::setAlias('@webroot', static::PUBLIC_DIR);
}
public function testBundle()
{
$view = $this->application->getComponent('Piko\View');
$bundle = TestAssets::register($view);
$this->assertEquals(1, $bundle->registrationCount);
// Check if bundle is registered once
$bundle = TestAssets::register($view);
$this->assertEquals(1, $bundle->registrationCount);
$output = $view->render(__DIR__ . '/mock/layout.php', ['content' => '']);
$this->assertDirectoryExists(self::PUBLIC_DIR . '/assets/test-parent');
$this->assertDirectoryExists(self::PUBLIC_DIR . '/assets/test');
$this->assertDirectoryExists(self::PUBLIC_DIR . '/assets/test-parent/css');
$this->assertDirectoryExists(self::PUBLIC_DIR . '/assets/test/css');
$this->assertFileExists(self::PUBLIC_DIR . '/assets/test-parent/css/parent.css');
$this->assertFileExists(self::PUBLIC_DIR . '/assets/test/css/test.css');
$this->assertFileExists(self::PUBLIC_DIR . '/assets/test-parent/parent.js');
$this->assertFileExists(self::PUBLIC_DIR . '/assets/test/test.js');
$this->assertMatchesRegularExpression('`<link href="/assets/test-parent/css/parent.css" rel="stylesheet">`', $output);
$this->assertMatchesRegularExpression('`<link href="/assets/test/css/test.css" rel="stylesheet">`', $output);
$this->assertMatchesRegularExpression('`<script src="/assets/test-parent/parent.js"></script>`', $output);
$this->assertMatchesRegularExpression('`<script src="/assets/test/test.js"></script>`', $output);
$this->assertMatchesRegularExpression('`<link href="http://domain.com/css/test.css" rel="stylesheet">`', $output);
$this->assertMatchesRegularExpression('`<script src="http://domain.com/js/test.js"></script>`', $output);
}
public function testBundleWithWrongPath()
{
$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Src: ' . (new WrongAssets())->sourcePath . ' does not exists.');
$view = $this->application->getComponent('Piko\View');
WrongAssets::register($view);
}
}