I am working on Laravel 5.7 and i'd like to ask you a question regarding the PHPUnit testing.
I have a test class, let's say ProductControllerTest, with two methods testProductSoftDelete() and testProductPermanentlyDelete(). I want to use the annotation @depends in the testProductPermanentlyDelete() in order to soft-delete first a product and then get the product id and proceed to permanently deletion test. The problem here is that the DatabaseTransaction trait runs the transactions in every test (method) execution. I need to start the transaction before all the tests of my ProductControllerTest class and then rollback the transaction at the end of all tests. Do you have any ideas? From what i have searched from the web nothing worked properly.
The tests are independent, but there is a business logic behind that needs first to soft-delete a product and the proceed with the permanently deletion action.
public function testProductSoftDelete()
{
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/products/soft-delete/xxxxx-sxxx-x-xxxxxxx-xxxx');
$response ->assertStatus(200);
$product = $response->json('data');
return $product ['id'];
}
/**
* @depends testProductSoftDelete
*/
public function testProductPermanentlyDelete($product_id)
{
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/products/permanently-delete/'.$product_id);
$response
->assertStatus(200)
->assertSeeText('"Product deleted successfully');
}
The second test always fail because of the business logic which checks first if the product is soft-deleted in order to deleted forever. Does make sense to use depends annotations or it is better to create one test with 2 requests ?
Nice idea. I made a similar implementation by using a trait in my tests as the main factory that returns each time the relavant models for the specific tests.
But, the reason that i can't use the depends annotation in my tests it's an annoying issue.