PHP code example of wolfpack-it / laravel-gwt-plugin

1. Go to this page and download the library: Download wolfpack-it/laravel-gwt-plugin 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/ */

    

wolfpack-it / laravel-gwt-plugin example snippets


use App\Mails\ConfirmationMail;
use App\Models\Language;
use Illuminate\Contracts\Auth\Authenticatable;
use Illuminate\Mail\Mailable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Testing\TestResponse;
use WolfpackIT\LaravelGWTPlugin\TestCase;

// Extend the Laravel GWT Plugin TestCase
class StorePostTest extends TestCase
{
    public function test_user_can_store_post(): void
    {
        $this
            // Act as a random User
            ->as(User::factory()->create())
            // Fake sending the email
            ->fake(Mail::class)
            // Given is the post data, stored in a param called `post`
            ->given(fn (): array => [
                'title' => 'My first post',
                'content' => 'Lorem ipsum dolor amet sum it',
            ], 'post')
            // Given is the current language
            ->given(fn(): Language => Language::current())
            // The $post and $language are automatically injected based on the results of the `given` methods
            ->when(fn (array $post, Language $language): TestResponse =>
                $this->postJson(route('api.posts.store'), $post)
            )
            // Instead of using a closure you can call a protected method as a callable with `(...)`
            ->then($this->responseContainsPostId(...))
            // Add as much `then` methods as needed
            ->then($this->confirmationMailIsSent(...));
    }

    // The $response is automatically injected based on the TestResponse result of the `when` method
    protected function responseContainsPostId(TestResponse $response): void
    {
        $response
            ->assertCreated() // Status code 201
            ->assertJson(fn (AssertableJson $json) =>
                $json->has('id')
                // .. add other assertions  
            );
    }

    protected function confirmationMailIsSent(Authenticatable $user): void
    {
        Mail::assertSent(ConfirmationMail::class, function (Mailable $mail) use ($user) {
            return $mail->hasTo($user->email);
            // .. add other assertions  
        });
    }
}

$this->as(User::factory()->create());
$this->as(Admin::factory()->create(), 'admin');

TestScenario::setAuthGuard('admin');
TestScenario::setAuthProvider(TestScenario::PASSPORT_AUTH_PROVIDER);

$this->fake(Mail::class);

    $this
        ->given(fn(): User => User::factory()->student()->create(), 'student')
        ->given(fn(): User => User::factory()->teacher()->create(), 'teacher');

$this->when(fn(array $postData): TestResponse => $this->postJson('/posts', $postData));

$this->then(fn(TestResponse $response) => $response->assertOk());