This commit is contained in:
2022-03-21 11:16:38 +08:00
commit e89e807c64
1040 changed files with 284164 additions and 0 deletions

161
tests/app/Api/Auth_Test.php Normal file
View File

@@ -0,0 +1,161 @@
<?php
/**
* PhalApi_App\Api\Auth_Test
*
* 针对 ../src/app/Api/Auth.php App\Api\Auth 类的PHPUnit单元测试
*
* @author: dogstar 20191219
*/
namespace tests\App\Api;
use App\Api\Auth;
use PhalApi\Helper\TestRunner;
class PhpUnderControl_AppApiAuth_Test extends \PHPUnit\Framework\TestCase
{
public $appApiAuth;
protected function setUp()
{
parent::setUp();
$this->appApiAuth = new \App\Api\Auth();
}
protected function tearDown()
{
// 输出本次单元测试所执行的SQL语句
// var_dump(\PhalApi\DI()->tracer->getSqls());
// 输出本次单元测试所涉及的追踪埋点
// var_dump(\PhalApi\DI()->tracer->getStack());
}
/**
* @group testGetRules
*/
public function testGetRules()
{
$rs = $this->appApiAuth->getRules();
$this->assertTrue(is_array($rs));
}
/**
* @group testApplyToken
* @expectedException PhalApi\Exception\BadRequestException
*/
public function testApplyTokenFailNotVerify()
{
// 创建app
$domain = new \Base\Domain\Apps();
$domain->addApp('test', 'test_not_verify', '123456');
//Step 1. 构建请求URL
$url = 'service=App.Auth.ApplyToken&app_key=test_not_verify&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';
//Step 2. 执行请求
$rs = TestRunner::go($url);
}
/**
* @group testApplyToken
*/
public function testApplyToken()
{
// 创建app
$domain = new \Base\Domain\Apps();
$domain->addApp('test', 'test', '123456', 0, 0, '', 1);
//Step 1. 构建请求URL
$url = 'service=App.Auth.ApplyToken&app_key=test&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertNotEmpty($rs['access_token']);
return $rs['access_token'];
}
/**
* @depends testApplyToken
*/
public function testRefreshAccessTokenForApp($at) {
//Step 1. 构建请求URL
$url = 'service=App.Auth.RefreshAccessToken&access_token='.$at;
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertNotEmpty($rs['access_token']);
}
/**
* @expectedException \PhalApi\Exception\BadRequestException
*/
public function testApplyTokenWrongKey()
{
//Step 1. 构建请求URL
$url = 'service=App.Auth.ApplyToken&app_key=test_404&app_secret=123456&config_name=phpunit_test&config_value=2020&username=dogstar&sign=B83F9015965B68185E97792EC40FC55D';
//Step 2. 执行请求
$rs = TestRunner::go($url);
}
/**
* @ expectedException \PhalApi\Exception\BadRequestException
*/
public function testUserLogin()
{
//Step 1. 构建请求URL
$url = 'service=App.User.Register&username=test_auth&password=123456';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 1. 构建请求URL
$url = 'service=App.Auth.UserLogin&username=test_auth&password=123456&app_key=test';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertNotEmpty($rs['access_token']);
return $rs['access_token'];
}
public function testAppUserLogin() {
//Step 1. 构建请求URL
$url = 'service=App.Auth.AppUserLogin&username=test_auth&password=123456&app_key=test&app_secret=123456';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertNotEmpty($rs['access_token']);
}
/**
* @depends testUserLogin
*/
//public function testRefreshAccessTokenForUser($at) {
// //Step 1. 构建请求URL
// $url = 'service=App.Auth.RefreshAccessToken&access_token='.$at;
// //Step 2. 执行请求
// $rs = TestRunner::go($url);
// //Step 3. 验证
// $this->assertNotEmpty($rs);
// $this->assertNotEmpty($rs['access_token']);
//}
}

View File

