Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/sequence' into sequence
Browse files Browse the repository at this point in the history
  • Loading branch information
markhuot committed Oct 27, 2023
2 parents 703ee2b + ebe7957 commit 9b3c41b
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
23 changes: 20 additions & 3 deletions docs/factories.md
Original file line number Diff line number Diff line change
Expand Up @@ -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())
])
```

Expand Down Expand Up @@ -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
Expand All @@ -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.

Expand Down
9 changes: 8 additions & 1 deletion docs/logging-in.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
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();
```
12 changes: 0 additions & 12 deletions docs/making-requests.md
Original file line number Diff line number Diff line change
@@ -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).
Expand All @@ -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()
Expand Down

0 comments on commit 9b3c41b

Please sign in to comment.