mirror of
https://github.com/yiisoft/yii2.git
synced 2026-03-22 23:17:12 +01:00
* php-cs-fixer: PSR2 rule. * php-cs-fixer: PSR2 rule - fix views. * Travis setup refactoring. * Add php-cs-fixer to travis cs tests. * Fix tests on hhvm-3.12 * improve travis config * composer update * revert composer update * improve travis config * Fix CS. * Extract config to separate classes. * Extract config to separate classes. * Add file header. * Force short array syntax. * binary_operator_spaces fixer * Fix broken tests * cast_spaces fixer * concat_space fixer * dir_constant fixer * ereg_to_preg fixer * function_typehint_space fixer * hash_to_slash_comment fixer * is_null fixer * linebreak_after_opening_tag fixer * lowercase_cast fixer * magic_constant_casing fixer * modernize_types_casting fixer * native_function_casing fixer * new_with_braces fixer * no_alias_functions fixer * no_blank_lines_after_class_opening fixer * no_blank_lines_after_phpdoc fixer * no_empty_comment fixer * no_empty_phpdoc fixer * no_empty_statement fixer * no_extra_consecutive_blank_lines fixer * no_leading_import_slash fixer * no_leading_namespace_whitespace fixer * no_mixed_echo_print fixer * no_multiline_whitespace_around_double_arrow fixer * no_multiline_whitespace_before_semicolons fixer * no_php4_constructor fixer * no_short_bool_cast fixer * no_singleline_whitespace_before_semicolons fixer * no_spaces_around_offset fixer * no_trailing_comma_in_list_call fixer * no_trailing_comma_in_singleline_array fixer * no_unneeded_control_parentheses fixer * no_unused_imports fixer * no_useless_return fixer * no_whitespace_before_comma_in_array fixer * no_whitespace_in_blank_line fixer * not_operator_with_successor_space fixer * object_operator_without_whitespace fixer * ordered_imports fixer * php_unit_construct fixer * php_unit_dedicate_assert fixer * php_unit_fqcn_annotation fixer * phpdoc_indent fixer * phpdoc_no_access fixer * phpdoc_no_empty_return fixer * phpdoc_no_package fixer * phpdoc_no_useless_inheritdoc fixer * Fix broken tests * phpdoc_return_self_reference fixer * phpdoc_single_line_var_spacing fixer * phpdoc_single_line_var_spacing fixer * phpdoc_to_comment fixer * phpdoc_trim fixer * phpdoc_var_without_name fixer * psr4 fixer * self_accessor fixer * short_scalar_cast fixer * single_blank_line_before_namespace fixer * single_quote fixer * standardize_not_equals fixer * ternary_operator_spaces fixer * trailing_comma_in_multiline_array fixer * trim_array_spaces fixer * protected_to_private fixer * unary_operator_spaces fixer * whitespace_after_comma_in_array fixer * `parent::setRules()` -> `$this->setRules()` * blank_line_after_opening_tag fixer * Update finder config. * Revert changes for YiiRequirementChecker. * Fix array formatting. * Add missing import. * Fix CS for new code merged from master. * Fix some indentation issues.
201 lines
5.8 KiB
PHP
201 lines
5.8 KiB
PHP
<?php
|
|
/**
|
|
* @link http://www.yiiframework.com/
|
|
* @copyright Copyright (c) 2008 Yii Software LLC
|
|
* @license http://www.yiiframework.com/license/
|
|
*/
|
|
|
|
namespace yiiunit;
|
|
|
|
use Yii;
|
|
use yii\helpers\ArrayHelper;
|
|
|
|
/**
|
|
* This is the base class for all yii framework unit tests.
|
|
*/
|
|
abstract class TestCase extends \PHPUnit\Framework\TestCase
|
|
{
|
|
public static $params;
|
|
|
|
/**
|
|
* Clean up after test case.
|
|
*/
|
|
public static function tearDownAfterClass()
|
|
{
|
|
parent::tearDownAfterClass();
|
|
$logger = Yii::getLogger();
|
|
$logger->flush();
|
|
}
|
|
|
|
/**
|
|
* Returns a test configuration param from /data/config.php
|
|
* @param string $name params name
|
|
* @param mixed $default default value to use when param is not set.
|
|
* @return mixed the value of the configuration param
|
|
*/
|
|
public static function getParam($name, $default = null)
|
|
{
|
|
if (static::$params === null) {
|
|
static::$params = require(__DIR__ . '/data/config.php');
|
|
}
|
|
|
|
return isset(static::$params[$name]) ? static::$params[$name] : $default;
|
|
}
|
|
|
|
/**
|
|
* Clean up after test.
|
|
* By default the application created with [[mockApplication]] will be destroyed.
|
|
*/
|
|
protected function tearDown()
|
|
{
|
|
parent::tearDown();
|
|
$this->destroyApplication();
|
|
}
|
|
|
|
/**
|
|
* Populates Yii::$app with a new application
|
|
* The application will be destroyed on tearDown() automatically.
|
|
* @param array $config The application configuration, if needed
|
|
* @param string $appClass name of the application class to create
|
|
*/
|
|
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
|
|
{
|
|
new $appClass(ArrayHelper::merge([
|
|
'id' => 'testapp',
|
|
'basePath' => __DIR__,
|
|
'vendorPath' => $this->getVendorPath(),
|
|
], $config));
|
|
}
|
|
|
|
protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
|
|
{
|
|
new $appClass(ArrayHelper::merge([
|
|
'id' => 'testapp',
|
|
'basePath' => __DIR__,
|
|
'vendorPath' => $this->getVendorPath(),
|
|
'aliases' => [
|
|
'@bower' => '@vendor/bower-asset',
|
|
'@npm' => '@vendor/npm-asset',
|
|
],
|
|
'components' => [
|
|
'request' => [
|
|
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
|
|
'scriptFile' => __DIR__ . '/index.php',
|
|
'scriptUrl' => '/index.php',
|
|
],
|
|
],
|
|
], $config));
|
|
}
|
|
|
|
protected function getVendorPath()
|
|
{
|
|
$vendor = dirname(dirname(__DIR__)) . '/vendor';
|
|
if (!is_dir($vendor)) {
|
|
$vendor = dirname(dirname(dirname(dirname(__DIR__))));
|
|
}
|
|
return $vendor;
|
|
}
|
|
|
|
/**
|
|
* Destroys application in Yii::$app by setting it to null.
|
|
*/
|
|
protected function destroyApplication()
|
|
{
|
|
if (\Yii::$app && \Yii::$app->has('session', true)) {
|
|
\Yii::$app->session->close();
|
|
}
|
|
\Yii::$app = null;
|
|
}
|
|
|
|
/**
|
|
* Asserting two strings equality ignoring line endings
|
|
* @param string $expected
|
|
* @param string $actual
|
|
*/
|
|
protected function assertEqualsWithoutLE($expected, $actual)
|
|
{
|
|
$expected = str_replace("\r\n", "\n", $expected);
|
|
$actual = str_replace("\r\n", "\n", $actual);
|
|
|
|
$this->assertEquals($expected, $actual);
|
|
}
|
|
|
|
/**
|
|
* Invokes a inaccessible method
|
|
* @param $object
|
|
* @param $method
|
|
* @param array $args
|
|
* @param bool $revoke whether to make method inaccessible after execution
|
|
* @return mixed
|
|
* @since 2.0.11
|
|
*/
|
|
protected function invokeMethod($object, $method, $args = [], $revoke = true)
|
|
{
|
|
$reflection = new \ReflectionClass($object->className());
|
|
$method = $reflection->getMethod($method);
|
|
$method->setAccessible(true);
|
|
$result = $method->invokeArgs($object, $args);
|
|
if ($revoke) {
|
|
$method->setAccessible(false);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sets an inaccessible object property to a designated value
|
|
* @param $object
|
|
* @param $propertyName
|
|
* @param $value
|
|
* @param bool $revoke whether to make property inaccessible after setting
|
|
* @since 2.0.11
|
|
*/
|
|
protected function setInaccessibleProperty($object, $propertyName, $value, $revoke = true)
|
|
{
|
|
$class = new \ReflectionClass($object);
|
|
while (!$class->hasProperty($propertyName)) {
|
|
$class = $class->getParentClass();
|
|
}
|
|
$property = $class->getProperty($propertyName);
|
|
$property->setAccessible(true);
|
|
$property->setValue($object, $value);
|
|
if ($revoke) {
|
|
$property->setAccessible(false);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Gets an inaccessible object property
|
|
* @param $object
|
|
* @param $propertyName
|
|
* @param bool $revoke whether to make property inaccessible after getting
|
|
* @return mixed
|
|
*/
|
|
protected function getInaccessibleProperty($object, $propertyName, $revoke = true)
|
|
{
|
|
$class = new \ReflectionClass($object);
|
|
while (!$class->hasProperty($propertyName)) {
|
|
$class = $class->getParentClass();
|
|
}
|
|
$property = $class->getProperty($propertyName);
|
|
$property->setAccessible(true);
|
|
$result = $property->getValue($object);
|
|
if ($revoke) {
|
|
$property->setAccessible(false);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
|
|
/**
|
|
* Asserts that value is one of expected values
|
|
*
|
|
* @param mixed $actual
|
|
* @param array $expected
|
|
* @param string $message
|
|
*/
|
|
public function assertIsOneOf($actual, array $expected, $message = '')
|
|
{
|
|
self::assertThat($actual, new IsOneOfAssert($expected), $message);
|
|
}
|
|
}
|