Testy integracyjne wykonują realne żądania na API. Trzeba zatem zadbać o odpowiednie skonfigurowanie środowiska do testów. Kwestia danych to jedno z podstawowych zagadnień z którym należy sobie poradzić. Do tematu można podejść na kilka sposobów, ale sprowadzają się one do dwóch kategorii: baza danych albo implementacja w pamięci. Dowiedz się, które z nich sprawdzi się w twoim projekcie.
Testy API: połączenie z bazą danych czy implementacja w pamięci
1. Testy API
połączenie z bazą danych czy implementacja w pamięci
Krystian Żądło (koddlo.pl)
2. ● Testy API, czyli co…
● Testowanie kodu wymagającego źródła danych
● Baza danych dla testów
○ Zady i Walety
○ Przykład w PHP
● Implementacja w pamięci
○ Zady i Walety
○ Przykład w PHP
● Porównanie rozwiązań
● Podsumowanie
● Bonus
● Q&A
Spis treści
7. Zady i Walety
+ najbardziej zbliżone do produkcji
+ krótki czas implementacji
+ łatwy development
+ łatwe do wprowadzenia w legacy
- bardzo wolne i zasobożerne
- rzadko uruchamiane
8.
9. final class UserAPITest extends WebTestCase
{
private KernelBrowser $client;
private UserFixture $fixtures;
public function testGivenValidPayloadWhenCreatingUserShouldReturn201(): void {...}
public function testGivenNoParametersWhenListingUsersShouldReturn200WithAllUsers(): void {...}
protected function setUp(): void {...}
}
Przykład w PHP
10. protected function setUp(): void
{
$this→client = static::createClient();
$this→fixtures = $this→client→getContainer()→get(UserFixture::class);
$entityManager = $this→client→getContainer()→get(EntityManagerInterface::class);
$metadata = $entityManager→getMetadataFactory()→getAllMetadata();
$schemaTool = new SchemaTool($entityManager);
$schemaTool→dropSchema($metadata);
$schemaTool→createSchema($metadata);
}
Przykład w PHP
14. Zady i Walety
+ bardzo szybkie
+ często uruchamiane
+ wymusza dobre praktyki
+ łatwo się z nich wycofać
- mniejsze pewność i wiarygodność
- trudny i wolny development
- dużo dodatkowego kodu
- trudne do wprowadzenia w legacy
17. interface UserRepositoryInterface
{
public function save(User $user): void;
}
final class UserRepository implements UserRepositoryInterface
{
public function __construct(private EntityManager $entityManager) {}
public function save(User $user): void
{
$this→entityManager→persist($user);
$this→entityManager→flush();
}
}
Przykład w PHP
18. final class UserInMemoryRepository implements UserRepositoryInterface
{
private array $users = [];
public function save(User $user): void
{
$this→users[$user→getId()→toString()] = $user;
}
public function getOneByEmail(string $email): User
{
foreach ($this→users as $user) {
if ($email === $user→getEmail()→toString()) {
return $user;
}
}
throw new UserNotFoundException();
}
public function findAll(): array
{
return array_values($this→users);
}
}
Przykład w PHP
19. interface ListUsersQueryInterface
{
/** @return User[] */
public function execute(): array;
}
final class ListUsersInMemoryQuery implements ListUsersQueryInterface
{
public function __construct(private UserInMemoryRepository $repository) {}
public function execute(): array
{
return array_map(
fn (User $user) ⇒ new DTO(
$user→getId()→toString(),
$user→getEmail()→toString(),
$user→getName()→toString()
),
$this→repository→findAll()
);
}
}
Przykład w PHP
20. interface UniqueEmailQueryInterface
{
public function execute(string $email, ?string $contextId = null): bool;
}
final class UniqueEmailInMemoryQuery implements UniqueEmailQueryInterface
{
public function __construct(private UserInMemoryRepository $repository) {}
public function execute(string $email, ?string $contextId = null): bool
{
try {
$user = $this→repository→getOneByEmail($email);
} catch (UserNotFoundException) {
return true;
}
return (string) $contextId === $user→getId()→toString();
}
}
Przykład w PHP
21. final class UserAPITest extends WebTestCase
{
private KernelBrowser $client;
private UserFixture $fixtures;
public function testGivenValidPayloadWhenCreatingUserShouldReturn201(): void {...}
public function testGivenNoParametersWhenListingUsersShouldReturn200WithAllUsers(): void {...}
protected function setUp(): void {...}
}
Przykład w PHP
22. protected function setUp(): void
{
$this→client = static::createClient();
$this→fixtures = $this→client→getContainer()→get(UserFixture::class);
}
Przykład w PHP