-
-
Notifications
You must be signed in to change notification settings - Fork 100
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RefreshDatabaseTrait is not executed in time #30
Comments
nvm, I'm not even creating two users. public function testGetItemUser(): void
{
$client = static::createClient();
$user = $this->createUser($client, 'test_user_1@example.com', 'Test_password_1!', 'Test User 1');
$client->request('GET', '/api/users/' . $user->getId());
$this->assertResponseStatusCodeSame(401, 'GET item (no auth)');
} |
Isn't it an issue with |
But then why does it work if I remove the |
also, when running the full test the first two tests run true fine but when hitting the third I get an SQL Duplicate entry error for "test_user_1@example.com" but this should not be happening if the reset would have already happend. <?php
namespace App\Tests\Functional;
use App\test\CustomApiTestCase;
use Hautelook\AliceBundle\PhpUnit\RefreshDatabaseTrait;
class UserResourceTest extends CustomApiTestCase
{
use RefreshDatabaseTrait;
// Tests:
// POST (no auth)
public function testPostUser(): void
{
$client = static::createClient();
// POST (no auth)
$this->createUser($client, 'test_user_1@example.com', 'Test_password_1!', 'Test User 1');
$this->assertResponseStatusCodeSame(201, 'POST (no auth)');
}
// Tests:
// GET collection (no auth)
// GET collection (auth)
// GET collection (admin auth)
public function testGetCollectionUser(): void
{
$client = static::createClient();
// GET collection (no auth)
$client->request('GET', '/api/users');
$this->assertResponseStatusCodeSame(401, 'GET collection (no auth)');
// GET collection (auth)
$user = $this->createUser($client, 'test_user_1@example.com', 'Test_password_1!', 'Test User 1');
$token = $this->createAuth($client, $user);
$client->request('GET', '/api/users', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
$this->assertResponseStatusCodeSame(403, 'GET collection (auth)');
// GET collection (admin auth)
$admin = $this->createAdmin($client, 'test_admin_1@example.com', 'Test_password_1!', 'Test Admin 1');
$token = $this->createAuth($client, $admin);
$client->request('GET', '/api/users', [
'headers' => [
'Authorization' => 'Bearer ' . $token,
],
]);
$this->assertResponseStatusCodeSame(200, 'GET collection (admin auth)');
}
// Tests:
// GET item (no auth)
// GET item (auth owne)
// GET item (auth *)
// GET item (admin auth own)
// GET item (admin auth *)
public function testGetItemUser(): void
{
$client = static::createClient();
// GET item (no auth)
$user = $this->createUser($client, 'test_user_1@example.com', 'Test_password_1!', 'Test User 1');
$client->request('GET', '/api/users/' . $user->getId());
$this->assertResponseStatusCodeSame(401, 'GET item (no auth 2)');
$client->request('GET', '/api/users/' . $user->getId());
$this->assertResponseStatusCodeSame(401, 'GET item (no auth 2)');
}
} |
I'm not sure but it looks similar to my problem. When I load fixtures from files (use @renja-g confirm if it's the same in your case. If not @theofidry I'll create a new issue. |
I've fixed my issue. The problem was that the "base test class" and specific "test class" use |
I wonder if there would be a way to detect this and throw an error in this case? |
I don't know exactly the tests flow but is |
Actually it looks like this is already handled? https://github.com/theofidry/AliceBundle/blob/master/src/PhpUnit/RefreshDatabaseTrait.php#L38 So there maybe more to it |
Yes, I saw it! But |
I think this may be related to mixing up Refresh and Reload. The fix is NOT to add what is in Refresh to Reload. The idea of Reload is to populate the database at kernel boot, if there is already fixtures a purge (via delete or truncate) is done before populating the database. So even calling it several times over should be fine (although expensive for no reasons). Where this will get messy is if Reload is mixed up with Refresh. Indeed Refresh populates the DB once and then uses transactions to rollback. But purging with a truncate will be effective and remove the data regardless of the transaction. Calling it twice should also have no effect (besides having nested transactions). I am not sure if this really addresses the original case, but I will close this as I cannot find anything. if you encounter the problem and want me to look into it please provide a small reproducer that I can debug. |
I was trying to use RefreshDatabaseTrait for API testing but for some reason the assert gave me 404.
After a long time of testing, I found out that with this code only the first assert fails.
It feels like the database refresh is not finishing in time so that the first user is deleted
The text was updated successfully, but these errors were encountered: