PHP code example of n4m-ward / laravel-perry

1. Go to this page and download the library: Download n4m-ward/laravel-perry 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/ */

    

n4m-ward / laravel-perry example snippets




namespace Tests\Perry;

use Perry\Attributes\Info;
use Perry\Attributes\Server;
use Perry\Attributes\Servers;
use Perry\PerryHttp\PerryHttpRequest;
use Illuminate\Foundation\Testing\TestCase as LaravelTestCase;

#[Servers(
    new Server(description: 'Example server local', url: 'http://localhost:8000')
)]
#[Info(
    version: '1.0.0',
    title: 'Example server title',
    description: 'Example server description',
    contactEmail: '[email protected]',
    termsOfService: 'https://example.com/terms-of-service', // optional parameter
    externalDocs: new ExternalDocs( // optional parameter
        url: 'https://example.com/external-docs',
        description: 'Find more info here',
    ),
)]
abstract class BaseTestCase extends LaravelTestCase
{
    use PerryHttpRequest;
}


public function test_shouldCreateUser() {
    $this
        ->perryHttp()
        ->withBody([
            'name' => 'John Doe',
            'age' => 25,
            'email' => '[email protected]',
            'password' => 'password',
        ])
        ->post('/user')
        ->assertJson(['success' => true])
        ->assertStatus(Response::HTTP_CREATED);

    $this->assertDatabaseHas('users', [
        'name' => 'John Doe',
        'age' => 25,
        'email' => '[email protected]',
    ]);
}

public function test_exampleWithHeaders(): void
{
    $this
        ->perryHttp()
        ->withHeaders([
            'Authorization' => 'Bearer token',
        ])
        ->get('/api/example');
}

public function test_exampleWithHeaders(): void
{
    $this
        ->perryHttp()
        ->withBody([
            'foo' => 'bar',
        ])
        ->post('/api/example');
}

public function test_exampleWithHeadersAndBody(): void
{
    $this
        ->perryHttp()
        ->withHeaders([
            'Authorization' => 'Bearer token',
        ])
        ->withBody([
            'foo' => 'bar',
        ])
        ->post('/api/example');
}

public function test_get(): void
{
    $this->perryHttp()->get('/api/example/123');
}

public function test_post(): void
{
    $this
        ->perryHttp()
        ->withBody(['foo' => 'bar'])
        ->post('/api/example');
}

public function test_put(): void
{
    $this
        ->perryHttp()
        ->withBody(['foo' => 'bar'])
        ->put('/api/example/123');
}

public function test_patch(): void
{
    $this
        ->perryHttp()
        ->withBody(['foo' => 'bar'])
        ->patch('/api/example/123');
}

public function test_delete(): void
{
    $this->perryHttp()->delete('/api/example/123');
}

#[SecurityScheme(securityScheme: 'BearerToken', type: 'http', scheme: 'bearer')]
class SomeTest extends BaseTestCase {}

#[UseSecurityScheme('BearerToken')]
public function test_shouldCreateUser(): void
{
}

#[\Perry\Attributes\Tag\Tag(name: 'User', description: "User related endpoints")]
class SomeTest extends BaseTestCase {}

#[\Perry\Attributes\Tag\Tag(
    name: 'User', 
    description: "User related endpoints"
    externalDocs: new \Perry\Attributes\ExternalDocs(
        url: 'https://example.com/external-docs',
        description: 'Find more info here',
)]
class SomeTest extends BaseTestCase {}

#[\Perry\Attributes\Tag\UsingTag('User')]
public function test_shouldCreateUser(): void
{
}

public function test_shouldMakeHttpRequest_AndValidateResponseBody(): void
{
    $this
        ->perryHttp()
        ->get('/user/123')
        ->assertJson([
            'name' => 'John Doe',
            'age' => 25,
            'email' => '[email protected]',
            'password' => 'password',
        ])
        ->assertStatus(Response::HTTP_OK);
}

public function test_shouldDoHttpRequest_AndValidateIsUnauthorized(): void
{
    $this
        ->perryHttp()
        ->delete('/user/123')
        ->assertJson([
            'message' => 'Unauthorized',
        ])
        ->assertUnauthorized();
}