2021-07-09 11:53:24 +02:00
|
|
|
<?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);
|
2021-07-09 11:53:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2024-03-28 16:27:15 +08:00
|
|
|
protected function setUp(): void
|
2021-07-09 11:53:24 +02:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->mockWebApplication();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritDoc}
|
|
|
|
*/
|
2024-03-28 16:27:15 +08:00
|
|
|
protected function tearDown(): void
|
2021-07-09 11:53:24 +02:00
|
|
|
{
|
|
|
|
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',
|
2022-01-05 11:19:41 +01:00
|
|
|
'language' => 'en-US',
|
2021-07-09 11:53:24 +02:00
|
|
|
'aliases' => [
|
|
|
|
'@bower' => '@vendor/bower-asset',
|
|
|
|
'@npm' => '@vendor/npm-asset',
|
|
|
|
],
|
|
|
|
'components' => [
|
2022-01-05 11:19:41 +01:00
|
|
|
'i18n' => [
|
|
|
|
'translations' => [
|
|
|
|
'yii/bootstrap5*' => [
|
|
|
|
'class' => 'yii\i18n\GettextMessageSource',
|
|
|
|
'sourceLanguage' => 'en-US',
|
|
|
|
'basePath' => '@yii/bootstrap5/messages',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
2021-07-09 11:53:24 +02:00
|
|
|
'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();
|
|
|
|
}
|
|
|
|
}
|