{"id":15277444,"url":"https://github.com/tattersoftware/codeigniter4-imposter","last_synced_at":"2025-04-12T14:51:50.672Z","repository":{"id":40661870,"uuid":"428707911","full_name":"tattersoftware/codeigniter4-imposter","owner":"tattersoftware","description":"Mock authentication for CodeIgniter 4","archived":false,"fork":false,"pushed_at":"2024-01-18T04:57:28.000Z","size":54,"stargazers_count":2,"open_issues_count":2,"forks_count":2,"subscribers_count":1,"default_branch":"develop","last_synced_at":"2025-03-26T09:21:34.273Z","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":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tattersoftware.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":"SECURITY.md","support":null}},"created_at":"2021-11-16T15:22:26.000Z","updated_at":"2023-02-07T07:29:39.000Z","dependencies_parsed_at":"2022-09-04T05:00:22.248Z","dependency_job_id":null,"html_url":"https://github.com/tattersoftware/codeigniter4-imposter","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-imposter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-imposter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-imposter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tattersoftware%2Fcodeigniter4-imposter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tattersoftware","download_url":"https://codeload.github.com/tattersoftware/codeigniter4-imposter/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248586261,"owners_count":21128995,"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-09-30T11:06:25.057Z","updated_at":"2025-04-12T14:51:50.650Z","avatar_url":"https://github.com/tattersoftware.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Tatter\\Imposter\nMock authentication for CodeIgniter 4\n\n[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/test.yml)\n[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/analyze.yml)\n[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/inspect.yml)\n[![Coverage Status](https://coveralls.io/repos/github/tattersoftware/codeigniter4-imposter/badge.svg?branch=develop)](https://coveralls.io/github/tattersoftware/codeigniter4-imposter?branch=develop)\n\n## Quick Start\n\n1. Install with Composer: `\u003e composer require --dev tatter/imposter`\n2. Access via the service: `$user = service('auth')-\u003euser();`\n\n## Description\n\n`Imposter` provides a thin authentication layer for testing your CodeIgniter apps that\nrequire authentication. This library is for **testing purposes only**. The tiny footprint\nand easy-to-use interfaces make it ideal for handling mock authentication in a rapid way.\n\n`Imposter` fulfills all the [CodeIgniter authentication guidelines](https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html)\nand thus supplies the Composer provision for `codeigniter4/authentication-implementation`.\n\n## Installation\n\nInstall easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities\nand always be up-to-date:\n* `\u003e composer require tatter/imposter`\n\nOr, install manually by downloading the source files and adding the directory to\n`app/Config/Autoload.php`.\n\n## Usage\n\nUse the service to log a user in or out.\n\n```php\nservice('auth')-\u003elogin($userId);\nservice('auth')-\u003elogout();\n```\n\nThe current status can be checked by getting the ID or User; both will be `null` if no\nuser is authenticated.\n\n```php\nif ($user = service('auth')-\u003euser()) {\n    echo 'Logged in!';\n}\n```\n\nYou may also load the helper to use the `user_id()` convenience method as outlined in the\n[CodeIgniter authentication guidelines](https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html).\n\n```php\nhelper('auth');\n\nif ($userId = user_id()) {\n    return true;\n}\n\nthrow new RuntimeException('You must be authenticated!');\n```\n\n## Users\n\n`Imposter` comes with a minimal set of classes to be fully compatible with\n[Tatter\\Users](https://github.com/tattersoftware/codeigniter4-users). This means that any\nproject or library which uses the interfaces from `Tatter\\Users` can be tested using\n`Imposter` without the need of an actual authentication library or even a database.\n\n### User Entity\n\nThe `Tatter\\Imposter\\Entities\\User` entity class implements all three entity interfaces from\n`Tatter\\Users`: `UserEntity`, `HasGroup`, and `HasPermission`. Use it like any regular entity,\nexcept that the `$groups` and `$permissions` atributes are simple CSV casts for storing your\nentity relationships:\n```php\n$user = new \\Tatter\\Imposter\\Entities\\User();\n$user-\u003egroups = ['Administrators', 'Editors'];\n```\n\n### Imposter Factory\n\nThe `ImposterFactory` class allows `UserProvider` to use the `Imposter` classes automatically\nduring testing. To enable `ImposterFactory` add it to the list of providers during\nyour test's `setUp` or `setUpBeforeClass` phase:\n```php\n\u003c?php\n\nuse CodeIgniter\\Test\\CIUnitTestCase;\nuse Tatter\\Imposter\\Factories\\ImposterFactory;\nuse Tatter\\Users\\UserProvider;\n\nfinal class UserWidgetTest extends CIUnitTestCase\n{\n    public static function setUpBeforeClass(): void\n    {\n        UserProvider::addFactory(ImposterFactory::class, ImposterFactory::class);\n    }\n...\n```\n\nBecause `Imposter` is a database-free solution `UserFactory` has its own local storage for\n`User` entities. Use the static methods to manipulate the storage to stage your tests:\n\n* `index()` - Gets the current index of the store\n* `add(User $user)` - Adds a `Tatter\\Imposter\\Entities\\User` object to the store, returning the new index\n* `reset()` - Resets the store and the index\n* `fake()` - Returns a new `Tatter\\Imposter\\Entities\\User` object using Faker's generated data (note: not added to the store)\n\nFor example:\n```php\n\n    protected function setUp(): void\n    {\n        parent::setUp();\n\n        $user = ImposterFactory::fake();\n        $user-\u003epermissions = ['widgets.create'];\n        UserFactory::add($user);\n\n        $this-\u003euserId = ImposterFactory::index();\n    }\n\n    protected function tearDown(): void\n    {\n        parent::tearDown();\n\n        ImposterFactory::reset();\n    }\n\n    public testUserCanCreateWidget()\n    {\n        $user = service('users')-\u003efindById($this-\u003euserId);\n        service('auth')-\u003elogin($user);\n        ...\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattersoftware%2Fcodeigniter4-imposter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftattersoftware%2Fcodeigniter4-imposter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftattersoftware%2Fcodeigniter4-imposter/lists"}