From d59d8bacd63f531c49536e487fcfae2e9cc5477d Mon Sep 17 00:00:00 2001
From: Wilmer Arambula
Date: Thu, 16 Nov 2023 18:49:49 -0300
Subject: [PATCH 1/4] Initital commit.
---
README.md | 62 ++++++++++++++------
composer.json | 29 ++++++++--
phpstan.neon | 7 +--
phpunit.xml.dist | 4 +-
src/BootstrapAsset.php | 28 +++++++++
src/BootstrapPluginAsset.php | 35 ++++++++++++
src/Example.php | 13 -----
tests/AssetTest.php | 106 +++++++++++++++++++++++++++++++++++
tests/ExampleTest.php | 18 ------
tests/TestCase.php | 64 +++++++++++++++++++++
tests/bootstrap.php | 21 +++++++
tests/runtime/.gitkeep | 0
tests/support/main.php | 4 ++
13 files changed, 332 insertions(+), 59 deletions(-)
create mode 100644 src/BootstrapAsset.php
create mode 100644 src/BootstrapPluginAsset.php
delete mode 100644 src/Example.php
create mode 100644 tests/AssetTest.php
delete mode 100644 tests/ExampleTest.php
create mode 100644 tests/TestCase.php
create mode 100644 tests/bootstrap.php
create mode 100644 tests/runtime/.gitkeep
create mode 100644 tests/support/main.php
diff --git a/README.md b/README.md
index 7fc0a59..c4cdff4 100644
--- a/README.md
+++ b/README.md
@@ -1,8 +1,8 @@
-
+
-
Yii2-Template.
+ Asset for Twitter Bootstrap5.
@@ -13,32 +13,62 @@
-
-
+
+
-
-
+
+
-
+
-
+
-
-
-
-
+
+
+
+
-## Requirements
+## Installation
+
+The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
+
+Either run
-The minimun version of `PHP` required by this package is `PHP 8.1`.
+```
+composer require --dev --prefer-dist yii2-extensions/asset-bootstrap5
+```
-For install this package, you need [composer](https://getcomposer.org/).
+or add
+
+```
+"yii2-extensions/asset-bootstrap5": "dev-main"
+```
+
+to the require-dev section of your `composer.json` file.
## Usage
-[Check the documentation docs](/docs/README.md) to learn about usage.
+```php
+=8.1",
+ "npm-asset/bootstrap": "^5.3",
+ "oomphinc/composer-installers-extender": "^2.0",
"yiisoft/yii2": "^2.2"
},
"require-dev": {
"maglnet/composer-require-checker": "^4.6",
+ "php-forge/support": "dev-main",
"phpunit/phpunit": "^10.2",
"yii2-extensions/phpstan": "dev-main"
},
"autoload": {
"psr-4": {
- "yii\\template\\": "src"
+ "Yii2\\Asset\\": "src"
}
},
"autoload-dev": {
"psr-4": {
- "yii\\template\\tests\\": "tests"
+ "Yii2\\Asset\\Tests\\": "tests"
}
},
"extra": {
"branch-alias": {
"dev-main": "1.0.x-dev"
+ },
+ "installer-types": [
+ "bower-asset",
+ "npm-asset"
+ ],
+ "installer-paths": {
+ "./node_modules/{$name}": [
+ "type:bower-asset",
+ "type:npm-asset"
+ ]
}
},
"config": {
"sort-packages": true,
"allow-plugins": {
- "yiisoft/yii2-composer": true
+ "yiisoft/yii2-composer": true,
+ "composer/installers": true,
+ "oomphinc/composer-installers-extender": true
}
},
"scripts": {
diff --git a/phpstan.neon b/phpstan.neon
index 0b239cb..17ba560 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -1,12 +1,11 @@
includes:
- vendor/yii2-extensions/phpstan/extension.neon
parameters:
+ bootstrapFiles:
+ - tests/bootstrap.php
+
dynamicConstantNames:
- - YII_DEBUG
- YII_ENV
- - YII_ENV_DEV
- - YII_ENV_PROD
- - YII_ENV_TEST
level: 2
diff --git a/phpunit.xml.dist b/phpunit.xml.dist
index f29a28d..e95d41e 100644
--- a/phpunit.xml.dist
+++ b/phpunit.xml.dist
@@ -2,7 +2,7 @@
-
+
tests
diff --git a/src/BootstrapAsset.php b/src/BootstrapAsset.php
new file mode 100644
index 0000000..2e59d22
--- /dev/null
+++ b/src/BootstrapAsset.php
@@ -0,0 +1,28 @@
+js[] = $assetBootstrap;
+ $this->publishOptions['only'] = [$assetBootstrap];
+ }
+}
diff --git a/src/BootstrapPluginAsset.php b/src/BootstrapPluginAsset.php
new file mode 100644
index 0000000..973bd91
--- /dev/null
+++ b/src/BootstrapPluginAsset.php
@@ -0,0 +1,35 @@
+js[] = $assetBootstrapPlugin;
+ $this->publishOptions['only'] = [$assetBootstrapPlugin];
+ }
+}
diff --git a/src/Example.php b/src/Example.php
deleted file mode 100644
index 067eeb8..0000000
--- a/src/Example.php
+++ /dev/null
@@ -1,13 +0,0 @@
-getView();
+
+ $this->assertEmpty($view->assetBundles);
+
+ BootstrapAsset::register($view);
+
+ $this->assertCount(1, $view->assetBundles);
+ $this->assertArrayHasKey(BootstrapAsset::class, $view->assetBundles);
+ }
+
+ public function testBootstrapAssetSourcesPublish(): void
+ {
+ $view = new View();
+ $bundle = BootstrapAsset::register($view);
+
+ $this->assertDirectoryExists($bundle->basePath);
+ $this->sourcesPublishVerifyFiles('js', $bundle);
+ }
+
+ public function testBootstrapAssetRegister(): void
+ {
+ $view = new View();
+
+ $this->assertEmpty($view->assetBundles);
+
+ BootstrapAsset::register($view);
+
+ $this->assertCount(1, $view->assetBundles);
+ $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);
+
+ $result = $view->renderFile(__DIR__ . '/support/main.php');
+
+ $this->assertMatchesRegularExpression('/bootstrap.css/', $result);
+ }
+
+ public function testBootstrapPluginAssetSimpleDependency(): void
+ {
+ $view = Yii::$app->getView();
+
+ $this->assertEmpty($view->assetBundles);
+
+ BootstrapPluginAsset::register($view);
+
+ $this->assertCount(2, $view->assetBundles);
+ $this->assertArrayHasKey(BootstrapAsset::class, $view->assetBundles);
+ $this->assertArrayHasKey(BootstrapPluginAsset::class, $view->assetBundles);
+ $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);
+ }
+
+ public function testBootstrapPluginAssetSourcesPublish(): void
+ {
+ $view = new View();
+ $bundle = BootstrapPluginAsset::register($view);
+
+ $this->assertDirectoryExists($bundle->basePath);
+ $this->sourcesPublishVerifyFiles('js', $bundle);
+ }
+
+ public function testBootstrapPluginAssetRegister(): void
+ {
+ $view = new View();
+
+ $this->assertEmpty($view->assetBundles);
+
+ BootstrapPluginAsset::register($view);
+
+ $this->assertCount(2, $view->assetBundles);
+ $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapPluginAsset::class]);
+ $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]);
+
+ $result = $view->renderFile(__DIR__ . '/support/main.php');
+
+ $this->assertMatchesRegularExpression('/bootstrap.css/', $result);
+ $this->assertMatchesRegularExpression('/bootstrap.bundle.js/', $result);
+ }
+
+ private function sourcesPublishVerifyFiles(string $type, object $bundle): void
+ {
+ foreach ($bundle->$type as $filename) {
+ $publishedFile = $bundle->basePath . DIRECTORY_SEPARATOR . $filename;
+ $sourceFile = $bundle->sourcePath . DIRECTORY_SEPARATOR . $filename;
+ $this->assertFileExists($publishedFile);
+ $this->assertFileEquals($publishedFile, $sourceFile);
+ }
+
+ $this->assertDirectoryExists($bundle->basePath);
+ }
+}
diff --git a/tests/ExampleTest.php b/tests/ExampleTest.php
deleted file mode 100644
index 2825a4e..0000000
--- a/tests/ExampleTest.php
+++ /dev/null
@@ -1,18 +0,0 @@
-assertTrue($example->getExample());
- }
-}
diff --git a/tests/TestCase.php b/tests/TestCase.php
new file mode 100644
index 0000000..0faf6c4
--- /dev/null
+++ b/tests/TestCase.php
@@ -0,0 +1,64 @@
+ 'testapp',
+ 'aliases' => [
+ '@app' => dirname(__DIR__),
+ '@bower' => '@app/node_modules',
+ '@npm' => '@app/node_modules',
+ '@public' => '@app/public',
+ '@vendor' => '@app/vendor',
+ '@web' => __DIR__ . '/runtime',
+ ],
+ 'basePath' => dirname(__DIR__),
+ 'vendorPath' => dirname(__DIR__) . '/vendor',
+ 'components' => [
+ 'assetManager' => [
+ 'basePath' => __DIR__ . '/runtime',
+ ],
+ 'request' => [
+ 'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
+ 'scriptFile' => __DIR__ . '/index.php',
+ 'scriptUrl' => '/index.php',
+ ],
+ ],
+ ],
+ );
+ }
+
+ protected function setup(): void
+ {
+ parent::setUp();
+ $this->mockWebApplication();
+ }
+
+ protected function tearDown(): void
+ {
+ parent::tearDown();
+ $this->destroyApplication();
+ Assert::removeFilesFromDirectory(__DIR__ . '/runtime');
+ }
+}
diff --git a/tests/bootstrap.php b/tests/bootstrap.php
new file mode 100644
index 0000000..ed7d498
--- /dev/null
+++ b/tests/bootstrap.php
@@ -0,0 +1,21 @@
+
+beginPage();?>1head();?>2beginBody();?>3endBody();?>4endPage();
From a586e0283b4a107fb90f05da3c98aa6eacc001b8 Mon Sep 17 00:00:00 2001
From: Wilmer Arambula
Date: Thu, 16 Nov 2023 18:51:44 -0300
Subject: [PATCH 2/4] Fix tests.
---
composer-require-checker.json | 5 +++++
1 file changed, 5 insertions(+)
create mode 100644 composer-require-checker.json
diff --git a/composer-require-checker.json b/composer-require-checker.json
new file mode 100644
index 0000000..e6a9190
--- /dev/null
+++ b/composer-require-checker.json
@@ -0,0 +1,5 @@
+{
+ "symbol-whitelist": [
+ "YII_ENV"
+ ]
+}
From cdd2ad2d522cf297f80d57c24d826928ac95f911 Mon Sep 17 00:00:00 2001
From: Wilmer Arambula <42547589+terabytesoftw@users.noreply.github.com>
Date: Thu, 16 Nov 2023 18:52:48 -0300
Subject: [PATCH 3/4] Apply fixes from StyleCI (#2)
---
tests/AssetTest.php | 2 --
1 file changed, 2 deletions(-)
diff --git a/tests/AssetTest.php b/tests/AssetTest.php
index 9bd499c..ece90da 100644
--- a/tests/AssetTest.php
+++ b/tests/AssetTest.php
@@ -8,9 +8,7 @@
use Yii2\Asset\BootstrapAsset;
use Yii2\Asset\BootstrapPluginAsset;
use yii\web\AssetBundle;
-use yii\web\JqueryAsset;
use yii\web\View;
-use yii\web\YiiAsset;
final class AssetTest extends TestCase
{
From 09d59ab551df699ae04d4d6ac85fb912e2861ce6 Mon Sep 17 00:00:00 2001
From: Wilmer Arambula
Date: Fri, 17 Nov 2023 06:22:27 -0300
Subject: [PATCH 4/4] Fix minor corrections.
---
README.md | 2 +-
phpstan.neon | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/README.md b/README.md
index c4cdff4..5e01d07 100644
--- a/README.md
+++ b/README.md
@@ -23,7 +23,7 @@
-
+
diff --git a/phpstan.neon b/phpstan.neon
index 17ba560..19c5038 100644
--- a/phpstan.neon
+++ b/phpstan.neon
@@ -7,7 +7,7 @@ parameters:
dynamicConstantNames:
- YII_ENV
- level: 2
+ level: 5
paths:
- src