定制 digitalrevolution/phpunit-extensions 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

digitalrevolution/phpunit-extensions

Composer 安装命令:

composer require digitalrevolution/phpunit-extensions

包简介

A library for phpunit utility and support classes

README 文档

README

Minimum PHP Version Run tests

PHPUnit extensions

Utility classes to make unit testing life easier.

Features

withConsecutive

In PHPUnit 10 withConsecutive method was removed. To still be able to migrate existing codebases a replacement method:

PHPUnit <= 9.5:

$mock->method('myMethod')->withConsecutive([123, 'foobar'], [456]);

PHPUnit >= 9.6:

$mock->method('myMethod')->with(...consecutive([123, 'foobar'], [456]));

Symfony controller tests

Testing a Symfony controller internally invokes the dependency container. A utility class to mock these classes more easily.

use DR\PHPUnitExtensions\Symfony\AbstractControllerTestCase;

class MyControllerTest extends AbstractControllerTestCase 
{    
    public function myTest(): void 
    {
        $this->expectDenyAccessUnlessGranted('attribute', null, true);
        $this->expectGetUser(new User());
        $this->expectCreateForm(TextType::class);
        
        ($this->controller)();    
    }
    
    public function getController() {
        return new MyController();    
    }    
}

Methods

  • expectGetUser
  • expectIsGranted
  • expectDenyAccessUnlessGranted
  • expectCreateForm
  • expectAddFlash
  • expectGenerateUrl
  • expectGenerateUrlWithConsecutive
  • expectRedirectToRoute
  • expectForward
  • expectRender

Symfony ConstraintValidator tests

TestCase for testing Symfony ConstraintValidators.

use DR\PHPUnitExtensions\Symfony\AbstractConstraintValidatorTestCase;

class MyConstraintValidatorTest extends AbstractConstraintValidatorTestCase 
{    
    public function testValidate(): void
    {
        $this->expectBuildViolation($constraint->message, ['parameter' => 123])
            ->expectSetCode(789)
            ->expectAtPath('path')
            ->expectAddViolation();

        $this->validator->validate(123, $this->constraint);
    }
    
    protected function getValidator(): ConstraintValidator
    {
        return new MyConstraintValidator();
    }

    protected function getConstraint(): Constraint
    {
        return new MyConstraint();
    }
}

Methods

  • assertHandlesIncorrectConstraintType
  • expectNoViolations
  • expectBuildViolation(): ConstraintViolationBuilderAssertion

ConstraintViolationBuilderAssertion

  • expectSetInvalidValue
  • expectSetPlural
  • expectSetCode
  • expectSetCause
  • expectSetTranslationDomain
  • expectSetParameters
  • expectSetParameter
  • expectSetParameterWithConsecutive
  • expectAtPath
  • expectAddViolation

ResponseAssertions trait

The ResponseAssertions trait provides a set of assertion methods designed to streamline the testing of Symfony HTTP responses. This trait includes methods for verifying the status code, response message content and specific types of responses such as JSON responses

Methods

  • assertJsonResponse
  • assertResponse
  • assertResponseIsSuccessful
  • assertResponseIsRedirection
  • assertResponseIsClientError
  • assertResponseIsServerError

ClockTestTrait

The ClockTestTrait provides a set of methods to manipulate the current time in tests. This trait will automatically freeze the time at the start of each test. The trait also provides methods to get the current time as timestamp or DateTimeImmutable object,

Methods

  • self::time(): int
  • self::now(): DateTimeImmutable
  • self::sleep(int|float $seconds): void
  • self::assertNow()
  • self::assertSameTime()

ImageTestTrait

The ImageTestTrait provides methods to compare two images by binary string, SplFileInfo or resource. (Requires Imagick). Internally Imagick::compareImages() is used to compare the images and therefore supports various image formats such as PNG, JPEG, GIF, PDF and more.

Methods

  • self::assertSameImage(string|SplFileInfo|resource, string|SplFileInfo|resource)
  • self::assertNotSameImage(string|SplFileInfo|resource, string|SplFileInfo|resource)

PdfTestTrait

The PdfTestTrait provides methods to compare a TCPDF object against an image of binary string, SplFileInfo, resource or another TCPDF file. (Requires Imagick with ghostscript and TCPDF)

Methods

  • self::assertSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF)
  • self::assertNotSamePdf(string|SplFileInfo|resource|TCPDF, TCPDF).

For both ImageTestTrait and PdfTestTrait:

The environment variable PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_PATH can be set to a directory where a diff.html will be generated which will show the differences. Optionally, the environment variable PHPUNIT_EXTENSIONS_IMAGE_DIFF_OUTPUT_URL can be set to inform the user where to find the outputted diff.html file. This is useful when running tests in a CI environment.

About us

At 123inkt (Part of Digital Revolution B.V.), every day more than 50 development professionals are working on improving our internal ERP and our several shops. Do you want to join us? We are looking for developers.

digitalrevolution/phpunit-extensions 适用场景与选型建议

digitalrevolution/phpunit-extensions 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 99.2k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2023 年 07 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 digitalrevolution/phpunit-extensions 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 99.2k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 19
  • 依赖项目数: 3
  • 推荐数: 0

GitHub 信息

  • Stars: 3
  • Watchers: 1
  • Forks: 2
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-07-06