framework/tests/integration/extenders/FrontendPreloadTest.php
David Wheatley 88724bb4cb
performance(frontend): Preload FontAwesome, JS and CSS (#3057)
* Add preloads support to Document class

* Add frontend extender for asset preloading

* Provide default preloads for FontAwesome

* Add tests for preload extender and default preloads

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Fix typo

* Fix two more typos 🙃

* Preload core JS and CSS

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Reorder preloads

* Remove singular preloads method

* Use filesystem disk driver for getting FA font paths

* Update test to use full URL

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Address review comment

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Fix typo

* Apply fixes from StyleCI

[ci skip] [skip ci]

* Correct callback wrapping

* Update src/Extend/Frontend.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update src/Extend/Frontend.php

Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>

* Update src/Extend/Frontend.php

* Fix preload extender logic

* Convert base FontAwesome preloads into a Singleton

* Apply fixes from StyleCI

[ci skip] [skip ci]

Co-authored-by: luceos <luceos@users.noreply.github.com>
Co-authored-by: Sami Mazouz <sychocouldy@gmail.com>
Co-authored-by: Alexander Skvortsov <38059171+askvortsov1@users.noreply.github.com>
Co-authored-by: Alexander Skvortsov <sasha.skvortsov109@gmail.com>
2021-09-20 23:12:09 +01:00

94 lines
2.4 KiB
PHP

<?php
/*
* This file is part of Flarum.
*
* For detailed copyright and license information, please view the
* LICENSE file that was distributed with this source code.
*/
namespace Flarum\Tests\integration\extenders;
use Flarum\Extend;
use Flarum\Testing\integration\TestCase;
class FrontendPreloadTest extends TestCase
{
private $customPreloadUrls = ['/my-preload', '/my-preload2'];
/**
* @test
*/
public function default_preloads_are_present()
{
$response = $this->send(
$this->request('GET', '/')
);
$filesystem = $this->app()->getContainer()->make('filesystem')->disk('flarum-assets');
$urls = [
$filesystem->url('fonts/fa-solid-900.woff2'),
$filesystem->url('fonts/fa-regular-400.woff2'),
];
$body = $response->getBody()->getContents();
foreach ($urls as $url) {
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\" as=\"font\" type=\"font/woff2\" crossorigin=\"\">", $body);
}
}
/**
* @test
*/
public function preloads_can_be_added()
{
$urls = $this->customPreloadUrls;
$this->extend(
(new Extend\Frontend('forum'))
->preloads(
array_map(function ($url) {
return ['href' => $url];
}, $urls)
)
);
$response = $this->send(
$this->request('GET', '/')
);
$body = $response->getBody()->getContents();
foreach ($urls as $url) {
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\">", $body);
}
}
/**
* @test
*/
public function preloads_can_be_added_via_callable()
{
$urls = $this->customPreloadUrls;
$this->extend(
(new Extend\Frontend('forum'))
->preloads(function () use ($urls) {
return array_map(function ($url) {
return ['href' => $url];
}, $urls);
})
);
$response = $this->send(
$this->request('GET', '/')
);
$body = $response->getBody()->getContents();
foreach ($urls as $url) {
$this->assertStringContainsString("<link rel=\"preload\" href=\"$url\">", $body);
}
}
}