diff --git a/docs/factories.md b/docs/factories.md index becf436..480edcc 100644 --- a/docs/factories.md +++ b/docs/factories.md @@ -60,10 +60,10 @@ passed an instance of faker ```php Entry::factory() ->set('title, 'SOME GREAT TITLE') - ->set('title', fn ($faker) => str_to_upper($faker->sentence)) + ->set('title', fn ($faker) => str_to_upper($faker->sentence())) ->set([ 'title' => 'SOME GREAT TITLE', - 'title' => fn ($faker) => str_to_upper($faker->sentence) + 'title' => fn ($faker) => str_to_upper($faker->sentence()) ]) ``` @@ -98,7 +98,7 @@ class Post extends \markhuot\craftpest\factories\Entry { return [ // The entry's title field - 'title' => $this->faker->sentence, + 'title' => $this->faker->sentence(), // A Category field takes an array of category ids or category factories 'category' => Category::factory()->count(3), // Generate three body paragraphs of text @@ -118,6 +118,23 @@ set by definition or through a `->set()` call. When creating custom factories, this will most likely meed to be overridden. +## sequence($sequence) +Set a sequence that will be iterated on as multiple models are created. You can +set this to a callback (which gets passed the index) or an array of definitions +where each definition will be used in order. +```php +->sequence(fn ($index) => ['someField' => "the index is {$index}"]) +->sequence(['someField' => 'the index is 1'], ['someField' => 'the index is 2']) +``` +With the array approach the sequence will be iterated over and looped so if you +pass two items in to a sequence the third created element will re-use the first +item in the sequence. E.g., this will iterate around true/false admins creating +5 admins and 5 non-admins. +```php +->count(10) +->sequence(['isAdmin' => true], ['isAdmin' => false]) +``` + ## make($definition = array ()) Instantiate an Model without persisting it to the database. diff --git a/docs/logging-in.md b/docs/logging-in.md index 0aa7104..3245131 100644 --- a/docs/logging-in.md +++ b/docs/logging-in.md @@ -12,4 +12,11 @@ Acting as accepts a number of "user-like" identifiers to log in a user for the t For many tests the actual user doesn't matter, only that the user is an admin. This method will return a generic user with admin permissions. This is helpful for testing that something works, not whether the permissions for that thing are accurate. For more fine-tuned permission -testing you should use `->actingAs()` with a curated user element. \ No newline at end of file +testing you should use `->actingAs()` with a curated user element. + +## withToken(string $token) +For GQL requests (and other bearer token requests) you can set a token on the request by calling +`->withToken()` and passing a valid bearer token. +```php +$this->withToken($token)->get('/')->assertOk(); +``` \ No newline at end of file diff --git a/docs/making-requests.md b/docs/making-requests.md index 0bdbbd1..edfacc1 100644 --- a/docs/making-requests.md +++ b/docs/making-requests.md @@ -1,35 +1,26 @@ # Requests - You can simulate requests to the Craft application via several helper methods on the `TestCase` as well as via the standalone `Http` helpers. The most common helper is `$this->get('uri')` or `get('uri')`. This will make a request to Craft at the given `'uri'` and return a [`TestableResponse`](assertions/response.md). - You can kick off a request in a classic test, - ```php it ('gets something', function () { $this->get('/')->assertOk(); }); ``` - Using Pest's higher order proxies you can do the same thing without a closure, - ```php it('gets something') ->get('/') ->assertOk(); ``` - And, lastly, you can skip the description all together and use a descriptionless test. - ```php use function markhuot\craftpest\helpers\Http\get; - get('/')->assertOk(); ``` - All of these are functionally identical. You are free to select the syntax that reads the most naturally for your test and provides the right context for the test. For more information on the test context see, [Getting Started](getting-started.md). @@ -39,19 +30,16 @@ Makes a `GET` request to Craft. ## post(string $uri, array $body = array ()) Makes a `POST` request to Craft. - ```php $this->post('/comments', [ 'author' => '...', 'body' => '...', ])->assertOk(); ``` - Because _many_ `POST` requests need to send the CSRF token along with the request it is handled automatically within the `->post()` method. If you would prefer to handle this yourself you may use the raw `->http()` method insetad. The above `/comments` example is functionally similar to, - ```php $this->http('post', '/comments') ->withCsrfToken()