PHP code example of bradietilley / pest-stories

1. Go to this page and download the library: Download bradietilley/pest-stories 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/ */

    

bradietilley / pest-stories example snippets


uses(BradieTilley\Stories\Concerns\Stories::class);

action('as_admin', function () {
    actingAs(User::factory()->admin()->create());
});

action('as_customer', function () {
    actingAs(User::factory()->customer()->create());
});

action('create_product', function (array $product) {
    return test()->postJson(route('products.store'), array_replace([
        'title' => 'Default',
        'sku' => 'default',
        'price' => 99.99,
    ], $product))
}, 'response');

action('response:ok', function (TestResponse $response) {
    $response->assertOk();
});

action('response:invalid', function (TestResponse $response) {
    $response->assertUnprocessable();
});

test('can create a product via the api')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ])
    ->action('create_product')
    ->assertDatabaseHas('products', [
        'title' => 'Default',
    ]);

test('cannot create a product via the api as a customer')
    ->action('as_customer')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ])
    ->action('create_product')
    ->assertDatabaseMissing('products', [
        'title' => 'Default',
    ]);

test('can create a product via the api with a custom title')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [ 'title' => 'Custom Product' ])
    ->action('create_product', [
        'product' => [
            'title' => 'Custom Product',
        ],
    ])
    ->assertDatabaseHas('products', [ 'title' => 'Custom Product' ]);

test('can create a product via the api with a custom price')
    ->action('as_admin')
    ->assertDatabaseMissing('products', [ 'price' => 12345.67 ])
    ->action('create_product', [
        'product' => [
            'price' => 12345.67,
        ],
    ])
    ->assertDatabaseHas('products', [ 'price' => 12345.67 ]);