Skip to content

Testing with Sharp

Sharp provides a few assertions and helpers to help you test your Sharp code.

The SharpAssertions trait

The Code16\Sharp\Utils\Testing\SharpAssertions trait is intended to be used in a Feature test. It has to be initialized, like this:

php
protected function setUp()
{
    parent::setUp();

    $this->initSharpAssertions();
}
protected function setUp()
{
    parent::setUp();

    $this->initSharpAssertions();
}

Helpers

First, the trait adds a few helpers:

loginAsSharpUser

Logs in the given user as a Sharp user.

getSharpForm

Call the Sharp API to display the form for the Entity $entityKey. If $instanceId is provided, it will be an edit form, and otherwise a create one.

updateSharpForm

Call the Sharp API to update the Entity $entityKey of id $instanceId, with $data.

storeSharpForm

Call the Sharp API to store a new Entity $entityKey with $data.

deleteSharpEntityList

Call the Sharp API to delete an $entityKey instance on the Entity List.

deleteSharpShow

Call the Sharp API to delete an $entityKey instance on the Show Page.

callSharpEntityCommandFromList

Call the $commandKeyOrClassName Entity Command with the optional $data.

callSharpInstanceCommandFromList

Call the $commandKeyOrClassName Instance Command with the optional $data.

callSharpInstanceCommandFromShow

Call the $commandKeyOrClassName Instance Command with the optional $data.

withSharpCurrentBreadcrumb

It can be useful to fake a Sharp context before calling a Sharp endpoint, and that's the purpose of this method; $breadcrumb is an array of arrays, each one containing, in this order:

  • a Sharp page type: "list", "show" or "form"
  • an entityKey
  • (optional) an instanceId

For instance:

php
    $this
        ->withSharpCurrentBreadcrumb([
            ['list', 'trees'],
            ['show', 'trees', 8],
            ['show', 'leaves', 16],
            ['form', 'leaves', 16],
        ])
        ->getSharpForm(...)
        ->assertOk();
    $this
        ->withSharpCurrentBreadcrumb([
            ['list', 'trees'],
            ['show', 'trees', 8],
            ['show', 'leaves', 16],
            ['form', 'leaves', 16],
        ])
        ->getSharpForm(...)
        ->assertOk();

Assertions

You can use regular assertions, for instance:

php
$this
    ->updateSharpForm(
        'orders',
        $order->id,
        array_merge($order->toArray(), [
            'client' => 'test',
            'payment_delay' => 10
        ])
    )
    ->assertStatus(200);
$this
    ->updateSharpForm(
        'orders',
        $order->id,
        array_merge($order->toArray(), [
            'client' => 'test',
            'payment_delay' => 10
        ])
    )
    ->assertStatus(200);

But sometimes you'll want to test some specific Sharp things. Here's the list of custom assertions added by the SharpAssertions trait:

assertSharpHasAuthorization

assertSharpHasNotAuthorization

Example:

php
$this->getSharpForm('orders', $order->id)
     ->assertSharpHasAuthorization('update')
     ->assertSharpHasAuthorization('delete');
$this->getSharpForm('orders', $order->id)
     ->assertSharpHasAuthorization('update')
     ->assertSharpHasAuthorization('delete');

assertSharpFormHasFields

Example:

php
$this->getSharpForm('orders')
     ->assertSharpFormHasFields([
           'number', 'client'
     ]);
$this->getSharpForm('orders')
     ->assertSharpFormHasFields([
           'number', 'client'
     ]);

assertSharpFormHasFieldOfType

Example:

php
$this->getSharpForm('orders', $order->id)
     ->assertSharpFormHasFieldOfType(
         'number', SharpFormTextField::class
     );
$this->getSharpForm('orders', $order->id)
     ->assertSharpFormHasFieldOfType(
         'number', SharpFormTextField::class
     );

assertSharpFormDataEquals

Example:

php
$this->getSharpForm('orders', $order->id)
     ->assertSharpFormDataEquals('number', $order->number);
$this->getSharpForm('orders', $order->id)
     ->assertSharpFormDataEquals('number', $order->number);

Released under the MIT License.