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.

php
class PostFormTest extends TestCase
{
    use SharpAssertions;
    
    // ...
}

Helpers

The trait adds a few helpers:

loginAsSharpUser

Logs in the given user as a Sharp user.

getSharpShow

Call the Sharp API to display the Show Page for the Entity $entityKey and instance $instanceId.

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 creation one.

getSharpSingleForm

Call the Sharp API to display the edit Form for the single Entity $entityKey.

updateSharpForm

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

updateSharpSingleForm

Call the Sharp API to update the single Entity $entityKey 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.

withSharpBreadcrumb

Most of the time, the breadcrumb automatically set by Sharp is enough. But sometimes it can be useful to define a whole Sharp context before calling an endpoint, and that's the purpose of this method. The $callback contains a built instance of Code16\Sharp\Utils\Links\BreadcrumbBuilder, which can be used like this:

php
it('allow the user to display a leaf form', function () {
    $this
        ->loginAsSharpUser()
        ->withSharpBreadcrumb(function (BreadcrumbBuilder $builder) {
            return $builder
                ->appendEntityList('trees')
                ->appendShowPage('trees', 6)
                ->appendShowPage('leaves', 16);
        })
        ->getSharpForm('leaves', 16)
        ->assertOk();
});

Released under the MIT License.