? ??? ??? Laravel? ?? ???: ??? ?? ??(?? ??)? ?? ?????. ??? ??? ???? ??? ? ??? ????. .
??? ?? ??? ???
?????? ?? ???? ??? ? ?? ??? ?? ???? ?? ???? ???? ??? ?????. ?????? ???. ? ???? Admin
? Front
??????? ???? ?? ??? ???? ????? ??? ???? ?????. Admin
和Front
來分離管理后臺的測試和前端頁面的測試。
現(xiàn)在讓我們專注于管理后臺carousel
的CRUD功能測試。在test/Feature
目錄里添加CarouselFeatureTest ?
類。
<?php namespace Tests\Feature\Admin\Carousels; use Tests\TestCase; class CarouselFeatureTest extends TestCase { /** @test */ public function it_can_show_the_create_carousel_page() { $employee = factory(User::class)->create(); ????????$this ????????????->actingAs($employee,?'admin') ????????????->get(route('admin.carousel.create')) ????????????->assertStatus(200) ????????????->assertSee('Title') ????????????->assertSee('Subtitle') ????????????->assertSee('Link') ????????????->assertSee('Link?Text') ????????????->assertSee('Image'); ????} }
我們來分析一下上面的代碼。
我們需要
->actingAs()
方法來通過用戶認證中間件并且模擬admin
用戶(如果項目中沒有進行使用認證看守器則不需要這一步)。然后我們通過
route()
取出了創(chuàng)建carousel
的頁面。斷言響應的HTTP狀態(tài)碼為
200
。最后斷言會在頁面上看到的文本值。
運行phpunit
看看會發(fā)生什么。
PHPUnit?6.5.7?by?Sebastian?Bergmann?and?contributors. E???????????????????????????????????????????????????????????????????1?/?1?(100%) Time:?920?ms,?Memory:?26.00MB There?was?1?error: 1)?Tests\Feature\Admin\Carousels\CarouselFeatureTest::it_can_show_the_create_carousel_page InvalidArgumentException:?Route?[admin.carousel.create]?not?defined.
出錯就對了。我們還沒有在web.php
這個路由文件中定義路由,所以將會出現(xiàn)上面的錯誤。讓我們定義這個路由。
<?php Route::namespace('Admin')->group(function?()?{ ????Route::resource('carousel',?'Carousels\CarouselController'); });
分析:
在我的
app/Http/Controllers
目錄中還有其他目錄歸置文件和文件夾。我有Admin
,Front
和Auth
這幾個目錄。在
Admin
這個命名空間中還有Carousels
文件夾,在這個文件夾中是CarouselController.php
文件。
在終端中運行中運行如下命令來創(chuàng)建控制器
php?artisan?make:controller?--resource?Admin/Carousels/CarouselController
定義路由、創(chuàng)建好Controller之后,再次運行phpunit
PHPUnit?6.5.7?by?Sebastian?Bergmann?and?contributors. F???????????????????????????????????????????????????????????????????1?/?1?(100%) Time:?987?ms,?Memory:?28.00MB There?was?1?failure: 1)?Tests\Feature\Admin\Carousels\CarouselFeatureTest::it_can_show_the_create_carousel_page Failed?asserting?that?''?contains?"Title".
起作用了!路由錯誤消失了不過我們遇到了一個新的錯誤,通過新錯誤讓我們想到應該是測試用例服務在響應的UI頁面上找到Title
這個單詞。Hmm 好吧,這是應為我們沒有在create
方法中返回視圖,讓我們加上返回視圖的代碼。
<?php namespace App\Http\Controllers\Admin\Carousels; use App\Http\Controllers\Controller; class CarouselController extends Controller { /** * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function create() { return view('admin.carousels.create'); } }
視圖文件位于resource/views/admin/carousels/create.blade.php
@extends('layouts.admin.app') @section('content') <!-- Main content --> ????<section class="content"> ????????@include('layouts.errors-and-messages') ????????<p class="box"> ????????????<p class="box-body"> ????????????????<form action="{{ route('admin.carousel.store') }}" method="post" enctype="multipart/form-data"> ????????????????????{{?csrf_field()?}} ????????????????????<p class="form-group"> ????????????????????????<label for="title">Title</label> ????????????????????????<input type="text" name="title" id="title" class="form-control" placeholder="Title" value="{{ old('title') }}"> ????????????????????</p> ????????????????????<p class="form-group"> ????????????????????????<label for="image">Image</label> ????????????????????????<input type="file" name="image" id="image" class="form-control"> ????????????????????</p> ????????????????????<p class="form-group"> ????????????????????????<label for="link">Link</label> ????????????????????????<p class="input-group"> ????????????????????????????<span class="input-group-addon">http://</span> ????????????????????????????<input type="text" name="link" id="link" class="form-control" placeholder="www.example.com" value="{{ old('link') }}"> ????????????????????????</p> ????????????????????</p> ????????????????????<p class="btn-group"> ????????????????????????<a href="{{ route('admin.carousel.index') }}" class="btn btn-default btn-sm">Back</a> ????????????????????????<button type="submit" class="btn btn-primary btn-sm">Create</button> ????????????????????</p> ????????????????</form> ????????????</p> ????????</p> ????</section> ????<!-- /.content --> @endsection
視圖文件夾中并沒有admin
和carousels
文件夾,所以你需要自己創(chuàng)建它們。
創(chuàng)建好blade視圖文件后再次運行phpunit
???git:?phpunit?--filter=CarouselFeatureTest::it_can_show_the_create_carousel_page PHPUnit?6.5.7?by?Sebastian?Bergmann?and?contributors. .???????????????????????????????????????????????????????????????????1?/?1?(100%) Time:?810?ms,?Memory:?28.00MB OK?(1?test,?6?assertions)
Nice,看起來非常好。
現(xiàn)在,如果有人搞亂了你的blade模板,你會馬上知道因為這個測試會執(zhí)行失敗。到Github里去檢查一下到底是誰搞亂模板文件,蛤!
通過POST數(shù)據(jù)創(chuàng)建carousel
現(xiàn)在讓我們測試一下通過頁面里的表單是否能夠創(chuàng)建carousel
數(shù)據(jù)。
要創(chuàng)建carousel
別忘了先寫測試,沒有捷徑。
<?php namespace Tests\Feature\Admin\Carousels; use Tests\TestCase; class CarouselFeatureTest extends TestCase { /** @test */ public function it_can_create_the_carousel() { $file = UploadedFile::fake()->create('image.jpg'); ????????$data?=?[ ????????????'title'?=>?$this->faker->word, ????????????'link'?=>?$this->faker->url, ????????????'image'?=>?$file, ????????]; ?????? ????????$employee?=?factory(User::class)->create(); ?????? ????????$this ????????????->actingAs($employee,?'admin') ????????????->post(route('admin.carousel.store'),?$data) ????????????->assertStatus(302) ????????????->assertRedirect(route('admin.carousel.index')) ????????????->assertSessionHas('message',?'Create?carousel?successful!'); ????} ?? ????/**?@test?*/ ????public?function?it_can_show_the_create_carousel_page() ????{ ???????$employee?=?factory(User::class)->create(); ????????$this ????????????->actingAs($employee,?'admin') ????????????->get(route('admin.carousel.create')) ????????????->assertStatus(200) ????????????->assertSee('Title') ????????????->assertSee('Subtitle') ????????????->assertSee('Link') ????????????->assertSee('Link?Text') ????????????->assertSee('Image'); ????} }
分析:
我們斷言在創(chuàng)建成功后會重定向到carousel列表頁。
我們還斷言成功設置了
Create carousel successful!
這個Flash信息
這個測試會執(zhí)行失敗,因為store
()方法現(xiàn)在還空著,讓我們用下面的代碼填充它:
<?php namespace App\Http\Controllers\Admin\Carousels; use App\Http\Controllers\Controller; use App\Shop\Carousels\Exceptions\CarouselNotFoundException; use App\Shop\Carousels\Exceptions\CreateCarouselErrorException; use App\Shop\Carousels\Exceptions\UpdateCarouselErrorException; use App\Shop\Carousels\Repositories\CarouselRepository; use App\Shop\Carousels\Repositories\CarouselRepositoryInterface; use App\Shop\Carousels\Requests\CreateCarouselRequest; use App\Shop\Carousels\Requests\UpdateCarouselRequest; use Illuminate\Http\UploadedFile; class CarouselController extends Controller { /** * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function create() { return view('admin.carousels.create'); } /** * @param CreateCarouselRequest $request * @return \Illuminate\Http\RedirectResponse */ public function store(CreateCarouselRequest $request) { try { $data = $request->except('_token'); ????????????if?($request->hasFile('image')?&&?$request->file('image')?instanceof?UploadedFile)?{ ????????????????$data['src']?=?$request->file('image')->store('carousels',?['disk'?=>?'public']); ????????????} ???????????? ????????????$carouselRepo?=?new?CarouselRepository(new?Carousel); ????????????$carouselRepo->createCarousel($data); ?????????? ????????????$request->session()->flash('message',?'Create?carousel?successful!'); ????????????return?redirect()->route('admin.carousel.index'); ????????}?catch?(CreateCarouselErrorException?$e)?{ ????????????$request->session()->flash('error',?$e->getMessage()); ????????????return?redirect()->back()->withInput(); ????????} ????} }
然后運行phpunit
???
? CRUD ?? ???? ???????. test/Feature
????? CarouselFeatureTest
???? ?????.
???git:?phpunit?--filter=CarouselFeatureTest::it_can_create_the_carousel?????????? PHPUnit?6.5.7?by?Sebastian?Bergmann?and?contributors. .???????????????????????????????????????????????????????????????????1?/?1?(100%) Time:?993?ms,?Memory:?28.00MB OK?(1?test,?5?assertions)? ??? ??? ?????.
- ??? ?? ????? ????
->actingAs()
???? ?????. > admin ???(?????? ?? ??? ???? ?? ?? ? ??? ???? ????). - ???
route()
? ??carousel
? ??? ???? ?????. - ??? HTTP ?? ???
200
?? ?????. - ?? ??? ???? ??? ??? ????.
phpunit
? ???? ?? ?? ????? ?????. rrreee ???? ?????. ??? ?? web.php
? ??? ???? ??? ??? ?? ?? ??? ?????. ? ??? ??? ?????. rrreee
-
?app/Http/Controllers
????? ????. ??? ??? ???? ?? ???????.Admin
,Front
?Auth
????? ????. - #????#
Admin
????????Carousels
??? ??, ? ????CarouselController? ????. php
??. #????#
? ????? phpunit
#????#rrreee#????# ?????! ??? ??? ????? ??? ??? ???? ??? ?? ???? ?? UI ????? ??
??? ??? ????? ??????. ? ????. create
????? ?? ???? ??? ?????. ?? ???? ??? ??? ?????. #????#rrreee#????#? ??? resource/views/admin/carousels/create.blade.php
? ????.#????#rrreee#????#? ???? ?? ?? admin
? carousels
??? ???? ?? ???? ???. #????##????#???? ? ??? ??? ? ?? phpunit
? ??? ???. #????#rrreee#????#???, ?? ?? ????. #????##????#?? ???? ???? ???? ????? ???? ????? ?? ? ? ????. Github? ?? ?? ??? ??? ???? ??? ???. #????##????#POST ???? ?? ??? ??#????##????#?? ???? ??? ?? ???
???? ??? ? ??? ???? ?????. #????##????# ???
? ???? ?? ???? ???? ?? ?? ???. ???? ????. #????#rrreee#????#??: #????#- #????#??? ??? ?? ???? ????? ??? ? ????? ???? ?????. #????#
- #????# ??
??? ??? ??!
??? ???? ????? ?????? ????? #????#
store
() ???? ??? ?? ?? ??? ? ???? ?????. ?? ??? ???????. #????#rrreee#????#?? ?? phpunit code>? ?????. #????#rrreee#????#?? ???? ???? ??? ? ?? ?? ?? ???? ???? ??? ??? ???. #????##????# ?? ?? ??: #????##????##????#Nginx ??? ???? ?????? nginx ??? ??? ? ??? ???? ?#????##????##????##????# Laravel? ??? ??? ?? ??(??)#????##????##????## ???? #laravel5.5 ??????? ? ? ???? ???? ??(?? ??) #????##????##????##????##????#
? ??? Laravel? ??? ?? ???: ??? ?? ??(?? ??)? ?? ?????. ??? ??? PHP ??? ????? ?? ?? ??? ?????!

