yii2-bootstrap5/tests/TestCase.php

133 lines
3.5 KiB
PHP
Raw Normal View History

<?php
namespace yiiunit\extensions\bootstrap5;
use Yii;
use yii\base\Action;
use yii\base\Module;
use yii\di\Container;
use yii\helpers\ArrayHelper;
use yii\web\Controller;
/**
* This is the base class for all yii framework unit tests.
*/
class TestCase extends \PHPUnit\Framework\TestCase
{
/**
* Asserting two strings equality ignoring line endings
*
* @param string $expected
* @param string $actual
*/
public function assertEqualsWithoutLE(string $expected, string $actual)
{
$expected = str_replace("\r\n", "\n", $expected);
$actual = str_replace("\r\n", "\n", $actual);
$this->assertEquals($expected, $actual);
}
/**
* Asserting two strings equality ignoring line endings
*
* @param string $needle
* @param string $haystack
*/
public function assertContainsWithoutLE(string $needle, string $haystack)
{
$needle = str_replace("\r\n", "\n", $needle);
$haystack = str_replace("\r\n", "\n", $haystack);
2024-03-28 16:27:15 +08:00
$this->assertStringContainsString($needle, $haystack);
}
/**
* {@inheritDoc}
*/
2024-03-28 16:27:15 +08:00
protected function setUp(): void
{
parent::setUp();
$this->mockWebApplication();
}
/**
* {@inheritDoc}
*/
2024-03-28 16:27:15 +08:00
protected function tearDown(): void
{
parent::tearDown();
$this->destroyApplication();
}
/**
* @param array $config
* @param string $appClass
*/
protected function mockWebApplication(array $config = [], string $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
'language' => 'en-US',
'aliases' => [
'@bower' => '@vendor/bower-asset',
'@npm' => '@vendor/npm-asset',
],
'components' => [
'i18n' => [
'translations' => [
'yii/bootstrap5*' => [
'class' => 'yii\i18n\GettextMessageSource',
'sourceLanguage' => 'en-US',
'basePath' => '@yii/bootstrap5/messages',
],
],
],
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
]
]
], $config));
}
/**
* Mocks controller action with parameters
*
* @param string $controllerId
* @param string $actionID
* @param string|null $moduleID
* @param array $params
*/
protected function mockAction(string $controllerId, string $actionID, string $moduleID = null, array $params = [])
{
Yii::$app->controller = $controller = new Controller($controllerId, Yii::$app);
$controller->actionParams = $params;
$controller->action = new Action($actionID, $controller);
if ($moduleID !== null) {
$controller->module = new Module($moduleID);
}
}
/**
* Removes controller
*/
protected function removeMockedAction()
{
Yii::$app->controller = null;
}
/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
Yii::$app = null;
Yii::$container = new Container();
}
}