承接 whitedigital-eu/config-pack 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

whitedigital-eu/config-pack

Composer 安装命令:

composer require whitedigital-eu/config-pack

包简介

PhpCsFixer configuration to use in other organization repositories

README 文档

README

WARNING
When upgrading from v1 to v2, it is better to run composer update without scripts and plugins, as v2 does not require symfony/phpunit-bridge anymore and uninstalling it may change or delete test related files, like .env.test or phpunit.xml.dist:

composer update -nW --no-scripts --no-plugins whitedigital-eu/config-pack

Installation

composer require --dev whitedigital-eu/config-pack

Usage

PhpCsFixer:

vendor/bin/php-cs-fixer --config=vendor/whitedigital-eu/config-pack/.php-cs-fixer.php fix

In PhpStorm:

  • point to same config file in settings of PHP CS Fixer
  • check "allow risky rules" checkbox
  • choose custom ruleset
  • point to php-cs-fixer binary

PhpUnit:

vendor/bin/phpunit --log-junit report.xml --configuration vendor/whitedigital-eu/config-pack/phpunit.xml.dist tests

or if tests/bootstrap.php differs from one in whitedigital-eu/config-pack:

vendor/bin/phpunit --log-junit report.xml --configuration vendor/whitedigital-eu/config-pack/phpunit.xml.dist --bootstrap=tests/bootstrap.php tests

PhpStan

vendor/bin/phpstan --configuration=vendor/whitedigital-eu/config-pack/phpstan.neon.dist analyse src

In PhpStorm:

  • point to same config file in settings of PHPStan
  • point to your project's autoload, usually vendor/autoload.php
  • point to phpstan binary

Functional usage (since v2.2)

ConfigPack only works in dev and test environments, make sure you set this config only for dev and test.

/** config/bundles.php */
WhiteDigital\Config\ConfigPack::class => ['dev' => true, 'test' => true]

Faker

This package now comes with fakerphp/faker as a dependency thus making usage of fake data generation easier. If used without AbstractTestCase or AbstractFixture, you can easily use faker using FakerTrait.

use WhiteDigital\Config\Faker;
use WhiteDigital\Config\Traits\FakerTrait;

class Test 
{
    use FakerTrait;
    
    public function __construct(Faker $faker) 
    {
        self::setFaker($faker);
    }
    
    public function test(): string 
    {
        return self::text();
    }
}

As Faker is used with this library's configuration, setFaker function call is mandatory before using any faker functionality.
Faker is autowired automatically. FakerTrait primarily passes through functions from faker factory, most useful methods are defined as methods in annotation so IDE can see them.
By default Faker uses lv_LV as locale and 2022 as seed. If different values are needed, you can configure them:

config_pack:
    seed: 123
    locale: en_US
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Config\ConfigPackConfig;

return static function (ConfigPackConfig $config, ContainerConfigurator $container): void {
    if (in_array($container->env(), ['dev', 'test', ], true)) {
        $config
            ->seed(123)
            ->locale('en_US');
    }
};

AbstractTestCase

As most of tests for testing api-platform apis are somewhat similar, AbstractTestCase defines a lot of useful functions to make testing apis a lot easier.
In overall, with using AbstractTestCase usual api test should look like this:

use WhiteDigital\Config\Test\AbstractTestCase;
use WhiteDigital\Config\Test\Traits;

class CustomerTest extends AbstractTestCase 
{
    use Traits\DeleteItem;
    use Traits\GetCollection;
    use Traits\GetItem;
    use Traits\GetItemNotFound;

    protected static string $iri = '/api/customers';
    
    public function testPostItem(): int
    {
        return self::post([
            'key1' => self::words(),
            'key2' => self::text(),
            'key3' => self::randomDecimal(),
        ])->id;
    }

    #[Depends('testPostItem')]
    public function testPatchItem(int $id): int
    {
        return self::patch($id, [
            'key3' => self::randomDigit(),
        ])->id;
    }
}

This tests:
GET: /api/customers/{id}
GET: /api/customer/{not_existing_id}
GET: /api/customers
POST: /api/customers
PATCH: /api/customers/{id}
DELETE: /api/customers/{id}

Tests by default uses authentication, you can configure login_email and login_password in config_pack configuration:

config_pack:
    login_email: test@test.com
    login_password: test
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Config\ConfigPackConfig;

return static function (ConfigPackConfig $config, ContainerConfigurator $container): void {
    if (in_array($container->env(), ['dev', 'test', ], true)) {
        $config
            ->loginEmail('test@test.com')
            ->loginPassword('test');
    }
};

