Skip to content

Commit

Permalink
Merge pull request #553 from hydephp/develop
Browse files Browse the repository at this point in the history
v0.41.0-beta - 2022-06-24 - Improve the AssetService
  • Loading branch information
caendesilva authored Jun 24, 2022
2 parents 7a0dda5 + 572287f commit abcb616
Show file tree
Hide file tree
Showing 8 changed files with 288 additions and 8 deletions.
10 changes: 6 additions & 4 deletions resources/views/layouts/scripts.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{-- The core HydeFront scripts --}}
@if(Hyde::scripts())
<script defer src="{{ Hyde::scripts() }}"></script>
@endif
@unless(Asset::hasMediaFile('hyde.js'))
<script defer src="{{ Asset::cdnLink('hyde.js') }}"></script>
@else
<script defer src="{{ Hyde::relativeLink('media/hyde.js', $currentPage) }}"></script>
@endunless

{{-- The compiled Laravel Mix scripts --}}
@if(Hyde::assetManager()->hasMediaFile('app.js'))
@if(Asset::hasMediaFile('app.js'))
<script defer src="{{ Hyde::relativeLink('media/app.js', $currentPage) }}"></script>
@endif

Expand Down
10 changes: 6 additions & 4 deletions resources/views/layouts/styles.blade.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{{-- The core HydeFront stylesheet --}}
@if(Hyde::styles())
<link rel="stylesheet" href="{{ Hyde::styles() }}">
@endif
@unless(Asset::hasMediaFile('hyde.css'))
<link rel="stylesheet" href="{{ Asset::cdnLink('hyde.css') }}">
@else
<link rel="stylesheet" href="{{ Hyde::relativeLink('media/hyde.css', $currentPage) }}">
@endunless

{{-- The compiled Tailwind/App styles --}}
@if(Hyde::assetManager()->hasMediaFile('app.css'))
@if(Asset::hasMediaFile('app.css'))
<link rel="stylesheet" href="{{ Hyde::relativeLink('media/app.css', $currentPage) }}">
@endif

Expand Down
7 changes: 7 additions & 0 deletions src/Concerns/Internal/AssetManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,16 @@
/**
* Offloads asset related methods for the Hyde Facade.
*
* @deprecated version 0.41.x - Use the Asset facade instead.
* @see \Hyde\Framework\Hyde
*/
trait AssetManager
{
/**
* Get the asset service instance.
*
* @deprecated version 0.41.x - Use the Asset facade instead.
*
* @return \Hyde\Framework\Contracts\AssetServiceContract
*/
public static function assetManager(): AssetServiceContract
Expand All @@ -23,6 +26,8 @@ public static function assetManager(): AssetServiceContract

/**
* Return the Hyde stylesheet.
*
* @deprecated version 0.41.x - Use the Asset facade instead.
*/
public static function styles(): string
{
Expand All @@ -31,6 +36,8 @@ public static function styles(): string

/**
* Return the Hyde scripts.
*
* @deprecated version 0.41.x - Use the Asset facade instead.
*/
public static function scripts(): string
{
Expand Down
24 changes: 24 additions & 0 deletions src/Facades/Asset.php
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;
}
}
13 changes: 13 additions & 0 deletions src/Services/AssetService.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
use Hyde\Framework\Contracts\AssetServiceContract;
use Hyde\Framework\Hyde;

/**
* @see \Hyde\Framework\Facades\Asset
*/
class AssetService implements AssetServiceContract
{
/**
Expand Down Expand Up @@ -34,6 +37,16 @@ public function constructCdnPath(string $file): string
return 'https://cdn.jsdelivr.net/npm/hydefront@'.$this->version().'/dist/'.$file;
}

/**
* Alias for constructCdnPath.
*
* @since v0.41.x
*/
public function cdnLink(string $file): string
{
return $this->constructCdnPath($file);
}

public function hasMediaFile(string $file): bool
{
return file_exists(Hyde::path('_media').'/'.$file);
Expand Down
28 changes: 28 additions & 0 deletions tests/Unit/AssetFacadeTest.php
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());
}
}
103 changes: 103 additions & 0 deletions tests/Unit/Views/ScriptsComponentViewTest.php
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()
);
}
}
101 changes: 101 additions & 0 deletions tests/Unit/Views/StylesComponentViewTest.php
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()
);
}
}

0 comments on commit abcb616

Please sign in to comment.