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.php
, 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.
public function testProductSoftDelete()
{
some code
return $product_id;
}
/**
* @depends testProductSoftDelete
*/
public function testProductPermanentlyDelete($product_id)
{
code to test permanently deletion of the product with id $product_id.
There is a business logic behind that needs to soft delete first a
product before you permanently delete it.
}
Does the following make sense?
namespace Tests\App\Controllers\Product;
use Tests\DatabaseTestCase;
use Tests\TestRequestsTrait;
/**
* @group Coverage
* @group App.Controllers
* @group App.Controllers.Product
*
* Class ProductControllerTest
*
* @package Tests\App\Controllers\Product
*/
class ProductControllerTest extends DatabaseTestCase
{
use TestRequestsTrait;
public function testSoftDelete()
{
$response = $this->doProductSoftDelete('9171448');
$response
->assertStatus(200)
->assertSeeText('Product sof-deleted successfully');
}
public function testUnlink()
{
$this->doProductSoftDelete('9171448');
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/unlink/9171448');
$response
->assertStatus(200)
->assertSeeText('Product unlinked successfully');
}
}
namespace Tests;
trait TestRequestsTrait
{
/**
* Returns the response
*
* @param $product_id
* @return \Illuminate\Foundation\Testing\TestResponse
*/
protected function doProductSoftDelete($product_id)
{
$response = $this->actingAsSuperAdmin()->delete('/pages/admin/management/product/soft-delete/'.$product_id);
return $response;
}
}
namespace Tests;
use Illuminate\Foundation\Testing\DatabaseTransactions;
abstract class DatabaseTestCase extends TestCase
{
use CreatesApplication;
use DatabaseTransactions;
}