{"id":18907880,"url":"https://github.com/fkupper/module-dynamic-snapshots","last_synced_at":"2025-04-15T04:32:07.914Z","repository":{"id":45568853,"uuid":"280385438","full_name":"fkupper/module-dynamic-snapshots","owner":"fkupper","description":"This is a module that can be used together with Codeception to test Snapshots with dynamic data.","archived":false,"fork":false,"pushed_at":"2024-04-23T07:25:16.000Z","size":94,"stargazers_count":0,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2024-09-19T11:44:12.331Z","etag":null,"topics":["codeception","dynamic-data","dynamic-snapshots","snapshot-data","snapshot-tests"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/fkupper.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-07-17T09:36:45.000Z","updated_at":"2022-09-21T08:59:52.000Z","dependencies_parsed_at":"2024-04-23T09:14:59.094Z","dependency_job_id":null,"html_url":"https://github.com/fkupper/module-dynamic-snapshots","commit_stats":{"total_commits":48,"total_committers":3,"mean_commits":16.0,"dds":"0.22916666666666663","last_synced_commit":"8516c7b1598ca069522ca4e1122ea58a6d27dc40"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkupper%2Fmodule-dynamic-snapshots","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkupper%2Fmodule-dynamic-snapshots/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkupper%2Fmodule-dynamic-snapshots/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/fkupper%2Fmodule-dynamic-snapshots/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/fkupper","download_url":"https://codeload.github.com/fkupper/module-dynamic-snapshots/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249006550,"owners_count":21197294,"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":["codeception","dynamic-data","dynamic-snapshots","snapshot-data","snapshot-tests"],"created_at":"2024-11-08T09:23:45.345Z","updated_at":"2025-04-15T04:32:06.499Z","avatar_url":"https://github.com/fkupper.png","language":"PHP","readme":"# Codeception Dynamic Snapshots Module\n\n![Build Status](https://github.com/fkupper/module-dynamic-snapshots/actions/workflows/php.yml/badge.svg?branch=master)\n[![Coverage Status](https://coveralls.io/repos/github/fkupper/module-dynamic-snapshots/badge.svg?branch=master)](https://coveralls.io/github/fkupper/module-dynamic-snapshots?branch=master)\n[![Total Downloads](https://poser.pugx.org/fkupper/module-dynamic-snapshots/downloads)](//packagist.org/packages/fkupper/module-dynamic-snapshots)\n[![Monthly Downloads](https://poser.pugx.org/fkupper/module-dynamic-snapshots/d/monthly)](//packagist.org/packages/fkupper/module-dynamic-snapshots)\n\n\nThis is a module that can be used together with [Codeception](https://github.com/Codeception/Codeception) to test Snapshots with dynamic data.\n\n# Installation\n\nUsing composer:\n``` shell\ncomposer require \"fkupper/module-dynamic-snapshots\"\n```\n\nOn your `codeception.yml` file, add:\n``` yml\nextensions:\n    commands:\n        - Fkupper\\Command\\GenerateDynamicSnapshot\n```\n\n# Usage\n\n## Creating Snapshots\nNew snapshots cna be created using codeception client with the custom command in this package. Eg:\n``` shell\nphp ./vendor/bin/codecept generate:dynamicsnapshot Acceptance Products\n```\n\n## Fetching dynamic snapshot data\nSimilar to vanilla Codeception snapshots, the DynamicSnapshots classes will fetch data using the method `fetchDynamicData`. So, in your snapshots you will have to implement this method:\n```php\nclass FooSnapshot extends DynamicSnapshot\n{\n    /**\n     * @var Tester\n     */\n    protected $tester;\n\n    public function fetchDynamicData()\n    {\n        // fetch snapshot from a helper method or snomething and return\n        return $this-\u003etester-\u003efetchDataFromSomewhere();\n    }\n}\n```\n\n## Substitutions\nThis is the main feature of this package.\nWhen dealing with variable data in snapshots, they can be replaced with placeholders and replaced back in runtime everytime the snapshot tests are executed.\n\nFor example, if you want to test a XML API response containing static data and variable data:\n```xml\n\u003cxml\u003e\n    \u003cappVersion value=\"v8.8.9\" /\u003e\n    \u003csomeOtherNonDynamicData value=\"foo\" /\u003e\n    \u003cbar value=\"baz\" /\u003e\n\u003c/xml\u003e\n```\n\nThe property `\"appVersion\"` can change anytime and to avoid updating it every time, use `setSubstitutions` method to replace it with a string (or a value that can be casted to string) of your choice:\n\n```php\nclass FooSnapshot extends DynamicSnapshot\n{\n    /**\n     * @var Tester\n     */\n    protected $tester;\n\n    public function fetchDynamicData()\n    {\n        $this-\u003esetSubstitutions(\n            // $this-\u003etester-\u003egetAppVersion() returns \"v8.8.9\"\n            'app_version' =\u003e $this-\u003etester-\u003egetAppVersion()\n        );\n        // fetch snapshot from a helper method or snomething and return\n        return $this-\u003etester-\u003efetchDataFromXml();\n    }\n}\n```\n\nThe first time the dynamic snapshot test is executed, a snapshot **data** file will be created like:\n```xml\n\u003cxml\u003e\n    \u003cappVersion value=\"[snapshot_app_version]\" /\u003e\n    \u003csomeOtherNonDynamicData value=\"foo\" /\u003e\n    \u003cbar value=\"baz\" /\u003e\n\u003c/xml\u003e\n```\nNote that placeholders are wrapped in `[ ]`, and from now on, whenever the app version changes, the snapshot will not break or require an update.\n\n## Using the dynamic snapshot classes in tests\nPlease refer to [Codepcetion's standard Snapshot documentation](https://codeception.com/docs/09-Data#Testing-Dynamic-Data-with-Snapshots).\n\n## Custom placeholder wrappers\nBy default, placeholders are wrapped in brackets `[ ]`, but it is possible to change what character or sequence of charaters it should use.\n\nFor example, if brackets are sensible part of your snapshot data, you can change it to something else using `setWrappers`:\n\n```php\nclass FooSnapshot extends DynamicSnapshot\n{\n    protected $tester;\n\n    public function __constructor(Tester $I)\n    {\n        $this-\u003etester = $I;\n        $this-\u003esetWrappers('{', '}');\n        // $this-\u003esetWrappers('(', ')');\n        // $this-\u003esetWrappers('\u003c', '\u003e');\n    }\n}\n```\n\n## Ignoring parts of the snapshot data\nIf your snapshot have variable data that cannot be tested or that you just want to igore, it is possible to provide a list of regular expression patterns that will be removed from the data when asserting.\n\nFor example, in the snapshot data below, the current timestamp in the favicon href property have to be ignored:\n```html\n\u003chtml\u003e\n    \u003chead\u003e\n        \u003c!-- the favicon will always have the current timestamp suffix --\u003e\n        \u003clink rel=\"shortcut icon\" href=\"/favicon.ico?v=1588930951\"\u003e\n    \u003c/head\u003e\n\u003c/html\u003e\n```\nSo you can ignore this line by calling `setIgnoredLinesPatterns` from your snapshot object:\n```php\n$this-\u003esetIgnoredLinesPatterns(['/^.*favicon.*$/m']);\n```\n\n## Handling space sequences\nSometimes your snapshot data can have variable amounts of space characters in sequence that change out of your control.\n\nTo toggle your snapshot behavior to ignore these spaces or not, use `shouldAllowSpaceSequences(true|false)`. Setting it to true will compact these space sequences to a single space character.\n\nNote that the default is `true`, so these sequences will never be reduced to one space if you do not specify them to.\n\n## Handling trailing spaces\nIf you want to clear every line of your snapshot data of trailing spaces, use `shouldAllowTrailingSpaces(false)`.\n\nNote that the default is `true`, so trailing spaces in your snapshot data will never be removed.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkupper%2Fmodule-dynamic-snapshots","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffkupper%2Fmodule-dynamic-snapshots","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffkupper%2Fmodule-dynamic-snapshots/lists"}