-
-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Improve URL handling for
asset()
with tests
- Loading branch information
Showing
2 changed files
with
80 additions
and
21 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/** | ||
* This file is part of Bonfire. | ||
* | ||
* (c) Lonnie Ezell <[email protected]> | ||
* | ||
* For the full copyright and license information, please view | ||
* the LICENSE file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Tests\Assets; | ||
|
||
use CodeIgniter\Config\Factories; | ||
use InvalidArgumentException; | ||
use RuntimeException; | ||
use Tests\Support\TestCase; | ||
|
||
|
@@ -16,26 +28,52 @@ protected function setUp(): void | |
$this->mockCache(); | ||
parent::setUp(); | ||
|
||
helper('Bonfire\Assets\Helpers\assets'); | ||
helper(['Bonfire\Assets\Helpers\assets']); | ||
} | ||
|
||
public function testAssetThrowsNoFilename() | ||
public function testAssetThrowsNoFilenameExtension(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('You must provide a valid filename and extension to the asset() helper.'); | ||
|
||
\asset_link('foo', 'css'); | ||
asset_link('foo', 'css'); | ||
} | ||
|
||
public function testAssetThrowsNoFilename(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
|
||
asset_link('/admin/css/admin_missing.css', 'css'); | ||
} | ||
|
||
public function testAssetThrowsEmptyLocation(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
asset_link('/', 'css'); | ||
} | ||
|
||
public function testAssetThrowsNoExtension() | ||
public function testAssetThrowsNoExtension(): void | ||
{ | ||
$this->expectException(RuntimeException::class); | ||
$this->expectExceptionMessage('You must provide a valid filename and extension to the asset() helper.'); | ||
|
||
asset_link('admin/foo', 'css'); | ||
} | ||
|
||
public function testAssetVersion() | ||
public function testAssetIfIncorrectType(): void | ||
{ | ||
$this->assertEmpty(asset_link('/admin/css/admin.css', 'map')); | ||
} | ||
|
||
public function testAssetThrowsLocationAsFullURL(): void | ||
{ | ||
$this->expectException(InvalidArgumentException::class); | ||
|
||
asset_link('http://example.com/admin/css/admin.css', 'css'); | ||
} | ||
|
||
public function testAssetVersion(): void | ||
{ | ||
$config = config('Assets'); | ||
|
||
|
@@ -48,12 +86,14 @@ public function testAssetVersion() | |
// In testing environment, would be the current timestamp | ||
// so just test the pattern to ensure that works. | ||
preg_match('|assets/admin/css/admin~~([\d]+).css|i', $link, $matches); | ||
|
||
$this->assertIsNumeric($matches[1]); | ||
} | ||
|
||
public function testAssetFile() | ||
public function testAssetFile(): void | ||
{ | ||
$config = config('Assets'); | ||
$config = config('Assets'); | ||
|
||
$config->bustingType = 'file'; | ||
$config->separator = '~~'; | ||
Factories::injectMock('config', 'Assets', $config); | ||
|
@@ -63,6 +103,7 @@ public function testAssetFile() | |
// In testing environment, would be the current timestamp | ||
// so just test the pattern to ensure that works. | ||
preg_match('|assets/admin/css/admin~~([\d]+).css|i', $link, $matches); | ||
|
||
$this->assertSame(filemtime(BFPATH . '../themes/Admin/css/admin.css'), (int) $matches[1]); | ||
} | ||
} |