? AI ??

Undress AI Tool
??? ???? ??

Undresser.AI Undress
???? ?? ??? ??? ?? AI ?? ?

AI Clothes Remover
???? ?? ???? ??? AI ?????.

Clothoff.io
AI ? ???

Video Face Swap
??? ??? AI ?? ?? ??? ???? ?? ???? ??? ?? ????!

?? ??

??? ??

???++7.3.1
???? ?? ?? ?? ???

SublimeText3 ??? ??
??? ??, ???? ?? ????.

???? 13.0.1 ???
??? PHP ?? ?? ??

???? CS6
??? ? ?? ??

SublimeText3 Mac ??
? ??? ?? ?? ?????(SublimeText3)

PHP?? ?? ??? ???? ? ?? ?? ??? ????. 1. php.ini? ?? ??? ??; 2. ? ?? (? : Apache? Setenv ?? nginx? FastCGI_Param)? ??????. 3. PHP ?????? putenv () ??? ??????. ? ??? Php.ini? ????? ??? ???? ??? ???? ? ?? ??? ?? ???? ????? ???? Putenv ()? ?? ??? ?????. ?? ???? ?? ?? (? : php.ini ?? ? ?? ??)? ???? ????. ?? ?? ??? ??? ?? ??? ????? ???? ?? ????.