AbstractFixture

Similarly to AbstractTestCase, AbstractFixture defines base functions for doctrine fixtures. It also uses FakerTrait to simplify data generation.

use Doctrine\Persistence\ObjectManager;
use WhiteDigital\Config\DataFixture\AbstractFixture;

class OneFixture extends AbstractFixture
{
    public function load(ObjectManager $manager): void
    {
        $fixture = (new One())
            ->setKey1(self::words())
            ->setKey2(self::rnadomDecimal());
            
        $manager->persist($fixture);
        $manager->flush();
        
        $this->reference($fixture);
    }
}

class TwoFixture extends AbstractFixture
{
    public function load(ObjectManager $manager): void
    {
        for($i = 0; $i < 10; $i++){
            $fixture = (new Two())
                ->setKey1(self::words())
                ->setKey2($this->getEntity(One::class));
                
            $manager->persist($fixture);
            $manager->flush();
            
            $this->reference($fixture, $i);
        }
    }
    
    public function getDependencies() : array
    {
        $dependencies = parent::getDependencies();
        $dependencies[] = OneFixture::class;
        
        return $dependencies;
    }
}

AbstractFixture defines some useful functions:

getEntity -> getEntity(Entity::class): returns entity object for given class if fixture, where this class is created, is added to dependencies and dependant fixture defines reference.

getImage and getFile: coupled with whitedigital-eu/storage-item-resource library, these functions return entity for StorageItem based on need -> image or text file.

reference: set current fixture to references to be used elsewhere where this fixture is a dependency

getClassifier: explicitly returns Classifier if project contains any classifiers and project Classifier fixture extends BaseClassifierFixture.

BaseClassifierFixture

If your project have any classifier logic, you can extend BaseClassifierFixture and it will make classifier creation easier.
You need to have an entity with at least this structure to make this work:

use Doctrine\ORM\Mapping\Entity;

#[Entity]
class Classifier
{
    private ?int $id = null;
    private ?string $value = null;
    private ?array $data = [];
    private ?ClassifierType $type = null;
}

And you need to have a Backed enum (here called ClassifierType).
Example of ClassifierType is something like this:

enum ClassifierType: string
{
    case ONE = 'ONE';
    case TWO = 'TWO';
}

With BaseClassifierFixture classifiers can be created using two ways:
simple:

use WhiteDigital\Config\DataFixture\BaseClassifierFixture;

class ClassifierFixture extends BaseClassifierFixture
{
    public function __construct() 
    {
        parent::__construct(Classifier::class);
    }
    
    public function load(ObjectManager $manager): void
    {
        $classifiers = [
            'ONE' => ClassifierType::ONE,
            'TWO' => ClassifierType::TWO;
        ];

        $this->loadFixtures($manager, $classifiers);
    }
}

with data:

use WhiteDigital\Config\DataFixture\BaseClassifierFixture;

class ClassifierFixture extends BaseClassifierFixture
{
    public function __construct() 
    {
        parent::__construct(Classifier::class);
    }
    
    public function load(ObjectManager $manager): void
    {
        $classifiers = [
            'ONE' => ['classifier' => ClassifierType::ONE, 'data' => ['data' => 'one']],
            'TWO' => ['classifier' => ClassifierType::TWO, 'data' => ['data' => 'two']],
        ];

        $this->loadFixtures($manager, $classifiers);
    }
}

dependant:

use WhiteDigital\Config\DataFixture\BaseClassifierFixture;

class ClassifierFixture extends BaseClassifierFixture
{
    public function __construct() 
    {
        parent::__construct(Classifier::class);
    }
    
    public function load(ObjectManager $manager): void
    {
        $classifiers = [
            'ONE' => ClassifierType::ONE,
        ];

        $this->loadFixtures($manager, $classifiers);

        $dependants = [
            'TWO' => ['classifier' => ClassifierType::TWO, 'data' => ['one_iri' => '/api/classifiers/' . $this->getClassifier(ClassifierType::ONE)->getId(), ], ],
        ];

        $this->loadFixtures($manager, $dependants);
    }
}

whitedigital-eu/config-pack 适用场景与选型建议

whitedigital-eu/config-pack 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.16k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2022 年 12 月 02 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「symfony」 「php」 「config」 「php-cs-fixer」 「cs-fixer」 「PHPStan」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 whitedigital-eu/config-pack 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 whitedigital-eu/config-pack 我们能提供哪些服务?
定制开发 / 二次开发

基于 whitedigital-eu/config-pack 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 1.16k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 6
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-12-02