Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update asset dependencies. #16

Merged
merged 6 commits into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/Asset/DateTimePickerAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ final class DateTimePickerAsset extends AssetBundle
/**
* @phpstan-var array<array-key, mixed>
*/
public $depends = [
PopperAsset::class,
JQueryProviderAsset::class,
];
public $depends = [PopperAsset::class];

public function __construct()
{
Expand Down
5 changes: 5 additions & 0 deletions src/Asset/JQueryProviderAsset.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ final class JQueryProviderAsset extends AssetBundle
{
public $sourcePath = '@npm/eonasdan--tempus-dominus/dist/js';

/**
* @phpstan-var array<array-key, mixed>
*/
public $depends = [DateTimePickerAsset::class];

public function __construct()
{
parent::__construct();
Expand Down
2 changes: 1 addition & 1 deletion src/DateTimePicker.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
31 changes: 22 additions & 9 deletions tests/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -30,7 +33,7 @@ public function testDateTimePickerAssetSimpleDependency(): void
{
$this->assertEmpty($this->view->assetBundles);

DateTimePickerAsset::register($this->view);
JQueryProviderAsset::register($this->view);

$this->assertCount(3, $this->view->assetBundles);

Expand All @@ -41,12 +44,6 @@ public function testDateTimePickerAssetSimpleDependency(): void

public function testDateTimePickerAssetRegister(): void
{
$this->assertEmpty($this->view->assetBundles);

DateTimePickerAsset::register($this->view);

$this->assertCount(3, $this->view->assetBundles);

$result = $this->view->renderFile(
__DIR__ . '/Support/main.php',
[
Expand All @@ -59,6 +56,22 @@ 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");

$this->assertStringContainsString('css/tempus-dominus.css', $result);
$this->assertStringContainsString('js/tempus-dominus.js', $result);
$this->assertStringContainsString('jQuery-provider.js', $result);
Expand Down
6 changes: 4 additions & 2 deletions tests/DateTimePickerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion tests/ExceptionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
76 changes: 76 additions & 0 deletions tests/Support/TestSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
<?php

declare(strict_types=1);

namespace Yii2\Extensions\DateTimePicker\Tests\Support;

use PHPForge\Support\Assert;
use Yii;
use yii\{di\Container, i18n\PhpMessageSource, web\Application, web\View};

trait TestSupport
{
protected View $view;

/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
*/
protected function tearDown(): void
{
parent::tearDown();
$this->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' => '/',
'forceCopy' => true,
],
'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',
],
],
],
);

Yii::$app->assetManager->hashCallback = static function (string $path) {
return '16b8de20';
};
}

/**
* 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);
}
}
Loading