Skip to content

Commit

Permalink
增加单元测试
Browse files Browse the repository at this point in the history
  • Loading branch information
Yurunsoft committed Jun 25, 2019
1 parent 3729100 commit dc40669
Show file tree
Hide file tree
Showing 18 changed files with 344 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@
/.gitattributes export-ignore
/.gitignore export-ignore
/res export-ignore
/travis export-ignore
10 changes: 8 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
language: php

php:
- 7.1
- '7.1'
- '7.2'
- '7.3'

install:
- php ./travis/install.php -nproc $(nproc) -version-name $(phpenv version-name)

before_script:
- phpenv config-rm xdebug.ini
- composer update

script:
- php test/test.php
- composer test
12 changes: 12 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,24 @@
"psr/simple-cache": "~1.0",
"vlucas/phpdotenv": "~2.5"
},
"require-dev": {
"phpunit/phpunit": "^7"
},
"autoload": {
"psr-4" : {
"Imi\\" : "src/"
},
"files": [
"src/functions.php"
]
},
"autoload-dev": {
"psr-4": {
"Imi\\Test\\": "tests/unit/"
}
},
"scripts": {
"test": "php ./tests/phpunit -c ./tests/phpunit.xml",
"install-test": "php --ri swoole && composer install && composer test"
}
}
2 changes: 1 addition & 1 deletion src/Tool/Tool.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public static function getToolOperation()
* 初始化
* @return void
*/
private static function init()
public static function init()
{
// 跳过初始化的工具
foreach(Config::get('@Imi.skipInitTools') as $tool)
Expand Down
1 change: 0 additions & 1 deletion test/test.php

This file was deleted.

17 changes: 17 additions & 0 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
require dirname(__DIR__) . '/vendor/autoload.php';

use Imi\App;
use Imi\Event\Event;
use Imi\Event\EventParam;
use Imi\Tool\Tool;

Event::on('IMI.INITED', function(EventParam $param){
$param->stopPropagation();
Tool::init();
echo 'imi inited!', PHP_EOL;
}, PHP_INT_MAX);

echo 'init imi...', PHP_EOL;

App::run('Imi\Test');
3 changes: 3 additions & 0 deletions tests/phpunit
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
require dirname(__DIR__) . '/vendor/bin/phpunit';
17 changes: 17 additions & 0 deletions tests/phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
backupStaticAttributes="false"
bootstrap="./bootstrap.php"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Aop">
<directory>unit/Tests</directory>
<file>*.php</file>
</testsuite>
</testsuites>
</phpunit>
36 changes: 36 additions & 0 deletions tests/unit/Aop/Aop/AfterAop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Imi\Test\Aop\Aop;

use Imi\Aop\JoinPoint;
use Imi\Bean\BeanProxy;
use Imi\Bean\BeanFactory;
use Imi\Aop\Annotation\After;
use PHPUnit\Framework\Assert;
use Imi\Aop\Annotation\Aspect;
use Imi\Aop\Annotation\PointCut;

/**
* @Aspect
*/
class AfterAop
{
/**
* @After
* @PointCut(
* allow={
* "Imi\Test\Aop\Classes\TestAfterAopClass::test"
* }
* )
*
* @return void
*/
public function injectAfterAop(JoinPoint $joinPoint)
{
Assert::assertEquals([1], $joinPoint->getArgs());
Assert::assertEquals('test', $joinPoint->getMethod());
Assert::assertEquals(\Imi\Test\Aop\Classes\TestAfterClass::class, BeanFactory::getObjectClass($joinPoint->getTarget()));
Assert::assertEquals(BeanProxy::class, get_class($joinPoint->getThis()));
Assert::assertEquals('after', $joinPoint->getType());
$joinPoint->setArgs([2]);
}
}
36 changes: 36 additions & 0 deletions tests/unit/Aop/Aop/BeforeAop.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
namespace Imi\Test\Aop\Aop;

use Imi\Aop\JoinPoint;
use PHPUnit\Framework\Assert;
use Imi\Aop\Annotation\Aspect;
use Imi\Aop\Annotation\Before;
use Imi\Aop\Annotation\PointCut;
use Imi\Bean\BeanFactory;
use Imi\Bean\BeanProxy;

/**
* @Aspect
*/
class BeforeAop
{
/**
* @Before
* @PointCut(
* allow={
* "Imi\Test\Aop\Classes\TestBeforeClass::test"
* }
* )
*
* @return void
*/
public function injectBefore(JoinPoint $joinPoint)
{
Assert::assertEquals([1], $joinPoint->getArgs());
Assert::assertEquals('test', $joinPoint->getMethod());
Assert::assertEquals(\Imi\Test\Aop\Classes\TestBeforeClass::class, BeanFactory::getObjectClass($joinPoint->getTarget()));
Assert::assertEquals(BeanProxy::class, get_class($joinPoint->getThis()));
Assert::assertEquals('before', $joinPoint->getType());
$joinPoint->setArgs([2]);
}
}
16 changes: 16 additions & 0 deletions tests/unit/Aop/Classes/TestAfterClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Imi\Test\Aop\Classes;

use Imi\Bean\Annotation\Bean;

/**
* @Bean("TestAfterClass")
*/
class TestAfterClass
{
public function test(int $id)
{
return $id;
}

}
16 changes: 16 additions & 0 deletions tests/unit/Aop/Classes/TestBeforeClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php
namespace Imi\Test\Aop\Classes;

use Imi\Bean\Annotation\Bean;

/**
* @Bean("TestBeforeClass")
*/
class TestBeforeClass
{
public function test(int $id)
{
return $id;
}

}
9 changes: 9 additions & 0 deletions tests/unit/BaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php
namespace Imi\Test;

use PHPUnit\Framework\TestCase;

abstract class BaseTest extends TestCase
{

}
14 changes: 14 additions & 0 deletions tests/unit/Main.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
namespace Imi\Test;

use Imi\Main\AppBaseMain;

class Main extends AppBaseMain
{
public function __init()
{
// 这里可以做一些初始化操作,如果需要的话

}

}
32 changes: 32 additions & 0 deletions tests/unit/Tests/AopTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace Imi\Test\Tests;

use Imi\Test\BaseTest;
use Imi\App;

class AopTest extends BaseTest
{
/**
* Aop Before
*
* @return void
*/
public function testBefore()
{
$test = App::getBean('TestBeforeClass');
$result = $test->test(1);
$this->assertEquals(2, $result);
}

/**
* Aop After
*
* @return void
*/
public function testAfter()
{
$test = App::getBean('TestAfterClass');
$result = $test->test(1);
$this->assertEquals(1, $result);
}
}
11 changes: 11 additions & 0 deletions tests/unit/config/config.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
return [
'configs' => [
],
// bean扫描目录
'beanScan' => [
'Imi\Test\Aop',
],
'beans' => [
],
];
94 changes: 94 additions & 0 deletions travis/Args.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* 命令行参数操作类
*/
abstract class Args
{
/**
* 参数存储
* @var string
*/
private static $cliArgs;

/**
* 初始化
*
* @param integer $argBegin 从第几个参数算
* @return void
*/
public static function init($argBegin = 1)
{
static::$cliArgs = [];
$keyName = null;
for($i = $argBegin; $i < $_SERVER['argc']; ++$i)
{
if(isset($_SERVER['argv'][$i][0]) && '-' === $_SERVER['argv'][$i][0])
{
$keyName = substr($_SERVER['argv'][$i], 1);
static::$cliArgs[$keyName] = true;
}
else
{
if(null === $keyName)
{
static::$cliArgs[$_SERVER['argv'][$i]] = true;
}
else
{
static::$cliArgs[$keyName] = $_SERVER['argv'][$i];
$keyName = null;
}
}
}
}

/**
* 指定数据是否存在
* @param string $name
* @return bool
*/
public static function exists($name)
{
if(is_integer($name))
{
return isset($_SERVER['argv'][$name]);
}
else
{
return isset(static::$cliArgs[$name]);
}
}

/**
* 获取值
* @param string $name
* @param mixed $default
* @return mixed
*/
public static function get($name = '', $default = null)
{
if(is_integer($name))
{
$data = $_SERVER['argv'];
}
else
{
$data = &static::$cliArgs;
}
if ('' === $name)
{
// 全部的值
return $data;
}
// 判断指定的值是否存在
else if (isset($data[$name]))
{
return $data[$name];
}
else
{
// 返回默认值
return $default;
}
}
}
21 changes: 21 additions & 0 deletions travis/install.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
require __DIR__ . '/Args.php';

Args::init();
$nproc = Args::get('nproc');
$versionName = Args::get('version-name');

define('OTHER_SWOOLE_VERSION', 'v4.4.0-beta');

if(version_compare(PHP_VERSION, '7.1', '>='))
{
$version = OTHER_SWOOLE_VERSION;

`wget https://github.com/swoole/swoole-src/archive/{$version}.tar.gz -O swoole.tar.gz && mkdir -p swoole && tar -xf swoole.tar.gz -C swoole --strip-components=1 && rm swoole.tar.gz && cd swoole && phpize && ./configure && make -j{$nproc} && make install && cd -`;

`echo "extension = swoole.so" >> ~/.phpenv/versions/{$versionName}/etc/php.ini`;
}
else
{
echo 'Skip Swoole', PHP_EOL;
}

0 comments on commit dc40669

Please sign in to comment.