PHPackages                             tatter/imposter - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Testing &amp; Quality](/categories/testing)
4. /
5. tatter/imposter

ActiveLibrary[Testing &amp; Quality](/categories/testing)

tatter/imposter
===============

Mock authentication for CodeIgniter 4

v1.1.1(4y ago)23.1k↓45.8%2[1 issues](https://github.com/tattersoftware/codeigniter4-imposter/issues)[1 PRs](https://github.com/tattersoftware/codeigniter4-imposter/pulls)2MITPHPPHP ^7.4 || ^8.0

Since Nov 17Pushed 2y ago1 watchersCompare

[ Source](https://github.com/tattersoftware/codeigniter4-imposter)[ Packagist](https://packagist.org/packages/tatter/imposter)[ Docs](https://github.com/tattersoftware/codeigniter4-imposter)[ Fund](https://paypal.me/tatter)[ GitHub Sponsors](https://github.com/tattersoftware)[ RSS](/packages/tatter-imposter/feed)WikiDiscussions develop Synced 1mo ago

READMEChangelog (3)Dependencies (3)Versions (7)Used By (2)

Tatter\\Imposter
================

[](#tatterimposter)

Mock authentication for CodeIgniter 4

[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/PHPUnit/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/test.yml)[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/PHPStan/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/analyze.yml)[![](https://github.com/tattersoftware/codeigniter4-imposter/workflows/Deptrac/badge.svg)](https://github.com/tattersoftware/codeigniter4-imposter/actions/workflows/inspect.yml)[![Coverage Status](https://camo.githubusercontent.com/3b653cfbc7e146289652d92fc353b90f140389368c7c5ccd74942cffa32a124b/68747470733a2f2f636f766572616c6c732e696f2f7265706f732f6769746875622f746174746572736f6674776172652f636f646569676e69746572342d696d706f737465722f62616467652e7376673f6272616e63683d646576656c6f70)](https://coveralls.io/github/tattersoftware/codeigniter4-imposter?branch=develop)

Quick Start
-----------

[](#quick-start)

1. Install with Composer: `> composer require --dev tatter/imposter`
2. Access via the service: `$user = service('auth')->user();`

Description
-----------

[](#description)

`Imposter` provides a thin authentication layer for testing your CodeIgniter apps that require authentication. This library is for **testing purposes only**. The tiny footprint and easy-to-use interfaces make it ideal for handling mock authentication in a rapid way.

`Imposter` fulfills all the [CodeIgniter authentication guidelines](https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html)and thus supplies the Composer provision for `codeigniter4/authentication-implementation`.

Installation
------------

[](#installation)

Install easily via Composer to take advantage of CodeIgniter 4's autoloading capabilities and always be up-to-date:

- `> composer require tatter/imposter`

Or, install manually by downloading the source files and adding the directory to `app/Config/Autoload.php`.

Usage
-----

[](#usage)

Use the service to log a user in or out.

```
service('auth')->login($userId);
service('auth')->logout();
```

The current status can be checked by getting the ID or User; both will be `null` if no user is authenticated.

```
if ($user = service('auth')->user()) {
    echo 'Logged in!';
}
```

You may also load the helper to use the `user_id()` convenience method as outlined in the [CodeIgniter authentication guidelines](https://codeigniter4.github.io/CodeIgniter4/extending/authentication.html).

```
helper('auth');

if ($userId = user_id()) {
    return true;
}

throw new RuntimeException('You must be authenticated!');
```

Users
-----

[](#users)

`Imposter` comes with a minimal set of classes to be fully compatible with [Tatter\\Users](https://github.com/tattersoftware/codeigniter4-users). This means that any project or library which uses the interfaces from `Tatter\Users` can be tested using `Imposter` without the need of an actual authentication library or even a database.

### User Entity

[](#user-entity)

The `Tatter\Imposter\Entities\User` entity class implements all three entity interfaces from `Tatter\Users`: `UserEntity`, `HasGroup`, and `HasPermission`. Use it like any regular entity, except that the `$groups` and `$permissions` atributes are simple CSV casts for storing your entity relationships:

```
$user = new \Tatter\Imposter\Entities\User();
$user->groups = ['Administrators', 'Editors'];
```

### Imposter Factory

[](#imposter-factory)

The `ImposterFactory` class allows `UserProvider` to use the `Imposter` classes automatically during testing. To enable `ImposterFactory` add it to the list of providers during your test's `setUp` or `setUpBeforeClass` phase:

```
