I have a little problem with my unit tests, I don’t know how I can assert the recipe’s likes number after increase/decrease process in my test.
I give you my code below.
<?php
declare(strict_types=1);
namespace AppTestsController;
use AppRepositoryRecipeRepository;
use AppRepositoryUserRepository;
use SymfonyBundleFrameworkBundleTestWebTestCase;
use SymfonyBundleFrameworkBundleKernelBrowser;
class RecipeControllerTest extends WebTestCase
{
private ?KernelBrowser $client = null;
private ?RecipeRepository $recipeRepository = null;
private ?UserRepository $userRepository = null;
public function setUp(): void
{
$this->client = static::createClient();
$this->recipeRepository = static::getContainer()->get(RecipeRepository::class);
$this->userRepository = static::getContainer()->get(UserRepository::class);
$this->urlGenerator = static::getContainer()->get('router.default');
}
/**
* @dataProvider userIdProvider
*/
public function testUserFavorites(int $userId)
{
$testUser = $this->userRepository->find($userId);
/* login testUser */
$this->client->loginUser($testUser);
$recipeId = 1;
/* simulate a user request to get a recipe */
$crawler = $this->client->request('GET', $this->urlGenerator->generate('app_recipe_show', ['id' => $recipeId]));
/* assert that the response is OK (200) */
$this->assertEquals(200, $this->client->getResponse()->getStatusCode());
/* get the recipe from the database */
$recipe = $this->recipeRepository->find($recipeId);
$this->assertStringContainsString($recipe->getName(), $crawler->filter('h1.main-title')->text());
/* if the recipe is in the user's favorites */
$isFavorite = $testUser->getFavorites() !== null && in_array($recipe->getId(), $testUser->getFavorites(), true);
/* submit button name to get favorite form */
$selector = (!$isFavorite) ? 'Add to favorites' : 'Remove from favorites';
/* favorite form */
$form = $crawler->selectButton($selector)->form();
/* submit the form */
$this->client->submit($form);
/* assert code 303 (HTTP_SEE_OTHER) redirection */
$this->assertEquals(303, $this->client->getResponse()->getStatusCode());
/* logout testUser */
$this->client->request('GET', $this->urlGenerator->generate('app_logout'));
}
public function testRecipeLikes()
{
/* TODO */
$recipeId = 1;
$recipe = $this->recipeRepository->find($recipeId);
/* after increase the number of likes with 3 users */
$this->assertEquals(3, $recipe->getLikes());
/* after decrease the number of likes with 3 users */
$this->assertEquals(0, $recipe->getLikes());
}
public function userIdProvider()
{
return [
[1], [2], [3]
];
}
}
Quite simply, I just want assert the recipe’s likes number after increase/decrease process. If I first run the test the recipe (with id=1) will get 3 likes, and if I run one more time the recipe will get 0 likes. How could I do ? I had the idea to create one test named testRecipeLikes to check the recipe’s likes after process but I don’t know the process increase or deacrease. Thanks for your help ! If you have any comments on my code let me know! I’m a beginner in the discipline.