R
R
Ruslan Absalyamov2018-10-26 10:41:00
Laravel
Ruslan Absalyamov, 2018-10-26 10:41:00

How to make tests saved in the test database?

I conduct tests, with confirmation, only I need data to be saved by rest not to the main database, but to the test one, how can this be implemented approximately?
My implementation

/**
     * Успещная регистрация
     */
    public function testRegisterSuccess()
    {
        $response = $this->getPathSuccess('POST', '/api/register', ['email' => '[email protected]', 'password' => 'test123']);
        $response->assertJsonStructure([
           'success',
           'error',
           'response' => ['message'],
           'message'
        ]);
    }
/**
     * Проверяет на успешность запроса на регистрацию
     * @param $method
     * @param $url
     * @param $data
     *
     * @return \Illuminate\Foundation\Testing\TestResponse
     */
    protected function getPathSuccess(string $method, string $url, array $data)
    {
        return $this->json($method, $url, $data)
            ->assertStatus(200)
            ->assertHeader('Content-type', 'application/json;charset=utf-8');
    }

controller if needed
public function register(ApiRegister $request)
    {
        $request->validated();
        try {
            User::create([
                'name' => $request->input('name'),
                'email' => $request->input('email'),
                'password' => Hash::make($request->input('password'))
            ]);
            return ResponseApi::response(['message' => 'Пользователь создан']);
        } catch (\Exception $exception){
            return ResponseApi::response([], false, 500, ['errors' => 'Произошла ошибка']);
        }
    }

added to /confin/database.php
'testing' => [
            'driver' => 'pgsql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '5432'),
            'database' => env('DB_DATABASE', 'test_database'),
            'username' => env('DB_USERNAME', 'forge'),
            'password' => env('DB_PASSWORD', ''),
            'charset' => 'utf8',
            'prefix' => '',
            'schema' => 'public',
            'sslmode' => 'prefer',
        ],

In .env.testing changed
APP_ENV=testing

DB_CONNECTION=testing
DB_HOST=127.0.0.1
DB_PORT=5432
DB_DATABASE=test_database
DB_USERNAME=implant_user
DB_PASSWORD=kazan1811

And in phpunit.xml
<env name="APP_ENV" value="testing"/>
        <env name="DB_CONNECTION" value="testing"/>

Then applied the migration
php artisan migrate --database=testing --env=testing

The migration has been applied, the tables have been created, but now the question is how to make the test data written to the test_database test database.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stanislav, 2018-10-26
@rusline18

<env name="DB_DATABASE" value="test_database"/>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question