| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152 |
- <?php
- namespace Tests\Unit;
- use App\Authority;
- use App\City;
- use App\Role;
- use App\User;
- use Illuminate\Support\Facades\DB;
- use Tests\TestCase;
- use Illuminate\Foundation\Testing\WithFaker;
- use Illuminate\Foundation\Testing\RefreshDatabase;
- class CityTest extends TestCase
- {
- public function testUserMark(){
- $userMark=factory(User::class)->create();
- $this->assertNotEmpty($userMark->id);
- return $userMark;
- }
- public function testRole(){
- $role=Role::create([
- 'name'=>'测试admin'
- ]);
- $this->assertNotEmpty($role->id);
- $authorities= Authority::get();
- foreach ($authorities as $authority){
- DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
- }
- return $role;
- }
- /**
- * @depends testRole
- */
- public function testUser(Role $role){
- $user=factory(User::class)->create();
- DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
- $user->touchToken();
- $this->assertNotEmpty($user->id);
- return $user;
- }
- public function testAddCity(){
- $city=new City([
- 'name'=>'测试',
- 'province_id'=>1,
- ]);
- $result=$city->save();
- $this->assertTrue($result);
- $this->assertNotEmpty($city);
- return $city;
- }
- /**
- * @depends testAddCity
- * @depends testUser
- * @depends testUserMark
- */
- public function testIndex(City $city,User $user,User $userMark){
- $response=$this->actingAs($user)->get('maintenance/city');
- $response->assertOk();
- $responseMark=$this->actingAs($userMark)->get('maintenance/city');
- $responseMark->assertStatus(302)->assertRedirect('/');
- $city=City::paginate(50);
- $this->assertNotEmpty($city);
- }
- /**
- * @depends testUser
- * @depends testUserMark
- */
- public function testCreate(User $user,User $userMark){
- $response=$this->actingAs($user)->get('maintenance/city/create');
- $response->assertOk()->assertSee('城市名称');
- $responseMark=$this->actingAs($userMark)->get('maintenance/city/create');
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testUser
- * @depends testUserMark
- * */
- public function testStore(User $user,User $userMark){
- $response=$this->actingAs($user)->post('maintenance/city',['City'=>[
- 'name'=>'测试Two',
- 'province_id'=>1,
- ]]);
- $response->assertStatus(302)->assertRedirect('maintenance/city')->assertSessionHas('successTip');
- $responseMark=$this->actingAs($userMark)->post('maintenance/city',['City'=>[
- 'name'=>'测试Two',
- 'province_id'=>1,
- ]]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- $city=City::where('name','=','测试Two')->first();
- $result=$city->delete();
- $this->assertTrue($result);
- }
- /**
- * @depends testAddCity
- * @depends testUser
- * @depends testUserMark
- */
- public function testEdit(City $city,User $user,User $userMark){
- $response=$this->actingAs($user)->get("maintenance/city/$city->id/edit");
- $response->assertOk()->assertSee('测试');
- $responseMark=$this->actingAs($userMark)->get("maintenance/city/$city->id/edit");
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testAddCity
- * @depends testUser
- * @depends testUserMark
- */
- public function testUpdate(City $city,User $user,User $userMark){
- $response=$this->actingAs($user)->put("maintenance/city/$city->id",['City'=>[
- 'name'=>'测试Update',
- 'province_id'=>1,
- ]]);
- $response->assertStatus(302)->assertRedirect('maintenance/city')->assertSessionHas('successTip');
- $responseMark=$this->actingAs($userMark)->put("maintenance/city/$city->id",['City'=>[
- 'name'=>'测试Update',
- 'province_id'=>1,
- ]]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- }
- /**
- * @depends testAddCity
- * @depends testUser
- * @depends testUserMark
- * @depends testRole
- */
- public function testDestroy(City $city,User $user,User $userMark,Role $role){
- $response=$this->actingAs($user)->json('delete','maintenance/city/'.$city->id,['id'=>$city->id]);
- $response->assertOk()->assertJson(['success'=>true]);
- $responseMark=$this->actingAs($userMark)->json('delete','maintenance/city/'.$city->id,['id'=>$city->id]);
- $responseMark->assertStatus(302)->assertRedirect('/');
- $result=$user->delete();
- $this->assertTrue($result);
- $this->assertNotEmpty($userMark->id);
- $resultUserMark=$userMark->delete();
- $this->assertTrue($resultUserMark);
- $resultRole=$role->delete();
- $this->assertTrue($resultRole);
- DB::table('authority_role')->where('id_role','=',$role->id)->delete();
- DB::table('user_role')->where('id_role','=',$role->id)->delete();
- }
- }
|