From ab9b9c4b26478d402762bf96b74eabe7b48f190f Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 06:28:57 -0300 Subject: [PATCH 1/6] Update asset dependencies. --- src/Asset/DateTimePickerAsset.php | 5 +---- src/Asset/JQueryProviderAsset.php | 5 +++++ src/DateTimePicker.php | 2 +- tests/AssetTest.php | 4 ++-- 4 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/Asset/DateTimePickerAsset.php b/src/Asset/DateTimePickerAsset.php index 4c44c76..7bf7688 100644 --- a/src/Asset/DateTimePickerAsset.php +++ b/src/Asset/DateTimePickerAsset.php @@ -17,10 +17,7 @@ final class DateTimePickerAsset extends AssetBundle /** * @phpstan-var array */ - public $depends = [ - PopperAsset::class, - JQueryProviderAsset::class, - ]; + public $depends = [PopperAsset::class]; public function __construct() { diff --git a/src/Asset/JQueryProviderAsset.php b/src/Asset/JQueryProviderAsset.php index 15f9872..82fafee 100644 --- a/src/Asset/JQueryProviderAsset.php +++ b/src/Asset/JQueryProviderAsset.php @@ -13,6 +13,11 @@ final class JQueryProviderAsset extends AssetBundle { public $sourcePath = '@npm/eonasdan--tempus-dominus/dist/js'; + /** + * @phpstan-var array + */ + public $depends = [DateTimePickerAsset::class]; + public function __construct() { parent::__construct(); diff --git a/src/DateTimePicker.php b/src/DateTimePicker.php index af16229..4b512bd 100644 --- a/src/DateTimePicker.php +++ b/src/DateTimePicker.php @@ -195,7 +195,7 @@ private function registerClientScript(): void match ($this->cdn) { true => Asset\DateTimePickerCdnAsset::register($view), - default => Asset\DateTimePickerAsset::register($view), + default => Asset\JQueryProviderAsset::register($view), }; $view->registerJs($this->getScript()); diff --git a/tests/AssetTest.php b/tests/AssetTest.php index bcb4a85..0303dd8 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -30,7 +30,7 @@ public function testDateTimePickerAssetSimpleDependency(): void { $this->assertEmpty($this->view->assetBundles); - DateTimePickerAsset::register($this->view); + JQueryProviderAsset::register($this->view); $this->assertCount(3, $this->view->assetBundles); @@ -43,7 +43,7 @@ public function testDateTimePickerAssetRegister(): void { $this->assertEmpty($this->view->assetBundles); - DateTimePickerAsset::register($this->view); + JQueryProviderAsset::register($this->view); $this->assertCount(3, $this->view->assetBundles); From e1c13d11ca05b3f953ab9723a57660c661d34840 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 07:04:08 -0300 Subject: [PATCH 2/6] Remove unnecessary asset files from AssetTest.php. --- tests/AssetTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/AssetTest.php b/tests/AssetTest.php index 0303dd8..08d0b4f 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -59,6 +59,11 @@ public function testDateTimePickerAssetRegister(): void ], ); + $this->assertStringNotContainsString('css/tempus-dominus.min.css', $result); + $this->assertStringNotContainsString('js/tempus-dominus.min.js', $result); + $this->assertStringNotContainsString('jQuery-provider.min.js', $result); + $this->assertStringNotContainsString('popper.min.js', $result); + $this->assertStringNotContainsString('jquery.min.js', $result); $this->assertStringContainsString('css/tempus-dominus.css', $result); $this->assertStringContainsString('js/tempus-dominus.js', $result); $this->assertStringContainsString('jQuery-provider.js', $result); From 70abbf286b60d1da06e4f4a1e011165c16b9703d Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 07:10:44 -0300 Subject: [PATCH 3/6] Refactor tests and add TestSupport trait. --- tests/AssetTest.php | 7 +++- tests/DateTimePickerTest.php | 6 ++- tests/ExceptionTest.php | 2 +- tests/Support/TestSupport.php | 71 +++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 5 deletions(-) create mode 100644 tests/Support/TestSupport.php diff --git a/tests/AssetTest.php b/tests/AssetTest.php index 08d0b4f..992022f 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -9,13 +9,16 @@ Asset\DateTimePickerAsset, Asset\DateTimePickerCdnAsset, Asset\JQueryProviderAsset, - DateTimePicker + DateTimePicker, + Tests\Support\TestSupport }; use Yii; use yii\web\AssetBundle; -final class AssetTest extends TestCase +final class AssetTest extends \PHPUnit\Framework\TestCase { + use TestSupport; + public function setup(): void { parent::setUp(); diff --git a/tests/DateTimePickerTest.php b/tests/DateTimePickerTest.php index cd33808..594abe6 100644 --- a/tests/DateTimePickerTest.php +++ b/tests/DateTimePickerTest.php @@ -5,11 +5,13 @@ namespace Yii2\Extensions\DateTimePicker\Tests; use PHPForge\{Html\Textual\I, Support\Assert}; -use Yii2\Extensions\DateTimePicker\{DateTimePicker, Tests\Support\DateTimePickerModel}; +use Yii2\Extensions\DateTimePicker\{DateTimePicker, Tests\Support\DateTimePickerModel, Tests\Support\TestSupport}; use Yii; -final class DateTimePickerTest extends TestCase +final class DateTimePickerTest extends \PHPUnit\Framework\TestCase { + use TestSupport; + public function setup(): void { parent::setUp(); diff --git a/tests/ExceptionTest.php b/tests/ExceptionTest.php index 0d42871..b28b939 100644 --- a/tests/ExceptionTest.php +++ b/tests/ExceptionTest.php @@ -7,7 +7,7 @@ use Yii2\Extensions\DateTimePicker\DateTimePicker; use yii\base\InvalidConfigException; -final class ExceptionTest extends TestCase +final class ExceptionTest extends \PHPUnit\Framework\TestCase { public function testWithoutModelWithoutName(): void { diff --git a/tests/Support/TestSupport.php b/tests/Support/TestSupport.php new file mode 100644 index 0000000..c180a6f --- /dev/null +++ b/tests/Support/TestSupport.php @@ -0,0 +1,71 @@ +destroyApplication(); + } + + protected function mockApplication(): void + { + new Application( + [ + 'id' => 'testapp', + 'aliases' => [ + '@root' => dirname(__DIR__, 2), + '@bower' => '@vendor/bower-asset', + '@npm' => '@vendor/npm-asset', + ], + 'basePath' => dirname(__DIR__, 2), + 'components' => [ + 'assetManager' => [ + 'appendTimestamp' => false, + 'basePath' => __DIR__ . '/runtime', + 'baseUrl' => '/', + ], + 'i18n' => [ + 'translations' => [ + 'yii2.extensions.datetime.picker' => [ + 'class' => PhpMessageSource::class, + 'basePath' => dirname(__DIR__, 2) . '/resource/message', + ], + ], + ], + 'request' => [ + 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq', + 'scriptFile' => __DIR__ . '/index.php', + 'scriptUrl' => '/index.php', + ], + ], + ], + ); + } + + /** + * Destroys application in Yii::$app by setting it to null. + */ + protected function destroyApplication() + { + Yii::$app = null; + Yii::$container = new Container(); + Assert::removeFilesFromDirectory(__DIR__ . '/runtime'); + + unset($this->view); + } +} From 54901ec1a48bf984705124d33a07c8c6cd2b5206 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 07:18:49 -0300 Subject: [PATCH 4/6] Refactor asset registration and update asset manager configuration. --- tests/AssetTest.php | 16 +++++----------- tests/Support/TestSupport.php | 5 +++++ 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/tests/AssetTest.php b/tests/AssetTest.php index 992022f..2245c9e 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -44,12 +44,6 @@ public function testDateTimePickerAssetSimpleDependency(): void public function testDateTimePickerAssetRegister(): void { - $this->assertEmpty($this->view->assetBundles); - - JQueryProviderAsset::register($this->view); - - $this->assertCount(3, $this->view->assetBundles); - $result = $this->view->renderFile( __DIR__ . '/Support/main.php', [ @@ -62,11 +56,11 @@ public function testDateTimePickerAssetRegister(): void ], ); - $this->assertStringNotContainsString('css/tempus-dominus.min.css', $result); - $this->assertStringNotContainsString('js/tempus-dominus.min.js', $result); - $this->assertStringNotContainsString('jQuery-provider.min.js', $result); - $this->assertStringNotContainsString('popper.min.js', $result); - $this->assertStringNotContainsString('jquery.min.js', $result); + $directory = __DIR__ . '/Support/runtime/16b8de20'; + + $this->assertDirectoryDoesNotExist("$directory/css/tempus-dominus.min.css"); + $this->assertDirectoryDoesNotExist("$directory/css/js/tempus-dominus.min.js"); + $this->assertDirectoryDoesNotExist("$directory/css/js/jQuery-provider.min.js"); $this->assertStringContainsString('css/tempus-dominus.css', $result); $this->assertStringContainsString('js/tempus-dominus.js', $result); $this->assertStringContainsString('jQuery-provider.js', $result); diff --git a/tests/Support/TestSupport.php b/tests/Support/TestSupport.php index c180a6f..a4a55e8 100644 --- a/tests/Support/TestSupport.php +++ b/tests/Support/TestSupport.php @@ -38,6 +38,7 @@ protected function mockApplication(): void 'appendTimestamp' => false, 'basePath' => __DIR__ . '/runtime', 'baseUrl' => '/', + 'forceCopy' => true, ], 'i18n' => [ 'translations' => [ @@ -55,6 +56,10 @@ protected function mockApplication(): void ], ], ); + + Yii::$app->assetManager->hashCallback = static function (string $path) { + return '16b8de20'; + }; } /** From 2ee39ba891819a71645e6c86c50752468b851c44 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 07:27:30 -0300 Subject: [PATCH 5/6] Kill mutants. --- tests/AssetTest.php | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/tests/AssetTest.php b/tests/AssetTest.php index 2245c9e..caf008b 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -58,9 +58,16 @@ public function testDateTimePickerAssetRegister(): void $directory = __DIR__ . '/Support/runtime/16b8de20'; - $this->assertDirectoryDoesNotExist("$directory/css/tempus-dominus.min.css"); - $this->assertDirectoryDoesNotExist("$directory/css/js/tempus-dominus.min.js"); - $this->assertDirectoryDoesNotExist("$directory/css/js/jQuery-provider.min.js"); + $this->assertFileDoesNotExist("$directory/css/tempus-dominus.min.css"); + $this->assertFileDoesNotExist("$directory/js/tempus-dominus.min.js"); + $this->assertFileDoesNotExist("$directory/jQuery-provider.min.js"); + $this->assertFileDoesNotExist("$directory/popper.min.js"); + + $this->assertFileExists("$directory/css/tempus-dominus.css"); + $this->assertFileExists("$directory/js/tempus-dominus.js"); + $this->assertFileExists("$directory/jQuery-provider.js"); + $this->assertFileExists("$directory/popper.js"); + $this->assertStringContainsString('css/tempus-dominus.css', $result); $this->assertStringContainsString('js/tempus-dominus.js', $result); $this->assertStringContainsString('jQuery-provider.js', $result); From f8eb0e2bab14eb4ba648cee4d4ad6103e7113ac5 Mon Sep 17 00:00:00 2001 From: Wilmer Arambula Date: Wed, 28 Feb 2024 07:29:32 -0300 Subject: [PATCH 6/6] Kill mutants. --- tests/AssetTest.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/AssetTest.php b/tests/AssetTest.php index caf008b..e22f735 100644 --- a/tests/AssetTest.php +++ b/tests/AssetTest.php @@ -59,12 +59,16 @@ public function testDateTimePickerAssetRegister(): void $directory = __DIR__ . '/Support/runtime/16b8de20'; $this->assertFileDoesNotExist("$directory/css/tempus-dominus.min.css"); + $this->assertFileDoesNotExist("$directory/css/tempus-dominus.min.css.map"); $this->assertFileDoesNotExist("$directory/js/tempus-dominus.min.js"); + $this->assertFileDoesNotExist("$directory/js/tempus-dominus.min.js.map"); $this->assertFileDoesNotExist("$directory/jQuery-provider.min.js"); $this->assertFileDoesNotExist("$directory/popper.min.js"); $this->assertFileExists("$directory/css/tempus-dominus.css"); + $this->assertFileExists("$directory/css/tempus-dominus.css.map"); $this->assertFileExists("$directory/js/tempus-dominus.js"); + $this->assertFileExists("$directory/js/tempus-dominus.js.map"); $this->assertFileExists("$directory/jQuery-provider.js"); $this->assertFileExists("$directory/popper.js");