@@ -0,0 +1,56 @@
<?php
namespace App;
use App\Api\Site;
use PhalApi\Helper\TestRunner;
/**
* PhpUnderControl_ApiSite_Test
*
* 针对 App\Api\Site 类的PHPUnit单元测试
*
* @author: dogstar 20170703
*/
class PhpUnderControl_ApiSite_Test extends \PHPUnit_Framework_TestCase
{
public $site;
protected function setUp()
{
parent::setUp();
$this->site = new Site();
}
protected function tearDown()
{
}
/**
* @group testGetRules
*/
public function testGetRules()
{
$rs = $this->site->getRules();
$this->assertNotEmpty($rs);
}
public function testIndex()
{
$this->assertTrue(true);
return;
//Step 1. 构建请求URL
$url = 'service=App.Site.Index&username=dogstar';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertArrayHasKey('title', $rs);
}
}

124
tests/app/Api/User_Test.php Normal file
View File

@@ -0,0 +1,124 @@
<?php
/**
* PhalApi_App\Api\User_Test
*
* 针对 ../src/app/Api/User.php App\Api\User 类的PHPUnit单元测试
*
* @author: dogstar 20191219
*/
namespace tests\App\Api;
use App\Api\User;
use PhalApi\Helper\TestRunner;
class PhpUnderControl_AppApiUser_Test extends \PHPUnit\Framework\TestCase
{
public $appApiUser;
protected function setUp()
{
parent::setUp();
$this->appApiUser = new \App\Api\User();
}
protected function tearDown()
{
// 输出本次单元测试所执行的SQL语句
// var_dump(\PhalApi\DI()->tracer->getSqls());
// 输出本次单元测试所涉及的追踪埋点
// var_dump(\PhalApi\DI()->tracer->getStack());
}
/**
* @group testGetRules
*/
public function testGetRules()
{
$rs = $this->appApiUser->getRules();
$this->assertTrue(is_array($rs));
}
/**
* @group testRegister
*/
public function testRegister()
{
//Step 1. 构建请求URL
$url = 'service=App.User.Register&username=test_app&password=123456';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertTrue($rs['is_register']);
$this->assertGreaterThan(0, $rs['uid']);
}
/**
* @group testAlterPass
*/
public function testAlterPass()
{
// \PhalApi\DI()->context->setUid(1);
//Step 1. 构建请求URL
$url = 'service=App.User.AlterPass&username=test_app&password=123456&new_password=654321&_uid=1&access_token=123';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertTrue($rs['is_alter']);
//Step 1. 构建请求URL
$url = 'service=App.User.AlterPass&username=test_app&password=654321&new_password=123456&_uid=1&access_token=123';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertTrue($rs['is_alter']);
}
/**
* @group testProfile
*/
public function testUserProfile()
{
//Step 1. 构建请求URL
$url = 'service=App.User.UserProfile&username=test_app&access_token=123';
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertNotEmpty($rs['profile']);
$this->assertNotEmpty($rs['profile']['id']);
}
/**
* @group testUpdateProfile
*/
public function testUpdateProfile()
{
// \PhalApi\DI()->context->setUid(1);
//Step 1. 构建请求URL
$url = 'service=App.User.UpdateProfile&access_token=123&username=test_app&_uid=1&mobile='.time();
//Step 2. 执行请求
$rs = TestRunner::go($url);
//Step 3. 验证
$this->assertNotEmpty($rs);
$this->assertTrue($rs['is_update']);
}
}

View File

