markhuot / craft-pest-core
Pest 运行器
2.0.0-rc6
2023-12-12 09:16 UTC
Requires
- craftcms/cms: ^4.5
- fakerphp/faker: ^1.16
- illuminate/collections: ^8.23|^9.1|^10.0
- mockery/mockery: ^1.5
- pestphp/pest: ^2.26
- symfony/css-selector: ^5.3|^6.0
- symfony/dom-crawler: ^6.0.3
- symfony/process: ^5.3|^6.0
- vlucas/phpdotenv: ^2.4|^3.4|^5.4
Requires (Dev)
- craftcms/craft: ^2.0
- craftcms/phpstan: dev-main
- laravel/pint: ^1.13
- symfony/var-dumper: ^5.0|^6.0
This package is auto-updated.
Last update: 2024-09-13 20:13:34 UTC
README
Craft Pest for Craft CMS
composer require --dev markhuot/craft-pest-core
./craft pest # or ./vendor/bin/pest
处理 Pest 在 Craft CMS 中的设置和安装。这允许你编写类似以下的测试!
it('loads the homepage') ->get('/') ->assertOk(); it('has a welcoming h1 element') ->get('/') ->expectSelector('h1') ->text->toBe('Welcome'); it('asserts nine list items') ->get('/') ->querySelector('li') ->assertCount(9); it('promotes craft') ->get('/') ->assertHeader('x-powered-by', 'Craft CMS'); it('shows news on the homepage', function() { $titles = News::factory()->count(3)->create()->title; $this->get('/') ->expectSelector('.news__title') ->text->sequence(...$titles); });
Craft Pest 提供了多种测试辅助工具,以提高编写测试时的开发者体验。目标是使测试编写尽可能简单,以便在项目过程中编写更多测试。每个功能的更多文档可以在 /docs
文件夹中找到。以下“厨房水槽”测试包含了大多数示例。
it('tests the kitchen sink', function () { // Factories can be used to create fields, sections, entries, etc… Realistically, much // of this may come from your project.yaml, but in the event you need to scaffold some // content types while testing a plugin or module, it is absolutely possible. // What's more, if a project.yaml is detected, Craft Pest will automatically check and // apply that config before each run to ensure you are always testing against a clean // schema. $section = Section::factory()->create(); // Volume factories give you a local folder-based volume that ensures // your tests don't clutter your production S3 buckets, for example. $volume = Volume::factory()->create(); $entry = Entry::factory() // Most fields on the entry factory can be defined just like you would' // when querying `craft.entries` in a template. ->section($section->handle) // Even custom fields can be defined while creating an entry. This allows // you to mock/simulate a variety of content elements without needing to // pass around a complex and huge "seeding" database. ->isPromoted(true) // Custom fields can be set to nested factories and will be automatically // created as they are needed. // Most factories can be utilized with as few as one additional field, like // assets here, which only need to know their volume. The contents of the // image will default to a 500x500px gray square. ->heroImage(Asset::factory()->volume($volume->handle)) ->create(); // For simple tests, you can call `->get()` or `->post()` to make simulated // HTTP requests against the Craft site. It will take a site (or CP) URL and // return the rendered response. // For many people, just starting out with testing, their first (and only) // test is simply `$this->get('/')` to ensure the homepage loads. $this->get($entry->uri) // All HTTP tests should probably check the status code of the response and // ensure Craft returned a 200 Ok response. ->assertOk() // A response can be further inspected to check that the exact HTML matches // your expectations. Here the `querySelector` and `expectSelector` methods // allow you to parse over the response using a familiar CSS-based syntax. ->expectSelector('h1') // Querying the HTML allows you to test the text, count, or even HTML of the // DOM to ensure it matches your expectations. ->text->toBe('Welcome'); });