CarTypeTest.php 5.6 KB

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