@@ -0,0 +1,222 @@
<?php
/**
* PhalApi_App\Common\Filter_Test
*
* 针对 ../src/app/Common/Filter.php App\Common\Filter 类的PHPUnit单元测试
*
* @author: dogstar 20191231
*/
namespace tests\App\Common;
use App\Common\Filter;
class PhpUnderControl_AppCommonFilter_Test extends \PHPUnit\Framework\TestCase
{
public $appCommonFilter;
protected function setUp()
{
parent::setUp();
$this->appCommonFilter = new \App\Common\Filter();
\PhalApi\DI()->debug = false;
}
protected function tearDown()
{
\PhalApi\DI()->debug = true;
// 输出本次单元测试所执行的SQL语句
// var_dump(\PhalApi\DI()->tracer->getSqls());
// 输出本次单元测试所涉及的追踪埋点
// var_dump(\PhalApi\DI()->tracer->getStack());
}
/**
* @group testCheck
* @expectedException PhalApi\Exception\BadRequestException
*/
public function testCheckMissAT()
{
$rs = $this->appCommonFilter->check();
}
public function testCheckOK()
{
$time = time();
$config = \PhalApi\DI()->config;
$token = array(
"iss" => "phalapi_pro", // 该JWT的签发者
"aud" => "app", // 接收该JWT的一方
"sub" => 'test', // 该JWT所面向的用户
"uid" => 1,
"iat" => $time, // 在什么时候签发的
"exp" => $time + $config->get('app.jwt.exp', 86400), // 什么时候过期这里是一个Unix时间戳
);
$key = $config->get('app.jwt.key');
$jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');
$data = array(
'access_token' => $jwt,
's' => 'App.HelloWorld.Say',
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
$this->assertEquals('test', \PhalApi\DI()->context->getAppKey());
$this->assertEquals(1, \PhalApi\DI()->context->getUid());
}
/**
* @group testCheck
* @expectedException PhalApi\Exception\BadRequestException
*/
public function testCheckWrongAT()
{
$data = array(
'access_token' => 'ghjkghjkghj',
's' => 'App.HelloWorld.Say',
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
}
/**
* @group testCheck
* @expectedException PhalApi\Exception\BadRequestException
*/
public function testCheckExpireOut()
{
$time = time() - 86400; // 过期
$config = \PhalApi\DI()->config;
$token = array(
"iss" => "phalapi_pro", // 该JWT的签发者
"aud" => "app", // 接收该JWT的一方
"sub" => 'test', // 该JWT所面向的用户
"uid" => 1,
"iat" => $time, // 在什么时候签发的
"exp" => $time,
);
$key = $config->get('app.jwt.key');
$jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');
$data = array(
'access_token' => $jwt,
's' => 'App.HelloWorld.Say',
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
}
/**
* @group testCheck
* @expectedException PhalApi\Exception\BadRequestException
*/
public function testCheckAdmin()
{
$time = time();
$config = \PhalApi\DI()->config;
$token = array(
"iss" => "phalapi_pro", // 该JWT的签发者
"aud" => "app", // 接收该JWT的一方
"sub" => 'test', // 该JWT所面向的用户
"uid" => 1,
"iat" => $time, // 在什么时候签发的
"exp" => $time + $config->get('app.jwt.exp', 86400), // 什么时候过期这里是一个Unix时间戳
);
$key = $config->get('app.jwt.key');
$jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');
$data = array(
'access_token' => $jwt,
's' => 'Admin.HelloWorld.Say', // 管理员接口
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
}
/**
* @group testCheck
* @ expectedException PhalApi\Exception\BadRequestException
*/
public function testCheckNoAppRights()
{
$domain = new \Base\Domain\Rights();
$domain->removeRights('test', 'App.HelloWorld.SayNo', 1);
$time = time();
$config = \PhalApi\DI()->config;
$token = array(
"iss" => "phalapi_pro", // 该JWT的签发者
"aud" => "app", // 接收该JWT的一方
"sub" => 'test', // 该JWT所面向的用户
"uid" => 1,
"iat" => $time, // 在什么时候签发的
"exp" => $time + 100,
);
$key = $config->get('app.jwt.key');
$jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');
$data = array(
'access_token' => $jwt,
's' => 'App.HelloWorld.SayNo', // 此接口无权限
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
}
public function testCheckYesAppRights()
{
$domain = new \Base\Domain\Rights();
$domain->assignRights('test', 'App.HelloWorld.SayNo', 1);
$time = time();
$config = \PhalApi\DI()->config;
$token = array(
"iss" => "phalapi_pro", // 该JWT的签发者
"aud" => "app", // 接收该JWT的一方
"sub" => 'test', // 该JWT所面向的用户
"uid" => 1,
"iat" => $time, // 在什么时候签发的
"exp" => $time + 100,
);
$key = $config->get('app.jwt.key');
$jwt = \lmxdawn\jwt\JWT::encode($token, $key, 'HS256');
$data = array(
'access_token' => $jwt,
's' => 'App.HelloWorld.SayNo', // 此接口重新又有权限
);
\PhalApi\DI()->request = new \PhalApi\Request($data);
$this->appCommonFilter->check();
$this->assertTrue(true);
}
}