Laravel? ?? ??? ?? ?? ??? ?? ?? ??? ???? ??? ??????. ?? ???? ?? ??? ????? ? ???? I/O ?? ? ?? ?? ??? ???? ???? ??? ?? ? ????. 1. ?? ????? ?? ? ? ???????? ??? ????? ?? ???? ??????. 2. ??? ? ??? ?? ? ? PhPartisAnconfig? ?? ???????. 3. ?? ??? ??? ??? ???? ?? ?? ?? ???? ???? ????. 4. ?? ?? ??? ???? ?? ??? ??? .env ??? ???? ?? ???????.

PHP ????? ?? ??? ??? ? ??? ??? CI (Continuous Integration) ????? ???? ? ????. 1. DockerFile? ???? ?? ???, ?? ??, ??? ?? ? ?? ??? ???? PHP ??? ?????. 2. Gitlabci? ?? CI/CD ??? ???? .gitlab-ci.yml ??? ?? ??, ??? ? ?? ??? ???? ?? ??, ??? ? ??? ?????. 3. PHPUNIT? ?? ??? ??? ??? ???? ?? ?? ? ???? ???? ????????. 4. Kubernetes? ?? ?? ?? ??? ???? ?? .yaml ??? ?? ?? ??? ?????. 5. Dockerfile ??? ? ??? ??? ??????

