diff --git a/composer.json b/composer.json index 05e9cda..7cc3446 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -55,7 +56,10 @@ "autoload-dev": { "classmap": [ "tests/TestCase.php" - ] + ], + "psr-4": { + "Tests\\": "tests/" + } }, "scripts": { "post-root-package-install": [ diff --git a/composer.lock b/composer.lock index 5785b68..fb14022 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "content-hash": "72e932f0bd2510f3a3a7a426b324c79f", + "content-hash": "1b8422572078dc12d33b98331ae03c9f", "packages": [ { "name": "barryvdh/laravel-debugbar", @@ -1248,6 +1248,53 @@ ], "time": "2016-12-07T09:37:55+00:00" }, + { + "name": "laravel/browser-kit-testing", + "version": "v2.0.0", + "source": { + "type": "git", + "url": "https://github.com/laravel/browser-kit-testing.git", + "reference": "23f1a7eefcbca0797305b236600aa5426c9528f2" + }, + "dist": { + "type": "zip", + "url": "https://files.phpcomposer.com/files/laravel/browser-kit-testing/23f1a7eefcbca0797305b236600aa5426c9528f2.zip", + "reference": "23f1a7eefcbca0797305b236600aa5426c9528f2", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/css-selector": "~3.1", + "symfony/dom-crawler": "~3.1" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "2.0-dev" + } + }, + "autoload": { + "psr-4": { + "Laravel\\BrowserKitTesting\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Taylor Otwell", + "email": "taylor@laravel.com" + } + ], + "description": "Provides backwards compatibility for BrowserKit testing in Laravel 5.4.", + "keywords": [ + "laravel", + "testing" + ], + "time": "2017-05-26T12:46:48+00:00" + }, { "name": "laravel/framework", "version": "v5.4.28", @@ -3242,6 +3289,62 @@ "homepage": "https://symfony.com", "time": "2017-07-05T13:02:37+00:00" }, + { + "name": "symfony/dom-crawler", + "version": "v3.1.10", + "source": { + "type": "git", + "url": "https://github.com/symfony/dom-crawler.git", + "reference": "7eede2a901a19928494194f7d1815a77b9a473a0" + }, + "dist": { + "type": "zip", + "url": "https://files.phpcomposer.com/files/symfony/dom-crawler/7eede2a901a19928494194f7d1815a77b9a473a0.zip", + "reference": "7eede2a901a19928494194f7d1815a77b9a473a0", + "shasum": "" + }, + "require": { + "php": ">=5.5.9", + "symfony/polyfill-mbstring": "~1.0" + }, + "require-dev": { + "symfony/css-selector": "~2.8|~3.0" + }, + "suggest": { + "symfony/css-selector": "" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "3.1-dev" + } + }, + "autoload": { + "psr-4": { + "Symfony\\Component\\DomCrawler\\": "" + }, + "exclude-from-classmap": [ + "/Tests/" + ] + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Fabien Potencier", + "email": "fabien@symfony.com" + }, + { + "name": "Symfony Community", + "homepage": "https://symfony.com/contributors" + } + ], + "description": "Symfony DomCrawler Component", + "homepage": "https://symfony.com", + "time": "2017-01-21T17:13:55+00:00" + }, { "name": "symfony/event-dispatcher", "version": "v3.3.4", @@ -5869,62 +5972,6 @@ "homepage": "https://symfony.com", "time": "2017-06-02T09:51:43+00:00" }, - { - "name": "symfony/dom-crawler", - "version": "v3.1.10", - "source": { - "type": "git", - "url": "https://github.com/symfony/dom-crawler.git", - "reference": "7eede2a901a19928494194f7d1815a77b9a473a0" - }, - "dist": { - "type": "zip", - "url": "https://files.phpcomposer.com/files/symfony/dom-crawler/7eede2a901a19928494194f7d1815a77b9a473a0.zip", - "reference": "7eede2a901a19928494194f7d1815a77b9a473a0", - "shasum": "" - }, - "require": { - "php": ">=5.5.9", - "symfony/polyfill-mbstring": "~1.0" - }, - "require-dev": { - "symfony/css-selector": "~2.8|~3.0" - }, - "suggest": { - "symfony/css-selector": "" - }, - "type": "library", - "extra": { - "branch-alias": { - "dev-master": "3.1-dev" - } - }, - "autoload": { - "psr-4": { - "Symfony\\Component\\DomCrawler\\": "" - }, - "exclude-from-classmap": [ - "/Tests/" - ] - }, - "notification-url": "https://packagist.org/downloads/", - "license": [ - "MIT" - ], - "authors": [ - { - "name": "Fabien Potencier", - "email": "fabien@symfony.com" - }, - { - "name": "Symfony Community", - "homepage": "https://symfony.com/contributors" - } - ], - "description": "Symfony DomCrawler Component", - "homepage": "https://symfony.com", - "time": "2017-01-21T17:13:55+00:00" - }, { "name": "symfony/yaml", "version": "v3.3.4", diff --git a/tests/BrowserKitTestCase.php b/tests/BrowserKitTestCase.php new file mode 100644 index 0000000..b3c9e51 --- /dev/null +++ b/tests/BrowserKitTestCase.php @@ -0,0 +1,112 @@ +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; + } + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 8208edc..2932d4a 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -1,25 +1,10 @@ make(Illuminate\Contracts\Console\Kernel::class)->bootstrap(); +use Illuminate\Foundation\Testing\TestCase as BaseTestCase; - return $app; - } +abstract class TestCase extends BaseTestCase +{ + use CreatesApplication; }