CarTypeTest.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. <?php
  2. namespace Tests\Unit;
  3. use App\Authority;
  4. use App\CarType;
  5. use App\Role;
  6. use App\User;
  7. use Illuminate\Support\Facades\DB;
  8. use Tests\TestCase;
  9. use Illuminate\Foundation\Testing\WithFaker;
  10. use Illuminate\Foundation\Testing\RefreshDatabase;
  11. class CarTypeTest extends TestCase
  12. {
  13. public function testUserMark(){
  14. $userMark=factory(User::class)->create();
  15. $this->assertNotEmpty($userMark->id);
  16. return $userMark;
  17. }
  18. public function testRole(){
  19. $role=Role::create([
  20. 'name'=>'测试admin'
  21. ]);
  22. $this->assertNotEmpty($role->id);
  23. $authorities= Authority::get();
  24. foreach ($authorities as $authority){
  25. DB::table('authority_role')->insert(['id_authority'=>$authority->id,'id_role'=>$role->id]);
  26. }
  27. return $role;
  28. }
  29. /**
  30. * @depends testRole
  31. */
  32. public function testUser(Role $role){
  33. $user=factory(User::class)->create();
  34. DB::table('user_role')->insert(['id_user'=>$user->id,'id_role'=>$role->id]);
  35. $this->assertNotEmpty($user->id);
  36. return $user;
  37. }
  38. public function testAddCarType(){
  39. $carType=new CarType([
  40. 'name'=>'欧马可',
  41. 'model'=>'s3',
  42. 'length'=>5.6,
  43. 'load'=>10,
  44. 'remark'=>'测试车'
  45. ]);
  46. $result=$carType->save();
  47. $this->assertTrue($result);
  48. $this->assertNotEmpty($carType);
  49. return $carType;
  50. }
  51. /**
  52. * @depends testAddCarType
  53. * @depends testUser
  54. * @depends testUserMark
  55. */
  56. public function testIndex(CarType $carType,User $user,User $userMark){
  57. $response=$this->actingAs($user)->get('maintenance/carType');
  58. $response->assertStatus(200)->assertSee('欧马可');
  59. $responseMark=$this->actingAs($userMark)->get('maintenance/carType');
  60. $responseMark->assertStatus(302)->assertRedirect('/');
  61. $carType=CarType::paginate(10);
  62. $this->assertNotEmpty($carType);
  63. }
  64. /**
  65. * @depends testUser
  66. * @depends testUserMark
  67. */
  68. public function testCreate(User $user,User $userMark){
  69. $response=$this->actingAs($user)->get('maintenance/carType/create');
  70. $response->assertOk()->assertSee('载重');
  71. $responseMark=$this->actingAs($userMark)->get('maintenance/carType/create');
  72. $responseMark->assertStatus(302)->assertRedirect('/');
  73. }
  74. /**
  75. * @depends testUser
  76. * @depends testUserMark
  77. */
  78. public function testStore(User $user,User $userMark){
  79. $response=$this->actingAs($user)->post('maintenance/carType',['CarType'=>[
  80. 'name'=>'吉安',
  81. 'model'=>'35',
  82. 'length'=>3.5,
  83. 'load'=>8,
  84. 'remark'=>'测试添加'
  85. ]]);
  86. $response->assertStatus(302)->assertRedirect('maintenance/carType')->assertSessionHas('successTip');
  87. $responseMark=$this->actingAs($userMark)->post('maintenance/carType',['CarType'=>[
  88. 'name'=>'吉安',
  89. 'model'=>'35',
  90. 'length'=>3.5,
  91. 'load'=>8,
  92. 'remark'=>'测试添加'
  93. ]]);
  94. $responseMark->assertStatus(302)->assertRedirect('/');
  95. $carType=CarType::where('name','=','吉安')->first();
  96. $result=$carType->delete();
  97. $this->assertTrue($result);
  98. }
  99. /**
  100. * @depends testAddCarType
  101. * @depends testUser
  102. * @depends testUserMark
  103. */
  104. public function testEdit(CarType $carType,User $user,User $userMark){
  105. $response=$this->actingAs($user)->get("maintenance/carType/$carType->id/edit");
  106. $response->assertOk()->assertSee('欧马可');
  107. $responseMark=$this->actingAs($userMark)->get("maintenance/carType/$carType->id/edit");
  108. $responseMark->assertStatus(302)->assertRedirect('/');
  109. }
  110. /**
  111. * @depends testAddCarType
  112. * @depends testUser
  113. * @depends testUserMark
  114. */
  115. public function testUpdate(CarType $carType,User $user,User $userMark){
  116. $response=$this->actingAs($user)->put("maintenance/carType/$carType->id",['CarType'=>[
  117. 'name'=>'东风',
  118. 'model'=>'q7',
  119. 'length'=>6.7,
  120. 'load'=>18,
  121. 'remark'=>'测试添加'
  122. ]]);
  123. $response->assertStatus(302)->assertRedirect('maintenance/carType')->assertSessionHas('successTip');
  124. $responseMark=$this->actingAs($userMark)->put("maintenance/carType/$carType->id",['CarType'=>[
  125. 'name'=>'东风',
  126. 'model'=>'q7',
  127. 'length'=>6.7,
  128. 'load'=>18,
  129. 'remark'=>'测试添加'
  130. ]]);
  131. $responseMark->assertStatus(302)->assertRedirect('/');
  132. }
  133. /**
  134. * @depends testAddCarType
  135. * @depends testUser
  136. * @depends testUserMark
  137. * @depends testRole
  138. */
  139. public function testDestroy(CarType $carType,User $user,User $userMark,Role $role){
  140. $response=$this->actingAs($user)->json('delete','maintenance/carType/'.$carType->id,['id'=>$carType->id]);
  141. $response->assertOk();
  142. $responseMark=$this->actingAs($userMark)->json('delete','maintenance/carType/'.$carType->id,['id'=>$carType->id]);
  143. $responseMark->assertStatus(302)->assertRedirect('/');
  144. $response->assertJson(['success'=>true]);
  145. $result=$user->delete();
  146. $this->assertTrue($result);
  147. $this->assertNotEmpty($userMark->id);
  148. $resultUserMark=$userMark->delete();
  149. $this->assertTrue($resultUserMark);
  150. $resultRole=$role->delete();
  151. $this->assertTrue($resultRole);
  152. DB::table('authority_role')->where('id_role','=',$role->id)->delete();
  153. DB::table('user_role')->where('id_role','=',$role->id)->delete();
  154. }
  155. }