PHP code example of rouxtaccess / laravel-openapi-test

1. Go to this page and download the library: Download rouxtaccess/laravel-openapi-test library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rouxtaccess / laravel-openapi-test example snippets




namespace Tests\Feature\Api;

use App\User;
use RouxtAccess\OpenApi\Testing\Laravel\Traits\ImplementsOpenApiFunctions;
use Symfony\Component\HttpFoundation\Response;
use Tests\TestCase;

class AuthLoginTest extends TestCase
{
    use ImplementsOpenApiFunctions;
    
    protected function setUp(): void
    {
        parent::setUp();
        $this->setUpOpenApiTester();
    }

    public function testLoginWithoutDetails()
    {
        $this->requester->withMethod('POST')
            ->withPath('/api/auth/login');

        $this->validateRequestFails()
            ->sendRequest()
            ->validateResponse(Response::HTTP_UNPROCESSABLE_ENTITY);

        $this->assertResponseHas('errors.email');
        $this->assertResponseHas('errors.password');
    }

    public function testLoginIncorrectDetails()
    {
        $this->requester->withMethod('POST')
            ->withPath('/api/auth/login')
            ->withRequestBody(['email' => '[email protected]', 'password' => 'not_a_valid_password']);

        $this->validateRequestFails()
            ->sendRequest()
            ->validateResponse(Response::HTTP_UNAUTHORIZED);
    }

    public function testLoginSuccess()
    {
        $user = factory(User::class)->create(['name' => 'test-user', 'email' => '[email protected]', 'password' => bcrypt('bestpassword')]);

        $this->requester->withMethod('POST')
            ->withPath('/api/auth/login')
            ->withRequestBody(['email' => $user->email, 'password' => 'bestpassword']);

        $this->validateRequest()
            ->sendRequest()
            ->validateResponse(Response::HTTP_OK);
    }
}