Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHP]Testy w Laravel / php
trifek
post
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

Ostrzeżenie: (0%)
-----


Witam serdecznie,
Uczę się pisania testów w Laravelu. Jako że nie wiem jak powinny wyglądać "profesjonalnie" - to chciałem dopytać czy to, co robię ma sens (IMG:style_emoticons/default/smile.gif)

1. Test 1 - test sprawdzający czy strona działa poprawnie
  1. class HomeTest extends TestCase
  2. {
  3. public function testHomePageWorkCorrectly()
  4. {
  5. //$this->withoutExceptionHandling();
  6. $response = $this->get('/');
  7.  
  8. $response->assertStatus(200);
  9. }
  10. }



2. Test 2 - test logowania z dobrym i błędnym hasłem
  1. class LoginTest extends TestCase
  2. {
  3. public function testUserCanLoginWithCorrectPassword()
  4. {
  5. //$this->withoutExceptionHandling();
  6. $response = $this->post(route('cms.check_admin_login'), [
  7. 'email' => 'lukpeta@icloud.com',
  8. 'password' => 'passw'
  9. ]);
  10. $response->assertRedirect('/psCMS/');
  11. $this->assertTrue(Auth::check());
  12. }
  13.  
  14. public function testUserCannotLoginWithInCorrectPassword()
  15. {
  16. //$this->withoutExceptionHandling();
  17. $response = $this->post(route('cms.check_admin_login'), [
  18. 'email' => 'lukpeta@icloud.com',
  19. 'password' => 'xxxxx'
  20. ]);
  21. $response->assertRedirect('psCMS/login?error=1');
  22. $this->assertFalse(Auth::check());
  23. }
  24. }



3. Test 3 - test modułu reklam. Dodawanie nowej reklamy, sprawdzanie czy istnieje, pobieranie listy reklam, sprawdzanie czy walidacja pól działa, kasowanie
  1. class AdTest extends TestCase
  2. {
  3. use \Illuminate\Foundation\Testing\WithFaker;
  4.  
  5. public function testCreateAd()
  6. {
  7. $user = User::find(1);
  8. $this->be($user);
  9.  
  10. $ad = factory(Ad::class)->create();
  11. $this->post(route('ads.edit') . '/' . $ad->id, $ad->toArray());
  12. $this->assertDatabaseHas('ads', ['id' => $ad->id]);
  13. }
  14.  
  15. public function testAdExist()
  16. {
  17. $user = User::find(1);
  18. $this->be($user);
  19.  
  20. $ad = factory(Ad::class)->create();
  21. $this->post(route('ads.edit') . '/' . $ad->id, $ad->toArray());
  22. $this->assertDatabaseHas('ads', ['id' => $ad->id]);
  23. }
  24.  
  25.  
  26. public function testGetAdsList()
  27. {
  28. $user = User::find(1);
  29. $this->be($user);
  30.  
  31. $ads = factory(Ad::class)->create();
  32. $response = $this->get(route('ads.index'));
  33. $response->assertSee($ads->title);
  34. }
  35.  
  36. public function testCreateAdMustBeCompleted()
  37. {
  38. $user = User::find(1);
  39. $this->be($user);
  40.  
  41. $response = $this->actingAs($user)->post(route('ads.store'), [
  42. 'title' => null,
  43. 'provincial_id' => null,
  44. 'content' => null,
  45. ]);
  46.  
  47. $response->assertSessionHasErrors(['title', 'provincial_id', 'content']);
  48. }
  49.  
  50. public function testCreateAdFromForm()
  51. {
  52. $user = User::find(1);
  53. $this->be($user);
  54. $faker = Faker\Factory::create('pl_PL');
  55. $title = $faker->sentence($nbWords = 6, $variableNbWords = true);
  56. $response = $this->actingAs($user)->post(route('ads.store'), [
  57. 'title' => $title,
  58. 'content' => $faker->text($maxNbChars = 200),
  59. 'provincial_id' => $faker->numberBetween(1, 8),
  60. ]);
  61.  
  62. $this->assertDatabaseHas('ads', ['title' => $title]);
  63. }
  64.  
  65.  
  66. public function testEditAdFromForm()
  67. {
  68. $this->withExceptionHandling();
  69. $user = User::find(1);
  70. $this->be($user);
  71. $faker = Faker\Factory::create('pl_PL');
  72.  
  73. $ad = factory(Ad::class)->create();
  74. $id = $ad->id;
  75.  
  76. $title = $faker->sentence($nbWords = 6, $variableNbWords = true);
  77. $response = $this->actingAs($user)->post(route('ads.update') . '/' . $id, [
  78. 'title' => $title,
  79. 'content' => $faker->text($maxNbChars = 200),
  80. 'provincial_id' => $faker->numberBetween(1, 8),
  81. 'id' => $id
  82. ]);
  83.  
  84. //dd($response->getContent());
  85.  
  86. $this->assertDatabaseHas('ads', ['title' => $title, 'id' => $id]);
  87. }
  88.  
  89.  
  90. public function testDeleteAd()
  91. {
  92. //$this->withExceptionHandling();
  93. //$this->withoutExceptionHandling();
  94. $user = User::find(1);
  95. $this->be($user);
  96.  
  97. $ad = factory(Ad::class)->create();
  98. $id = $ad->id;
  99.  
  100. $faker = Faker\Factory::create('pl_PL');
  101. $title = $faker->sentence($nbWords = 6, $variableNbWords = true);
  102. $response = $this->actingAs($user)->post(route('ads.destroy'), [
  103. 'title' => $title,
  104. 'content' => $faker->text($maxNbChars = 200),
  105. 'provincial_id' => $faker->numberBetween(1, 8),
  106. //'id'=> $id,
  107. 'id' => [$id]
  108. ]);
  109.  
  110. $this->assertDatabaseMissing('ads', ['id' => $ad->id]);
  111. }
  112.  
  113.  
  114. }



Czy takie testy są poprawne? Czy raczej powinny wyglądać inaczej?(IMG:style_emoticons/default/smile.gif)
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.12.2025 - 17:44