Skip to content

Commit

Permalink
Refactor tests and add TestSupport trait.
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw committed Feb 28, 2024
1 parent e1c13d1 commit 70abbf2
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 5 deletions.
7 changes: 5 additions & 2 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 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
71 changes: 71 additions & 0 deletions tests/Support/TestSupport.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?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' => '/',
],
'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',
],
],
],
);
}

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

0 comments on commit 70abbf2

Please sign in to comment.