Summer Sale! All accounts are 50% off this week.

boboboy's avatar

Testing not ended and not showing error

Hello everyone, I need help on bellow issue. I have feature testing like this

<?php

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Support\Arr;
use Database\Seeders\ShipperSeeder;
use Database\Seeders\CategorySeeder;
use Database\Seeders\ShipperTypeSeeder;
use Database\Seeders\PaymentMethodSeeder;
use Database\Seeders\ProductSeeder;
use Database\Seeders\VariantSeeder;
use Illuminate\Support\Facades\DB;

class OrderTest extends TestCase
{
    public function prepare(): void
    {
        DB::table('categories')->truncate();

        $this->seed([
            CategorySeeder::class,
            ProductSeeder::class,
            VariantSeeder::class,
            ShipperTypeSeeder::class,
            ShipperSeeder::class,
            PaymentMethodSeeder::class
        ]);
    }

    public function orderRequest(): array
    {
        $order = [
            'voucher_id'            => null,
            'shipper_id'            => 1,
            'amount'                => 40000,
            'discount'              => 0,
            'point'                 => 200,
            'payment_type'          => 'saldo',
            'bank'                  => null,
            'shipment_weight'       => 1,
            'shipment_weight_unit'  => 'kg',
            'shipment_price'        => 18000,
            'shipment_eta'          => '2-3 days',
            'order_items' => [
                [
                    'product_id'    => 1,
                    'variant_id'    => 1,
                    'qty'           => 4,
                    'note'          => null
                ]
            ]
        ];

        return $order;
    }

    public function test_order_process_using_saldo_with_valid_request(): void
    {
        $this->prepare();
        $this->register();
        $userAddress = $this->storeAddress();
        $orderRequest = array_merge(['user_address_id' => $userAddress['id']], $this->orderRequest());
        $order = array_merge(['user_id' => $userAddress['user_id']], Arr::except($orderRequest, 'order_items'));

        $response = $this->json('post', route('order.store'), $orderRequest);

        $response->assertCreated();
        $response->assertJson([
            'rc'        => config('rc.successfully.rc'),
            'message'   => config('rc.successfully.message'),
            'data'      => [
                'order' => $order
            ]
        ]);
        $this->assertDatabaseHas('orders', $order);
        $this->assertAuthenticated();
    }

    public function test_order_process_using_bank_transfer_with_valid_request(): void
    {
        $this->prepare();
        $this->register();
        $userAddress = $this->storeAddress();
        $orderRequest = array_merge([
            'user_address_id'   => $userAddress['id'],
            'payment_type'      => 'bank_transfer',
            'bank'              => 'bca',
        ], $this->orderRequest());
        $order = array_merge(['user_id' => $userAddress['user_id']], Arr::except($orderRequest, 'order_items'));

        $response = $this->json('post', route('order.store'), $orderRequest);

        $response->assertCreated();
        $response->assertJson([
            'rc'        => config('rc.successfully.rc'),
            'message'   => config('rc.successfully.message'),
            'data'      => [
                'order' => $order
            ]
        ]);
        $this->assertDatabaseHas('orders', $order);
        $this->assertAuthenticated();
    }

    public function test_order_process_with_invalid_request(): void
    {
        $this->register();

        $response = $this->json('post', route('order.store'));

        $response->assertStatus(400);
        $response->assertJson(config('rc.invalid_data'));
        $response->assertInvalid(
            ['user_address_id', 'shipper_id', 'payment_type', 'shipment_weight', 'shipment_weight_unit', 'shipment_price', 'shipment_eta'],
            'default',
            'error'
        );
        $this->assertAuthenticated();
    }

    public function test_order_process_using_saldo_with_insufficient_balance(): void
    {
        $this->prepare();
        $this->register();
        $userAddress = $this->storeAddress();
        $orderRequest = array_merge($this->orderRequest(), [
            'user_address_id'   => $userAddress['id'],
            'amount'            => 999999
        ]);
        $order = array_merge(['user_id' => $userAddress['user_id']], Arr::except($orderRequest, 'order_items'));

        $response = $this->json('post', route('order.store'), $orderRequest);

        $response->assertStatus(400);
        $response->assertJson(config('rc.insufficient_balance'));
        $this->assertDatabaseMissing('orders', $order);
        $this->assertAuthenticated();
    }

    public function test_order_process_with_product_out_of_stock(): void
    {
        $this->prepare();
        $this->register();
        $userAddress = $this->storeAddress();
        $orderRequest = array_merge($this->orderRequest(), [
            'user_address_id'   => $userAddress['id'],
            'order_items'       => [
                0 => [
                    'product_id'    => 1,
                    'variant_id'    => 1,
                    'qty'           => 1000,
                    'note'          => null
                ]
            ]
        ]);
        $order = array_merge(['user_id' => $userAddress['user_id']], Arr::except($orderRequest, 'order_items'));

        $response = $this->json('post', route('order.store'), $orderRequest);

        $response->assertStatus(400);
        $response->assertJson(config('rc.out_of_stock'));
        $this->assertDatabaseMissing('orders', $order);
        $this->assertAuthenticated();
    }

    public function test_order_process_without_authentication(): void
    {
        $response = $this->json('post', route('order.store'));

        $response->assertUnauthorized();
        $response->assertJson(config('rc.unauthenticated'));
        $this->assertGuest();
    }
}

When I am running test one by one of function in testing using extension better php unit, the result is passed. But, when I am running testing one class, the testing not ended and not showing error in long time. How to fix it?

0 likes
5 replies
Tray2's avatar

Is there any particular reason why you don't use RefreshDatabase instead of doing it manually?

boboboy's avatar

@Tray2 i am not use RefreshDatabase because i am using database to development, so i don't want to seeding again after testing

Tray2's avatar
Tray2
Best Answer
Level 74

@luqmansolihin Part of the TDD way of working is controlling the world, and if you don't reset the world for each test, how are you then going to be able to control the world?

I suggest following the standard way of running tests against the database.

Use an SQLite in memory database until it's no longer possible (might be due to using syntax not supported in SQLite), then if necessary switch to the RDBMS that you will use in production.

If you think the tests will be slow, they won't be that slow, and if you want more speed, check out parallel testing.

https://laravel.com/docs/9.x/testing#running-tests-in-parallel

2 likes
boboboy's avatar

@Tray2 Thanks, this is very help me. When i am using RefreshDatabase and env.testing, the testing running more faster than using truncate manually.

1 like

Please or to participate in this conversation.