Level 1
Extremely Misteriously.
Just added.
$this->refreshApplication();
in every test case function and its working...
1 like
My test class
class RestaurantControllerTest extends TestCase
{
use RefreshDatabase;
public function test_can_list_restaurants_vendor()
{
$user = User::factory()->create();
//login user account with email [email protected]
$this->actingAs($user);
$response = $this->get('api/restaurant-management/restaurants');
$response->assertForbidden();
$user->assignRole(Role::create(['name' => 'Admin']));
$response = $this->get('api/restaurant-management/restaurants');
$response->assertOk();
$response->assertJsonStructure([
'draw',
'recordsTotal',
'recordsFiltered',
'data',
]);
}
public function test_has_data_restaurant_vendor()
{
$dataTable = new RestaurantsDataTable();
$dataTable->render('pages.vendor.restaurants.index');
$data = $dataTable->ajax();
$this->assertNotEmpty($data);
// has a restaurant at any index of this array
$restaurant = Tenant::first();
foreach ($data->getData()->data as $item) {
if ($item->id === $restaurant->id) {
$this->assertEquals($restaurant->email, $item->email);
}
}
}
public function test_can_show_restaurant_vendor()
{
$user = User::factory()->create();
$this->actingAs($user);
Tenant::create([
'id' => 1,
'name' => 'restaurant1',
'email' => '[email protected]',
]);
$response = $this->get('api/restaurant-management/restaurants/1');
$response->assertForbidden();
$user->assignRole(Role::create(['name' => 'Admin']));
$response = $this->get('api/restaurant-management/restaurants/1');
$response->assertOk();
$response->assertJsonStructure([
'data' => [
'id',
'name',
'email',
'domain',
'created_at',
],
]);
$response->assertJson([
'data' => [
'id' => 1,
'name' => 'restaurant1',
'email' => '[email protected]',
],
]);
}
public function test_can_create_restaurant_vendor()
{
$user = User::factory()->create();
$this->actingAs($user);
$id = rand(1000, 100000);
$response = $this->post('api/restaurant-management/restaurants', [
'name' => 'restaurant1',
'email' => '[email protected]',
'domain' => 'r1.rms360.com',
]);
$response->assertForbidden();
$user->assignRole(Role::createOrFirst(['name' => 'Admin']));
$response = $this->post('api/restaurant-management/restaurants', [
'name' => 'restaurant1',
'email' => "{$id}[email protected]",
'domain' => 'r1.rms360.com',
]);
$response->assertOk();
$response->assertJsonStructure([
'status',
'message',
'data' => [
'id',
'name',
'email',
'domain',
'created_at',
'subscription',
'payment_methods',
'default_payment_method_id',
'invoices',
'stats',
],
]);
self::assertTrue($response->json()['status'] === 'success');
self::assertTrue($response->json()['message'] === __('vendor/restaurant.create.success'));
self::assertTrue($response->json()['data']['name'] === 'restaurant1');
self::assertTrue($response->json()['data']['email'] === "{$id}[email protected]");
self::assertTrue($response->json()['data']['domain'] === 'r1.rms360.com');
}
public function test_can_update_restaurant_vendor()
{
$user = User::factory()->create();
$this->actingAs($user);
$id = rand(1000, 100000);
Tenant::create([
'id' => $id,
'name' => 'restaurant1',
'email' => "{$id}[email protected]",
]);
$response = $this->put("api/restaurant-management/restaurants/{$id}", [
'name' => 'restaurant1',
'domain' => 'r2.rms360.com',
]);
$response->assertForbidden();
$user->assignRole(Role::createOrFirst(['name' => 'Admin']));
$response = $this->put("api/restaurant-management/restaurants/{$id}", [
'name' => 'restaurant1',
'domain' => 'r2.rms360.com',
]);
$response->assertOk();
$response->assertJsonStructure([
'status',
'message',
'data' => [
'id',
'name',
'email',
'domain',
'created_at',
'subscription',
'payment_methods',
'default_payment_method_id',
'invoices',
'stats',
],
]);
self::assertTrue($response->json()['status'] === 'success');
self::assertTrue($response->json()['message'] === __('vendor/restaurant.update.success'));
self::assertTrue($response->json()['data']['name'] === 'restaurant1');
self::assertTrue($response->json()['data']['email'] === "{$id}[email protected]");
self::assertTrue($response->json()['data']['domain'] === 'r2.rms360.com');
}
public function test_can_delete_restaurant_vendor()
{
$user = User::factory()->create();
$this->actingAs($user);
$id = rand(1000, 100000);
Tenant::create([
'id' => $id,
'name' => 'restaurant1',
'email' => "{$id}[email protected]",
]);
$response = $this->delete("api/restaurant-management/restaurants/{$id}");
$response->assertForbidden();
$user->assignRole(Role::createOrFirst(['name' => 'Admin']));
$response = $this->delete("api/restaurant-management/restaurants/{$id}");
$response->assertOk();
self::assertTrue($response->json()['status'] === 'success');
self::assertTrue($response->json()['message'] === __('vendor/restaurant.delete.success'));
}
}
It works fine when I run individual test cases in this class. But update and delete test cases fails when I run the whole class.
it gives this error.
Illuminate\Database\QueryException: SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect DOUBLE value: 'eb88c08f-7ca7-4989-9b4f-4b647de28e9f' (Connection: mysql, SQL: update `tenants` set `id` = 62620, `email` = [email protected], `data` = {"updated_at":"2024-01-01 10:42:51","created_at":"2024-01-01 10:42:51","name":"restaurant1","tenancy_db_name":"tenant62620"}, `tenants`.`updated_at` = 2024-01-01 10:42:51 where `id` = 62620)
PHP Unit config
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" backupGlobals="false" bootstrap="vendor/autoload.php"
colors="true" processIsolation="false" stopOnFailure="false"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" cacheDirectory=".phpunit.cache"
backupStaticProperties="false">
<coverage/>
<testsuites>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
</testsuites>
<php>
<env name="APP_ENV" value="testing"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="QUEUE_DRIVER" value="sync"/>
<env name="DB_CONNECTION" value="mysql"/>
<env name="DB_DATABASE" value="rms360_test"/>
<!-- <env name="DB_USERNAME" value="root"/>-->
<!-- <env name="DB_TEST_PASSWORD" value=""/>-->
<env name="DB_HOST_HOST" value="127.0.0.1"/>
<env name="DB_PORT" value="3305"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
running on MySQL 8.0.30. Same issue on github actions.
Extremely Misteriously.
Just added.
$this->refreshApplication();
in every test case function and its working...
Please or to participate in this conversation.