-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #553 from hydephp/develop
v0.41.0-beta - 2022-06-24 - Improve the AssetService
- Loading branch information
Showing
8 changed files
with
288 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Facades; | ||
|
||
use Hyde\Framework\Contracts\AssetServiceContract; | ||
use Illuminate\Support\Facades\Facade; | ||
|
||
/** | ||
* @see \Hyde\Framework\Services\AssetService | ||
* | ||
* @method static string version() | ||
* @method static string stylePath() | ||
* @method static string scriptPath() | ||
* @method static string constructCdnPath(string $file) | ||
* @method static string cdnLink(string $file) | ||
* @method static bool hasMediaFile(string $file) | ||
*/ | ||
class Asset extends Facade | ||
{ | ||
protected static function getFacadeAccessor(): string | ||
{ | ||
return AssetServiceContract::class; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<?php | ||
|
||
use Hyde\Framework\Contracts\AssetServiceContract; | ||
use Hyde\Framework\Facades\Asset; | ||
use Hyde\Framework\Services\AssetService; | ||
use Hyde\Testing\TestCase; | ||
|
||
/** | ||
* @covers \Hyde\Framework\Facades\Asset | ||
*/ | ||
class AssetFacadeTest extends TestCase | ||
{ | ||
public function test_asset_facade_returns_the_asset_service() | ||
{ | ||
$this->assertInstanceOf(AssetServiceContract::class, Asset::getFacadeRoot()); | ||
} | ||
|
||
public function test_facade_returns_same_instance_as_bound_by_the_container() | ||
{ | ||
$this->assertSame(Asset::getFacadeRoot(), app(AssetServiceContract::class)); | ||
} | ||
|
||
public function test_asset_facade_can_call_methods_on_the_asset_service() | ||
{ | ||
$service = new AssetService(); | ||
$this->assertEquals($service->version(), Asset::version()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Testing\Unit\Views; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\AssetService; | ||
use Hyde\Testing\TestCase; | ||
use Illuminate\Support\Facades\Blade; | ||
|
||
/** | ||
* @see resources/views/layouts/scripts.blade.php | ||
*/ | ||
class ScriptsComponentViewTest extends TestCase | ||
{ | ||
protected ?string $mockCurrentPage = null; | ||
|
||
protected function renderTestView(): string | ||
{ | ||
view()->share('currentPage', $this->mockCurrentPage ?? ''); | ||
|
||
return Blade::render(file_get_contents( | ||
Hyde::vendorPath('resources/views/layouts/scripts.blade.php') | ||
)); | ||
} | ||
|
||
public function test_component_can_be_rendered() | ||
{ | ||
$this->assertStringContainsString('<script defer', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_has_link_to_app_js_file_when_it_exists() | ||
{ | ||
touch(Hyde::path('_media/app.js')); | ||
$this->assertStringContainsString('<script defer src="media/app.js"', $this->renderTestView()); | ||
unlink(Hyde::path('_media/app.js')); | ||
} | ||
|
||
public function test_component_does_not_render_link_to_app_js_when_it_does_not_exist() | ||
{ | ||
$this->assertStringNotContainsString('<script defer src="media/app.js"', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_uses_relative_path_to_app_js_file_for_nested_pages() | ||
{ | ||
touch(Hyde::path('_media/app.js')); | ||
$this->mockCurrentPage = 'foo'; | ||
$this->assertStringContainsString('<script defer src="media/app.js"', $this->renderTestView()); | ||
$this->mockCurrentPage = 'foo/bar'; | ||
$this->assertStringContainsString('<script defer src="../media/app.js"', $this->renderTestView()); | ||
$this->mockCurrentPage = 'foo/bar/cat.html'; | ||
$this->assertStringContainsString('<script defer src="../../media/app.js"', $this->renderTestView()); | ||
$this->mockCurrentPage = null; | ||
unlink(Hyde::path('_media/app.js')); | ||
} | ||
|
||
public function test_scripts_can_be_pushed_to_the_component_scripts_stack() | ||
{ | ||
view()->share('currentPage', ''); | ||
|
||
$this->assertStringContainsString('foo bar', | ||
Blade::render(' | ||
@push("scripts") | ||
foo bar | ||
@endpush | ||
@include("hyde::layouts.scripts")' | ||
) | ||
); | ||
} | ||
|
||
public function test_component_renders_link_to_hyde_js_when_it_exists() | ||
{ | ||
touch(Hyde::path('_media/hyde.js')); | ||
$this->assertStringContainsString('<script defer src="media/hyde.js"', $this->renderTestView()); | ||
unlink(Hyde::path('_media/hyde.js')); | ||
} | ||
|
||
public function test_component_does_not_render_link_to_hyde_js_when_it_does_not_exist() | ||
{ | ||
$this->assertStringNotContainsString('<script defer src="media/hyde.js"', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_renders_cdn_link_when_no_local_file_exists() | ||
{ | ||
$this->assertStringContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_does_not_render_cdn_link_when_a_local_file_exists() | ||
{ | ||
touch(Hyde::path('_media/hyde.js')); | ||
$this->assertStringNotContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); | ||
unlink(Hyde::path('_media/hyde.js')); | ||
} | ||
|
||
public function test_cdn_link_uses_the_correct_version_defined_in_the_asset_manager() | ||
{ | ||
$expectedVersion = (new AssetService)->version(); | ||
$this->assertStringContainsString( | ||
'https://cdn.jsdelivr.net/npm/hydefront@'.$expectedVersion.'/dist/hyde.js', | ||
$this->renderTestView() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Testing\Unit\Views; | ||
|
||
use Hyde\Framework\Hyde; | ||
use Hyde\Framework\Services\AssetService; | ||
use Hyde\Testing\TestCase; | ||
use Illuminate\Support\Facades\Blade; | ||
|
||
/** | ||
* @see resources/views/layouts/styles.blade.php | ||
*/ | ||
class StylesComponentViewTest extends TestCase | ||
{ | ||
protected ?string $mockCurrentPage = null; | ||
|
||
protected function renderTestView(): string | ||
{ | ||
view()->share('currentPage', $this->mockCurrentPage ?? ''); | ||
|
||
return Blade::render(file_get_contents( | ||
Hyde::vendorPath('resources/views/layouts/styles.blade.php') | ||
)); | ||
} | ||
|
||
public function test_component_can_be_rendered() | ||
{ | ||
$this->assertStringContainsString('<link rel="stylesheet"', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_has_link_to_app_css_file() | ||
{ | ||
$this->assertStringContainsString('<link rel="stylesheet" href="media/app.css"', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_uses_relative_path_to_app_css_file_for_nested_pages() | ||
{ | ||
$this->mockCurrentPage = 'foo'; | ||
$this->assertStringContainsString('<link rel="stylesheet" href="media/app.css"', $this->renderTestView()); | ||
$this->mockCurrentPage = 'foo/bar'; | ||
$this->assertStringContainsString('<link rel="stylesheet" href="../media/app.css"', $this->renderTestView()); | ||
$this->mockCurrentPage = 'foo/bar/cat.html'; | ||
$this->assertStringContainsString('<link rel="stylesheet" href="../../media/app.css"', $this->renderTestView()); | ||
$this->mockCurrentPage = null; | ||
} | ||
|
||
public function test_component_does_not_render_link_to_app_css_when_it_does_not_exist() | ||
{ | ||
rename(Hyde::path('_media/app.css'), Hyde::path('_media/app.css.bak')); | ||
$this->assertStringNotContainsString('<link rel="stylesheet" href="media/app.css"', $this->renderTestView()); | ||
rename(Hyde::path('_media/app.css.bak'), Hyde::path('_media/app.css')); | ||
} | ||
|
||
public function test_styles_can_be_pushed_to_the_component_styles_stack() | ||
{ | ||
view()->share('currentPage', ''); | ||
|
||
$this->assertStringContainsString('foo bar', | ||
Blade::render(' | ||
@push("styles") | ||
foo bar | ||
@endpush | ||
@include("hyde::layouts.styles")' | ||
) | ||
); | ||
} | ||
|
||
public function test_component_renders_link_to_hyde_css_when_it_exists() | ||
{ | ||
touch(Hyde::path('_media/hyde.css')); | ||
$this->assertStringContainsString('<link rel="stylesheet" href="media/hyde.css"', $this->renderTestView()); | ||
unlink(Hyde::path('_media/hyde.css')); | ||
} | ||
|
||
public function test_component_does_not_render_link_to_hyde_css_when_it_does_not_exist() | ||
{ | ||
$this->assertStringNotContainsString('<link rel="stylesheet" href="media/hyde.css"', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_renders_cdn_link_when_no_local_file_exists() | ||
{ | ||
$this->assertStringContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); | ||
} | ||
|
||
public function test_component_does_not_render_cdn_link_when_a_local_file_exists() | ||
{ | ||
touch(Hyde::path('_media/hyde.css')); | ||
$this->assertStringNotContainsString('https://cdn.jsdelivr.net/npm/hydefront', $this->renderTestView()); | ||
unlink(Hyde::path('_media/hyde.css')); | ||
} | ||
|
||
public function test_cdn_link_uses_the_correct_version_defined_in_the_asset_manager() | ||
{ | ||
$expectedVersion = (new AssetService)->version(); | ||
$this->assertStringContainsString( | ||
'https://cdn.jsdelivr.net/npm/hydefront@'.$expectedVersion.'/dist/hyde.css', | ||
$this->renderTestView() | ||
); | ||
} | ||
} |