tatter/imposter
Composer 安装命令:
composer require tatter/imposter
包简介
Mock authentication for CodeIgniter 4
README 文档
README
Mock authentication for CodeIgniter 4
Quick Start
- Install with Composer:
> composer require --dev tatter/imposter - Access via the service:
$user = service('auth')->user();
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
and thus supplies the Composer provision for codeigniter4/authentication-implementation.
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
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.
helper('auth'); if ($userId = user_id()) { return true; } throw new RuntimeException('You must be authenticated!');
Users
Imposter comes with a minimal set of classes to be fully compatible with
Tatter\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
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
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:
<?php use CodeIgniter\Test\CIUnitTestCase; use Tatter\Imposter\Factories\ImposterFactory; use Tatter\Users\UserProvider; final class UserWidgetTest extends CIUnitTestCase { public static function setUpBeforeClass(): void { UserProvider::addFactory(ImposterFactory::class, ImposterFactory::class); } ...
Because Imposter is a database-free solution UserFactory has its own local storage for
User entities. Use the static methods to manipulate the storage to stage your tests:
index()- Gets the current index of the storeadd(User $user)- Adds aTatter\Imposter\Entities\Userobject to the store, returning the new indexreset()- Resets the store and the indexfake()- Returns a newTatter\Imposter\Entities\Userobject using Faker's generated data (note: not added to the store)
For example:
protected function setUp(): void { parent::setUp(); $user = ImposterFactory::fake(); $user->permissions = ['widgets.create']; UserFactory::add($user); $this->userId = ImposterFactory::index(); } protected function tearDown(): void { parent::tearDown(); ImposterFactory::reset(); } public testUserCanCreateWidget() { $user = service('users')->findById($this->userId); service('auth')->login($user); ...
tatter/imposter 适用场景与选型建议
tatter/imposter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.28k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 11 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Authentication」 「mock」 「codeigniter」 「codeigniter4」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 tatter/imposter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 tatter/imposter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 tatter/imposter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
LazyPDO is a set of wrappers over PHP's standard PDO and PDOStatement classes. It enables lazy loading, serialization and decoration.
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
The CodeIgniter Redis package
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
Mock PSR-18 HTTP client
统计信息
- 总下载量: 3.28k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 2
- 点击次数: 9
- 依赖项目数: 8
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-11-17