-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from markhuot/snapshots
- Loading branch information
Showing
28 changed files
with
482 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Snapshots | ||
A variety of snapshot assertions are available to help you test your HTML and DOM output in craft-pest. In | ||
many places you can simply expect an object `->toMatchSnapshot()` and Pest will handle the rest for you. | ||
The two entrypoints to snapshotting are, | ||
- `expect($object)->toMatchSnapshot()` | ||
- `$this->assertMatchesSnapshot()` | ||
For example, responses, DOM Lists, and views are all snapshotable. | ||
```php | ||
it('matches responses')->get('/')->assertMatchesSnapshot(); | ||
it('matches dom lists')->get('/')->querySelector('h1')->assertMatchesSnapshot(); | ||
it('matches views')->renderTemplate('_news/entry', $variables)->assertMatchesSnapshot(); | ||
``` | ||
## Elements | ||
Many elements can be snapshotted as well. When using assertions Pest will automatically handle the | ||
conversion from an Element to a snapshot. | ||
```php | ||
Entry::factory()->title('foo')->create()->assertMatchesSnapshot(); | ||
``` | ||
Unfortunately, Pest is not smart enough to properly snapshot elements in an expectation so you must | ||
call `->toSnapshot()` on them first. | ||
```php | ||
it('imports entries', function () { | ||
$this->importEntries(); | ||
$entry = Entry::find()->section('news')->one(); | ||
expect($entry->toSnapshot())->toMatchSnapshot(); | ||
}); | ||
``` | ||
## Attributes | ||
Only a subset of attributes from the element are snapshotted so dynamic fields do not create | ||
unexpected failures. For example, the `$entry->postDate` defaults to the current time when | ||
the entry was generated. That means running the test on Tuesday and again on Wednesday would | ||
fail the test because the `postDate` would be Tuesday in the initial snapshot and Wednesday | ||
during the comparison. | ||
If you'd like to include additional attributes in the snapshot you can pass them as an array | ||
to the `->toSnapshot()` method. For example, | ||
```php | ||
it('snapshots postDate', function () { | ||
$entry = Entry::factory()->postDate('2022-01-01')->create(); | ||
expect($entry->toSnapshot(['postDate']))->toMatchSnapshot(); | ||
$entry->assertMatchesSnapshot(['postDate']); | ||
}); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<?php | ||
|
||
namespace markhuot\craftpest\behaviors; | ||
|
||
use craft\base\ElementInterface; | ||
use craft\elements\db\ElementQuery; | ||
use craft\elements\ElementCollection; | ||
use Illuminate\Support\Collection; | ||
use yii\base\Behavior; | ||
|
||
/** | ||
* @property ElementInterface $owner | ||
*/ | ||
class SnapshotableBehavior extends Behavior | ||
{ | ||
/** | ||
* @param array $extraAttributes Any additional fields that should be included in the snapshot | ||
* @param array $attributes The default list of attributes that should be included in a snapshot | ||
*/ | ||
public function toSnapshot(array $extraAttributes=[], array $attributes=['title', 'slug', 'isDraft', 'isRevision', 'isNewForSite', 'isUnpublishedDraft', 'enabled', 'archived', 'uri', 'trashed', 'ref', 'status', 'url']) | ||
{ | ||
$customFields = collect($this->owner->getFieldLayout()->getCustomFields()) | ||
->mapWithKeys(function ($field) { | ||
return [$field->handle => $field]; | ||
}) | ||
|
||
// remove any ElementQueries from the element so we don't try to snapshot | ||
// a serialized query. It will never match because it may have a dynamic `->where()` | ||
// or an `->ownerId` that changes with each generated element. | ||
->filter(fn ($field, $handle) => ! ($this->owner->{$handle} instanceof ElementQuery)) | ||
|
||
// snapshot any eager loaded element queries so nested elements are downcasted | ||
// to a reproducible array | ||
->map(function ($value, $handle) { | ||
if ($this->owner->{$handle} instanceof ElementCollection) { | ||
$value = $this->owner->{$handle}; | ||
return $value->map->toSnapshot(); // @phpstan-ignore-line can't get PHPStan to reason about the ->map higher order callable | ||
} | ||
|
||
return $value; | ||
}); | ||
|
||
return $customFields->merge( | ||
collect($attributes)->merge($extraAttributes) | ||
->mapWithKeys(fn ($attribute) => [ | ||
$attribute => $this->owner->{$attribute} ?? null, | ||
]) | ||
) | ||
->all(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace markhuot\craftpest\test; | ||
|
||
/** | ||
* # Snapshots | ||
* | ||
* A variety of snapshot assertions are available to help you test your HTML and DOM output in craft-pest. In | ||
* many places you can simply expect an object `->toMatchSnapshot()` and Pest will handle the rest for you. | ||
* | ||
* The two entrypoints to snapshotting are, | ||
* - `expect($object)->toMatchSnapshot()` | ||
* - `$this->assertMatchesSnapshot()` | ||
* | ||
* For example, responses, DOM Lists, and views are all snapshotable. | ||
* | ||
* ```php | ||
* it('matches responses')->get('/')->assertMatchesSnapshot(); | ||
* it('matches dom lists')->get('/')->querySelector('h1')->assertMatchesSnapshot(); | ||
* it('matches views')->renderTemplate('_news/entry', $variables)->assertMatchesSnapshot(); | ||
* ``` | ||
* | ||
* ## Elements | ||
* | ||
* Many elements can be snapshotted as well. When using assertions Pest will automatically handle the | ||
* conversion from an Element to a snapshot. | ||
* | ||
* ```php | ||
* Entry::factory()->title('foo')->create()->assertMatchesSnapshot(); | ||
* ``` | ||
* | ||
* Unfortunately, Pest is not smart enough to properly snapshot elements in an expectation so you must | ||
* call `->toSnapshot()` on them first. | ||
* | ||
* ```php | ||
* it('imports entries', function () { | ||
* $this->importEntries(); | ||
* $entry = Entry::find()->section('news')->one(); | ||
* | ||
* expect($entry->toSnapshot())->toMatchSnapshot(); | ||
* }); | ||
* ``` | ||
* | ||
* ## Attributes | ||
* | ||
* Only a subset of attributes from the element are snapshotted so dynamic fields do not create | ||
* unexpected failures. For example, the `$entry->postDate` defaults to the current time when | ||
* the entry was generated. That means running the test on Tuesday and again on Wednesday would | ||
* fail the test because the `postDate` would be Tuesday in the initial snapshot and Wednesday | ||
* during the comparison. | ||
* | ||
* If you'd like to include additional attributes in the snapshot you can pass them as an array | ||
* to the `->toSnapshot()` method. For example, | ||
* | ||
* ```php | ||
* it('snapshots postDate', function () { | ||
* $entry = Entry::factory()->postDate('2022-01-01')->create(); | ||
* | ||
* expect($entry->toSnapshot(['postDate']))->toMatchSnapshot(); | ||
* $entry->assertMatchesSnapshot(['postDate']); | ||
* }); | ||
* ``` | ||
*/ | ||
trait SnapshotAssertions | ||
{ | ||
public function assertMatchesSnapshot(): self | ||
{ | ||
expect($this)->toMatchSnapshot(); | ||
|
||
return $this; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
tests/.pest/snapshots/SnapshotTest/it_asserts_dom_snapshots.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
[ | ||
"<ul>\n <li>one<\/li>\n <li>two<\/li>\n <li>three<\/li>\n <\/ul>", | ||
"<ul>\n <li>one<\/li>\n <li>two<\/li>\n <li>three<\/li>\n <\/ul>" | ||
] |
Oops, something went wrong.