Skip to content
This repository has been archived by the owner on Nov 8, 2020. It is now read-only.

Commit

Permalink
处理升级后单元测试的变动
Browse files Browse the repository at this point in the history
  • Loading branch information
baijunyao committed Jul 11, 2017
1 parent 888b7b2 commit 059c47c
Show file tree
Hide file tree
Showing 4 changed files with 227 additions and 79 deletions.
8 changes: 6 additions & 2 deletions composer.json
Expand Up @@ -28,7 +28,8 @@
"maatwebsite/excel": "^2.1",
"barryvdh/laravel-dompdf": "^0.7.0",
"phpoffice/phpword": "^0.13.0",
"jaeger/querylist": "^3.2"
"jaeger/querylist": "^3.2",
"laravel/browser-kit-testing": "^2.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
Expand All @@ -55,7 +56,10 @@
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
],
"psr-4": {
"Tests\\": "tests/"
}
},
"scripts": {
"post-root-package-install": [
Expand Down
161 changes: 104 additions & 57 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

112 changes: 112 additions & 0 deletions tests/BrowserKitTestCase.php
@@ -0,0 +1,112 @@
<?php

use App\User;
use Illuminate\Contracts\Console\Kernel;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

abstract class BrowserKitTestCase extends BaseTestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';

/**
* header 头内容
*
* @var array
*/
protected $headersArray = [];

/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';

$app->make(Kernel::class)->bootstrap();

return $app;
}

/**
* api 登录
*
* @param int $userId
*/
protected function apiLogin($userId = 91)
{
$user = User::find($userId);
$token = JWTAuth::fromUser($user);
JWTAuth::setToken($token);
Auth::attempt(['name' => $user->name, 'password' => $user->password]);
$this->setApiToken($token);
}

/**
* 设置api的版本
*
* @param int $version
*/
public function setApiVersion($version = 1)
{
$this->headersArray['Accept'] = 'application/vnd.internal.v' .$version. '+json';
}

/**
* 设置token
*
* @param $token
*/
public function setApiToken($token)
{
$this->headersArray['HTTP_Authorization'] = "Bearer $token";
}

/**
* 封装一个便捷的api请求post方法
*
* @param $route
* @param $data
* @param $version
* @param int $userId
* @return $this
*/
public function apiPostWithHeaders($route, $data, $version, $userId = 91)
{
$this->setApiVersion($version);
$this->apiLogin($userId);
$this->post($route, $data, $this->headersArray);
return $this;
}

/**
* web 登录
*
* @param int $userId
* @return $this
*/
public function webLogin($userId = 1)
{
$user = User::find($userId);
$this->be($user);
// 获取用户数据
$userArray = $user->toArray();
// 存储到session中
if ($userArray['type'] === 1 || $userArray['type'] === 4) {
// 顾问或者分总经理 session存储城市id和城市名
$consultant = \App\Models\User::find($userArray['id'])->consultant;
$city = $consultant->city;
$userArray['city_id'] = $city->id;
$userArray['city_name'] = $city->name;
}
$this->withSession(['user' => $userArray]);
return $this;
}

}
25 changes: 5 additions & 20 deletions tests/TestCase.php
@@ -1,25 +1,10 @@
<?php

abstract class TestCase extends Illuminate\Foundation\Testing\TestCase
{
/**
* The base URL to use while testing the application.
*
* @var string
*/
protected $baseUrl = 'http://localhost';

/**
* Creates the application.
*
* @return \Illuminate\Foundation\Application
*/
public function createApplication()
{
$app = require __DIR__.'/../bootstrap/app.php';
namespace Tests;

$app->make(Illuminate\Contracts\Console\Kernel::class)->bootstrap();
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

return $app;
}
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
}

0 comments on commit 059c47c

Please sign in to comment.