Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [Symfony] Testowanie aplikacji
Malinaa
post
Post #1





Grupa: Zarejestrowani
Postów: 578
Pomógł: 6
Dołączył: 21.07.2008

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


Cześć,
chciałbym wykonać testowanie rejestracji i logowania w Symfony.

Symfony -> w katalogu tests/Controller/ utworzyłem plik SecurityControllerTest.php (test logowania),
gdzie chciałbym uzupełnic formularz danymi i zalogować użytkownika, następnie sprawdzić, czy jest zalogowany.

Napisałem taki kod, ale ciągle sypie errorami (coś tu rzeźbię)?

  1. <?php
  2.  
  3. declare(strict_types=1);
  4.  
  5. namespace App\Tests\Controller;
  6.  
  7. use App\Repository\UserRepository;
  8. use App\Tests\Integration\AbstractIntegrationWebTestCase;
  9.  
  10. class SecurityControllerTest extends AbstractIntegrationWebTestCase
  11. {
  12. public const TEST_USER_LOGIN = 'test@domain.com';
  13. public const TEST_USER_PASSWORD = 'test123';
  14.  
  15. public function testLogin(): void
  16. {
  17. $client = static::createClient();
  18. $crawler = $client->request('GET', '/login');
  19.  
  20. // Uzupelnij formularz danymi i zaloguj
  21. $dataLogin = [
  22. 'email' => self::TEST_USER_LOGIN,
  23. 'password' => self::TEST_USER_PASSWORD,
  24. //'submit' => 'Login',
  25. ];
  26.  
  27. //$formLogin = $crawler->filter('form')->form();
  28. $formLogin = $crawler->selectButton('Login')->form();
  29.  
  30. //$formLogin['email']->setValue(self::TEST_USER_LOGIN);
  31. //$formLogin['password']->setValue(self::TEST_USER_PASSWORD);
  32. //$formLogin['submit']->setValue('Login');
  33. // OFF $formLogin['_csrf_token"'] = 'It is generated automatically!';
  34.  
  35. //$client->submit($formLogin, $dataLogin);
  36.  
  37. //dd($client->getRequest()->request);
  38.  
  39. // Sprawdz, czy zalogowany -> sprawdza tylko czy email jest w bazie (IMG:style_emoticons/default/questionmark.gif) ?
  40. $repositoryUser = static::getContainer()->get(UserRepository::class);
  41. $testUser = $repositoryUser->findOneBy(['email' => self::TEST_USER_LOGIN]);
  42. $client->loginUser($testUser);
  43.  
  44. //dd($testUser);
  45.  
  46. $this->assertResponseIsSuccessful();
  47. }
  48. }


Mam prośbę o pomoc w wykonaniu testowania.

Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Malinaa
post
Post #2





Grupa: Zarejestrowani
Postów: 578
Pomógł: 6
Dołączył: 21.07.2008

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


Sprawy

1. Tak, wypełniam pole login, hasło, zaznaczam Agree terms i loguje się.

2. Przesłałem wygenerowany kod HTML, ponieważ tu wszystko widać i jest też token csrf

  1. <input type="hidden" id="_csrf_token" name="_csrf_token" value="abc..." />


bynajmniej było widać, teraz to chyba forum nie poradziło sobie z formatowaniem i "brzydko" wyświetla w PHP plaintext

dodatkowo przesyłam kod Twig

{%- block form -%}
{{ form_start(form) }}
{{- form_widget(form) -}}
{{ form_end(form) }}
{%- endblock -%}

w kontrolerze jest dodane:
$error = $authenticationUtils->getLastAuthenticationError();
i nie pokazuje, aby coś tu było nie tak,
chyba, że jest tu mowa o dodaniu w kontrolerze testowym?

3. Oprócz F - fail i liczby jest komunikat

Deprecated: App\Shared\Uuid\Uuid implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /var/www/src/Shared/Uuid/Uuid.php on line 11

Przesyłam ponownie klasę testu po wprowadzeniu zmian, o których pisałeś chociaż była już wcześniej podana

  1. class SecurityControllerTest extends AbstractIntegrationWebTestCase
  2. {
  3. public const TEST_USER_LOGIN = 'test@email.com';
  4. public const TEST_USER_PASSWORD = 'Test123';
  5.  
  6. public function testLogin(): void
  7. {
  8. $client = static::createClient();
  9. $crawler = $client->request('GET', '/login');
  10.  
  11. $formLogin = $crawler->selectButton('Login')->form();
  12.  
  13. $formLogin['email'] = self::TEST_USER_LOGIN;
  14. $formLogin['password'] = self::TEST_USER_PASSWORD;
  15. $client->submit($formLogin);
  16.  
  17. $router = static::getContainer()->get(UrlGeneratorInterface::class);
  18. $this->assertResponseRedirects($router->generate('app_main'));
  19. }
  20. }


Powyższa klasa sypie błędem FAILURES... Nie działa.

Ten post edytował Malinaa 26.09.2023, 08:34:24
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: 30.12.2025 - 14:32