ostark / craft-mockery
此包已被废弃且不再维护。未建议替代包。
Craft 模拟
dev-master
2021-01-02 09:23 UTC
Requires
- php: ^7.4 || ^8.0
- craftcms/cms: ^3.5.15
- mockery/mockery: ^1.4
Requires (Dev)
- codeception/module-yii2: ^1.1
- nunomaduro/phpinsights: ^1.14
- pestphp/pest: ^0.3.10
- phpstan/phpstan: ^0.12
- spatie/pest-plugin-snapshots: ^0.3.1
- symplify/easy-coding-standard: ^8.3
This package is auto-updated.
Last update: 2022-07-19 11:32:48 UTC
README
此包有助于使用 Mockery 在 Craft 中模拟对象。它是为那些不大量接触数据库的插件或模块设计的。与 fixtures 不同,fixtures 定义了测试数据集的持续和可预测状态,使用模拟可以更轻松地测试不同的场景,且无需过多的设置。
请注意,如果您的代码严重依赖于数据库 CRUD 操作,这可能是您测试的不正确工具。
设置
cd projects/your-plugin
composer require --dev ostark/craft-mockery
请确保使用您的测试加载此 tests/_bootstrap.php 文件。对于 PHPUnit,您可以在 phpunit.xml 中使用 bootstrap
属性进行定义,或者在 Codeception 中在 .yml 文件中定义。
模拟元素
ostark\CraftMockery\Element::make(\craft\elements\Entry::class);
ostark\CraftMockery\Element::make(\craft\elements\Category::class);
模拟记录
ostark\CraftMockery\Record::make(\craft\elements\Entry::class);
ostark\CraftMockery\Record::make(\craft\elements\Category::class);
模拟服务
ostark\CraftMockery\Service::all();
模拟查询
\ostark\CraftMockery\Query::make(\craft\db\Query::class);
期望
实际结果存储在简单的 PHP 文件中,文件名以类名为名。这些文件的存储位置是 tests/expectations
。以下是一个 Entry.php
文件的示例。
<?php return [ 'section(foo).all()' => [ entry([ 'id' => 1, 'title' => 'Fake title' ]), entry([ 'id' => 2, 'title' => 'Another fake title' ]) ], 'one()' => entry([ 'id' => 99, 'siteId' => 123, 'title' => 'fake' ]), ];
此数组中的键与查询的链式方法相匹配。要构造模型对象值,有可用的辅助函数:entry()
和 record()
。