|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Yii2\Asset\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\RequiresPhp; |
| 8 | +use Yii; |
| 9 | +use Yii2\Asset\BootstrapAsset; |
| 10 | +use Yii2\Asset\Tests\Support\TestSupport; |
| 11 | +use yii\web\AssetBundle; |
| 12 | +use yii\web\View; |
| 13 | + |
| 14 | +use function runkit_constant_redefine; |
| 15 | + |
| 16 | +final class BootstrapAssetTest extends \PHPUnit\Framework\TestCase |
| 17 | +{ |
| 18 | + use TestSupport; |
| 19 | + |
| 20 | + public function testRegister(): void |
| 21 | + { |
| 22 | + $view = new View(); |
| 23 | + |
| 24 | + $this->assertEmpty($view->assetBundles); |
| 25 | + |
| 26 | + BootstrapAsset::register($view); |
| 27 | + |
| 28 | + $this->assertCount(1, $view->assetBundles); |
| 29 | + $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]); |
| 30 | + |
| 31 | + $result = $view->renderFile(__DIR__ . '/Support/main.php'); |
| 32 | + |
| 33 | + $this->assertStringContainsString('bootstrap.css', $result); |
| 34 | + |
| 35 | + $this->assertSame(['bootstrap.css'], Yii::$app->assetManager->bundles[BootstrapAsset::class]->css); |
| 36 | + $this->assertFileExists(__DIR__ . '/Support/runtime/55145ba9/bootstrap.css'); |
| 37 | + $this->assertFileExists(__DIR__ . '/Support/runtime/55145ba9/bootstrap.css.map'); |
| 38 | + $this->assertFileDoesNotExist(__DIR__ . '/Support/runtime/55145ba9/bootstrap.min.css'); |
| 39 | + $this->assertFileDoesNotExist(__DIR__ . '/Support/runtime/55145ba9/bootstrap.min.css.map'); |
| 40 | + } |
| 41 | + |
| 42 | + #[RequiresPhp('8.1')] |
| 43 | + public function testRegisterWithEnvironmentProd(): void |
| 44 | + { |
| 45 | + runkit_constant_redefine('YII_ENV', 'prod'); |
| 46 | + |
| 47 | + $view = new View(); |
| 48 | + |
| 49 | + $this->assertEmpty($view->assetBundles); |
| 50 | + |
| 51 | + BootstrapAsset::register($view); |
| 52 | + |
| 53 | + $this->assertCount(1, $view->assetBundles); |
| 54 | + $this->assertInstanceOf(AssetBundle::class, $view->assetBundles[BootstrapAsset::class]); |
| 55 | + |
| 56 | + $result = $view->renderFile(__DIR__ . '/Support/main.php'); |
| 57 | + |
| 58 | + $this->assertStringContainsString('bootstrap.min.css', $result); |
| 59 | + |
| 60 | + $this->assertSame(['bootstrap.min.css'], Yii::$app->assetManager->bundles[BootstrapAsset::class]->css); |
| 61 | + $this->assertFileExists(__DIR__ . '/Support/runtime/55145ba9/bootstrap.min.css'); |
| 62 | + $this->assertFileExists(__DIR__ . '/Support/runtime/55145ba9/bootstrap.min.css.map'); |
| 63 | + $this->assertFileDoesNotExist(__DIR__ . '/Support/runtime/55145ba9/bootstrap.css'); |
| 64 | + $this->assertFileDoesNotExist(__DIR__ . '/Support/runtime/55145ba9/bootstrap.css.map'); |
| 65 | + |
| 66 | + runkit_constant_redefine('YII_ENV', 'dev'); |
| 67 | + } |
| 68 | +} |
0 commit comments