golossus/php-test-fixture-loading
Composer 安装命令:
composer require --dev golossus/php-test-fixture-loading
包简介
A package which provides an improved way of loading fixtures in tests
README 文档
README
php-test-fixture-loading is a package which tries to ease in the process of loading testing fixtures to have a better maintainability and code reuse. With this package, (data) fixtures can be created as simple classes that can be joined to compose more complex testing scenarios.
Installation
composer require --dev golossus/php-test-fixture-loading
Usage
Configuration
Use the trait FixtureLoaderTrait on a base test class or TestCase. Those will typically inherit from a PHPUnit
test class, like the Symfony WebTestCase or similar.
Additionally, this trait has an abstract method buildFixture which should be implemented as well. This method is
responsible to adapt the way data fixtures are instantiated, because different projects might follow different
approaches. This is specially true when a Dependency Injection container is needed to build a fixture.
As an example take the following TestCase class which uses a Symfony 4 WebTestCase. Take a look at the trait and
the method:
<?php declare(strict_types = 1); namespace App\Tests\Rest; use Golossus\TestFixtureLoading\Fixture; use Golossus\TestFixtureLoading\FixtureLoaderTrait; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class FunctionalTestCase extends WebTestCase { use FixtureLoaderTrait; protected function buildFixture(string $namespace): Fixture { return self::$container->get($namespace); } }
Your case might be more complex (or maybe simpler), so implement this method to cover your needs.
For Symfony applications like in the example above don't forget to declare the fixtures folder as services, or you won't be able to build the fixtures.
Although the example uses the testing container to build the data fixtures, so in practice you're allowed to build private services (which is the default behaviour), actually it's possible that you need to make the fixtures public. Look at the following example:
# in cofig/services_test.yaml services: _defaults: autowire: true autoconfigure: true App\Tests\DataFixtures\: resource: '../tests/DataFixtures/*' public: true
Creating a fixture class
To create a fixture is so easy as creating a new class which implement the Fixture interface. This interface only
provides two methods: load and depends. The first one defines the logic to create the data and provides
a FixtureRepository parameter to store a reference on memory of any object created (and maybe stored in the
database) which can be retrieved afterwards on the test function. The second method is useful to define which other data
fixtures should be loaded before the current one.
All dependencies of a loaded fixture will be also loaded even if they are not specified directly in the list of fixtures to load.
If any fixture declares a dependency graph which produces a cycle, will throw an exception during the loading phase.
Even if some dependency is used more than once it will actually load just one time.
As an example of fixture:
<?php declare(strict_types = 1); namespace Tests\Fixtures; use Golossus\TestFixtureLoading\AbstractFixture; use Golossus\TestFixtureLoading\FixtureRepository; ... class AdminUserFixture extends AbstractFixture { ... const ADMIN_USER = 'admin-user'; public function load(FixtureRepository $fixtureRepository): void { $company = $fixtureRepository->get(CompanyFixture::COMPANY); $user = $this->createAdminUserForCompany($company); $fixtureRepository->set(self::ADMIN_USER, $user); } public function depends(): array { return [ CompanyFixture::class, ]; } }
Loading fixtures
Once the configuration above has been completed, the usage is quite straightforward, just use the method loadFixtures
provided in the previous trait to load any desired fixture. This method returns a special fixture repository which is
used during loading to store object references on demand.
public function testWhateverYouLike() { // First load the required data fixtures $fixtures = $this->loadFixtures([ SomeDataFixture::class, AnotherDataFixture::class, ]); // You can get a data fixture object by key $dummy = $fixtures->get('some-key'); // The rest should be a normal test }
Community
- Join our Slack to meet the community and get support.
- Follow us on GitHub.
- Read our Code of Conduct.
Contributing
This is an Open Source project. The Golossus team wants to enable it to be community-driven and open to contributors. Take a look at contributing documentation.
Security Issues
If you discover a security vulnerability, please follow our disclosure procedure.
About Us
This package development is led by the Golossus Team Leaders and supported by contributors.
golossus/php-test-fixture-loading 适用场景与选型建议
golossus/php-test-fixture-loading 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.21k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2021 年 01 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「php」 「testing」 「Fixture」 「loading」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 golossus/php-test-fixture-loading 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 golossus/php-test-fixture-loading 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 golossus/php-test-fixture-loading 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Behat redis context for testing
Testing Suite For Lumen like Laravel does.
Fixtures replacement with a straightforward definition syntax
Fixtures documentation generator
Yii2 library to run fixtures the ActiveRecord Dao
Symfony bundle to manage fixtures with Alice and Faker.
统计信息
- 总下载量: 2.21k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 3
- 点击次数: 12
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-01-13