Skip to content

Commit

Permalink
添加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
kuangjy2 committed Oct 20, 2017
1 parent d89eb27 commit 6b185ee
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 2 deletions.
13 changes: 13 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="utf-8"?>
<phpunit bootstrap="./tests/bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="false">
<testsuites>
<testsuite name="yii2-phpsms Test Suite">
<directory>./tests</directory>
</testsuite>
</testsuites>
</phpunit>
5 changes: 3 additions & 2 deletions tests/RandomLibTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,23 @@
namespace kuangjy\RandomLib\Tests;

use kuangjy\RandomLib\RandomLib;
use PHPUnit\Framework\TestCase;
use SecurityLib\Strength;


class RandomLibTest extends TestCase
{

public function testNewComponent()
{
$component = new RandomLib();
$component = new RandomLib(['strength' => Strength::MEDIUM]);
$this->assertInstanceOf(RandomLib::class, $component);

return $component;
}

/**
* @depends testNewComponent
* @param $component
*/
public function testGenerateNumber($component)
{
Expand Down
63 changes: 63 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace kuangjy\RandomLib\Tests;

use yii\di\Container;
use yii\helpers\ArrayHelper;
use Yii;

/**
* This is the base class for all yii framework unit tests.
*/
abstract class TestCase extends \PHPUnit\Framework\TestCase
{
/**
* 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' => dirname(__DIR__) . '/vendor',
], $config));
}

protected function mockWebApplication($config = [], $appClass = '\yii\web\Application')
{
new $appClass(ArrayHelper::merge([
'id' => 'testapp',
'basePath' => __DIR__,
'vendorPath' => dirname(__DIR__) . '/vendor',
'components' => [
'request' => [
'cookieValidationKey' => 'wefJDF8sfdsfSDefwqdxj9oq',
'scriptFile' => __DIR__ . '/index.php',
'scriptUrl' => '/index.php',
],
],
], $config));
}

/**
* Destroys application in Yii::$app by setting it to null.
*/
protected function destroyApplication()
{
Yii::$app = null;
Yii::$container = new Container();
}
}
12 changes: 12 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

// ensure we get report on all possible php errors
error_reporting(-1);

define('YII_ENABLE_ERROR_HANDLER', false);
define('YII_DEBUG', true);
$_SERVER['SCRIPT_NAME'] = '/' . __DIR__;
$_SERVER['SCRIPT_FILENAME'] = __FILE__;

require_once(__DIR__ . '/../vendor/autoload.php');
require_once(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');

0 comments on commit 6b185ee

Please sign in to comment.