CarTypeTest.php 5.7 KB

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