??? ?? ??? PHP ???? ?? ?? ??? ???? ?? ???????. RBAC (Role-Based Access Control) ??? ?? ???, ?? ? ??? ???? ??? ?? ?? ? ??? ?????. ?? ???? ??? ?????. 1. ???, ?? ? ??? ? ???? user_roles ? role_permissions? 3 ?? ?? ???; 2. $ user-> can ( 'edit_post')? ?? ???? ?? ?? ??? ?????. 3. ??? ???? ??? ??????. 4. ?? ??? ???? ?? ?? ?? ? ??? ? ???? ???? ?? ??? ? ?? ??? ?????. 5. ??? ??? ?? ?? ???? ?? ???? "??"? ??????.

Laravel? eloquentscopes? ?? ??? ??? ??? ?????? ?? ?? ??? ????? ?????. 1. ?? ??? ???? ???? ???? ???? Post :: published (); 2. ??? ??? ?? ??? ???? ???? ?? ??? ?? ?? ?? ??? ???? ???? ??? ?????? ??? ???? ???????. 3. ????? ?? ?? ?? ??? ??? ?? ?? ??? ?? ? ? ??? ?? ? ? ?? ?? ??? ?????. 4. ?? ??? ? ??? ?? ???? ? ??? ? ?? ??, ?? ??, ?? ???? ? ?? ?????????.

CreateAhelpers.phpfileInapp/helperswithCustOmFunctionsikeFormatPrice, isactiveroute, andisAdmin.2.addTheFileTothe "??"sectionOfcomposer.jsonUnderAutoLoad.3.runcomposerDump-AUTOLOADTOMAKETHINGTICTIONSGLOBELYAVAILABLE.4.USETHEHELPERFUNCUNTION

?? ?? ?? : ?? ????? PHP? ?? Error_Log ()? ??? ? ????. ????? ???? ??? ?? ??? ?????? ???? ?? ??? ? ?? ??? ???? ??? ?? ???, ??, ?? ? ?? ? ?? ?? ??? ???? ??? ??????. 2. ??? ?? ?? : ??? ??? ??? ??? ? ??? ?? ??? ??? ?? ??? ??? ??????? ??????. MySQL/PostgreSQL? ???? ??? ? ???? ??????. Elasticsearch Kibana? ? ???/? ???? ?????. ???, ??? ?? ? ??? ? ?? ??? ?? ??????. 3. ?? ? ?? ????? : ??, ???, ?? ? ??? ??? ??????. Kibana? ?? ????? PHP ??? ?? ?? ?????? ???? ???? ?????? ???? ??? ? ?? ??? ??? ? ????.

USEMOCKERYFORCUSTOMDENCENTICESBYSETEXPINTIONSWITHSHOULDRECEIVE ()
