{"id":21352563,"url":"https://github.com/ostark/craft-testkit","last_synced_at":"2026-01-11T10:51:48.514Z","repository":{"id":47435463,"uuid":"515354344","full_name":"ostark/craft-testkit","owner":"ostark","description":"Testkit for Craft plugins and modules","archived":false,"fork":false,"pushed_at":"2022-07-20T20:34:34.000Z","size":38,"stargazers_count":1,"open_issues_count":1,"forks_count":0,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-01-22T17:24:26.577Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ostark.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-18T22:03:35.000Z","updated_at":"2022-12-27T20:25:39.000Z","dependencies_parsed_at":"2022-09-06T02:10:23.799Z","dependency_job_id":null,"html_url":"https://github.com/ostark/craft-testkit","commit_stats":null,"previous_names":["fortrabbit/craft-testkit"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fcraft-testkit","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fcraft-testkit/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fcraft-testkit/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ostark%2Fcraft-testkit/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ostark","download_url":"https://codeload.github.com/ostark/craft-testkit/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243826778,"owners_count":20354220,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-22T03:14:07.974Z","updated_at":"2026-01-11T10:51:48.508Z","avatar_url":"https://github.com/ostark.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# 🦠🧑‍🔬 Craft Testkit \n\n### An opinionated toolset for testing Craft Plugins and Modules\n\nUnder the hood \n\n* Mockery\n* PestPHP\n\n---\n\nTODO: Review / refactor the interfaces\n\n---\n\nIt's made for plugins or modules that do not touch the database heavily. In contrast to [fixtures](https://craftcms.com/docs/4.x/testing/testing-craft/fixtures.html) that define a consistent and predictable state of your test dataset, with mocking you can test different scenarios more easily with less setup efforts.\n\nPlease mind, if your code relies heavily on database CRUD operations, this is probably not the right tool for your test.\n\n## Setup \n\n```\ncd projects/your-plugin\ncomposer require --dev fortrabbit/craft-testkit\nphp vendor/bin/testkit-init (TBD)\n```\n\nMake sure to load this [tests/_bootstrap.php file](_bootstrap.example.php) with your tests. For PHPUnit your define it in phpunit.xml using the `bootstrap` property or in Codeception your .yml files.\n\n\n## Write your first test\n\n.\n.\n.\n.\n\n\n\n## Mocking\n\nwhat and why\n\n### Mock Elements\n\n```\nfortrabbit\\TestKit\\Element::make(\\craft\\elements\\Entry::class);\nfortrabbit\\TestKit\\Element::make(\\craft\\elements\\Category::class);\n\n// better interface?\nCategoryModel::mock()-\u003eusing('expectations/Category.php');\nCategoryModel::mock()-\u003esetExpectations([\n    'save' =\u003e fn($value) =\u003e false,\n    'getFoo' =\u003e 'foo,  \n])\n\n```\n\n### Mock Records\n\n```\nfortrabbit\\TestKit\\Record::make(\\craft\\elements\\Entry::class);\nfortrabbit\\TestKit\\Record::make(\\craft\\elements\\Category::class);\n```\n\n### Mock Services  \n\n```\nfortrabbit\\TestKit\\Service::all();\n```\n\n\n### Mock Queries  \n\n```\n\\fortrabbit\\TestKit\\Query::make(\\craft\\db\\Query::class);\n\n// better interface?\nEntryQuery::mock()-\u003eusing('expectations/EntryQuery.php');\nEntryQuery::mock()-\u003esetExpectations([\n    'save' =\u003e fn($value) =\u003e false,\n    'getFoo' =\u003e 'foo,  \n])\n\n```\n\n### Expectations\n\nThe actual results are stored in simple php files which named after the class name.\nThe location for these files is `tests/expectations`. Here is an example of an `Entry.php` file:\n\n```php\n\u003c?php\n\nreturn [\n    'section(foo).all()' =\u003e [\n        entry([\n            'id' =\u003e 1,\n            'title' =\u003e 'Fake title'\n        ]),\n        entry([\n            'id' =\u003e 2,\n            'title' =\u003e 'Another fake title'\n        ])\n    ],\n    'one()' =\u003e entry([\n        'id' =\u003e 99,\n        'siteId' =\u003e 123,\n        'title' =\u003e 'fake'\n    ]),\n];\n```\n\nThe keys in this array match with the chained methods of the query. To construct the values which are model objects, there are helper functions available:\n`entry()` and `record()`.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fostark%2Fcraft-testkit","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fostark%2Fcraft-testkit","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fostark%2Fcraft-testkit/lists"}