Skip to content

Commit

Permalink
More clean code in tests. (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Nov 20, 2023
1 parent 1f86d55 commit cda7841
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 22 deletions.
39 changes: 20 additions & 19 deletions tests/AssetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,43 @@
use Yii2\Extensions\Selectize\Selectize;
use yii\web\AssetBundle;
use yii\web\JqueryAsset;
use yii\web\View;

final class AssetTest extends TestCase
{
public function testSelectizeAssetSimpleDependency(): void
public function setup(): void
{
parent::setUp();
$this->mockApplication();

$view = Yii::$app->getView();
Selectize::$counter = 0;

$this->view = Yii::$app->getView();
}

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

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

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

$this->assertArrayHasKey(SelectizeAsset::class, $view->assetBundles);
$this->assertArrayHasKey(BootstrapAsset::class, $view->assetBundles);
$this->assertArrayHasKey(BootstrapPluginAsset::class, $view->assetBundles);
$this->assertArrayHasKey(JqueryAsset::class, $view->assetBundles);
$this->assertArrayHasKey(SelectizeAsset::class, $this->view->assetBundles);
$this->assertArrayHasKey(BootstrapAsset::class, $this->view->assetBundles);
$this->assertArrayHasKey(BootstrapPluginAsset::class, $this->view->assetBundles);
$this->assertArrayHasKey(JqueryAsset::class, $this->view->assetBundles);
}

public function testFileSelectizeAssetRegister(): void
{
$this->mockApplication();

$view = new View();

$this->assertEmpty($view->assetBundles);
$this->assertEmpty($this->view->assetBundles);

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

$this->assertCount(4, $view->assetBundles);
$this->assertInstanceOf(AssetBundle::class, $view->assetBundles[SelectizeAsset::class]);
$this->assertCount(4, $this->view->assetBundles);
$this->assertInstanceOf(AssetBundle::class, $this->view->assetBundles[SelectizeAsset::class]);

$result = $view->renderFile(
$result = $this->view->renderFile(
__DIR__ . '/Support/main.php',
['widget' => Selectize::widget(['name' => 'tags', 'id' => 'tests-id'])],
);
Expand Down
3 changes: 0 additions & 3 deletions tests/SelectizeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,9 @@
use Yii;
use Yii2\Extensions\Selectize\Selectize;
use Yii2\Extensions\Selectize\Tests\Support\SelectizeModel;
use yii\web\View;

final class SelectizeTest extends TestCase
{
private View $view;

public function setup(): void
{
parent::setUp();
Expand Down
5 changes: 5 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@
use Yii;
use yii\di\Container;
use yii\web\Application;
use yii\web\View;

/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
protected View $view;

/**
* Clean up after test.
* By default the application created with [[mockApplication]] will be destroyed.
Expand Down Expand Up @@ -58,5 +61,7 @@ protected function destroyApplication()
Yii::$app = null;
Yii::$container = new Container();
Assert::removeFilesFromDirectory(__DIR__ . '/Support/runtime');

unset($this->view);
}
}

0 comments on commit cda7841

Please sign in to comment.