diff --git a/README.md b/README.md index 7fc0a59..5e01d07 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@

- + -

Yii2-Template.

+

Asset for Twitter Bootstrap5.


@@ -13,32 +13,62 @@ yii2-version - - PHPUnit + + PHPUnit - - Codecov + + Codecov - + PHPStan - - PHPStan level - - - Code style - + + PHPStan level + + + Code style +

-## 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..19c5038 100644 --- a/phpstan.neon +++ b/phpstan.neon @@ -1,14 +1,13 @@ 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 + level: 5 paths: - src 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();