<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class UsersTest extends TestCase
{
    use RefreshDatabase;

    private array $loginData = ['email' => 'artur.nowakowski@gmail.com', 'password' => 'test123'];

    public function getToken()
    {
        $response = $this->post('http://localhost:8000/api/user/login', $this->loginData)->decodeResponseJson();

        return $response['data']['token'];
    }

    /**
     * A basic test example.
     *
     * @return void
     */
    public function testRegistration()
    {

        $response = $this->post('http://localhost:8000/api/user/register', $this->loginData);

        $response->assertStatus(200);
        $response->assertJsonStructure(['success', 'data' => ['token']]);
    }

    public function testLogin()
    {
        $this->testRegistration();
        $response = $this->post('http://localhost:8000/api/user/login', $this->loginData);

        $response->assertStatus(200);
        $response->assertJsonStructure(['success', 'data' => ['token']]);
    }

    public function testMe()
    {
        $this->testRegistration();
        $token = $this->getToken();
        $response = $this->get('http://localhost:8000/api/user/me', ['Authorization' => "Bearer $token"]);

        $response->assertStatus(200);
        $response->assertJsonStructure(['success', 'data' => ['email', 'role']]);
    }
}