定制 live627/phpunit-garbage-collector 二次开发

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

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

live627/phpunit-garbage-collector

最新稳定版本:1.0.0

Composer 安装命令:

composer require live627/phpunit-garbage-collector

包简介

Reset properties to save memory after each test

README 文档

README

Tests Scrutinizer Code Quality Code Coverage

PHPUnit seems to waste a lot of memory because it instantiates a lot of objects before actually starting any test, meaning that the initial memory usage varies depending on the number of tests available.

Say, for example, that you have UserTest with 1k test methods. This will unfortunately result in 1k UserTest class instances before any of the tests are actually run. This is actually by design. In order to determine the number of tests, PHPUnit runs @dataProvider methods before actually running the tests (and the setUp method).

The problem comes once all of the tests in the test case have finished. Objects in memory are closer than they appear because they aren't dereferenced until after the entire test suite has finished. Fortunately, this is on the radar.

Inspired by Kris Wallsmith faster PHPUnit article. The claim that this test listener that speeds up PHPUnit tests about 20% by freeing memory is dubious and probably is outdated now that PHP 7 has rewritten zvals to be a bit more compact.

Installation

To install this library, run the command below and you will get the latest version

composer require live627/phpunit-garbage-collector --dev

Usage

Just add to your phpunit.xml configuration

<phpunit>
    <listeners>
        <listener class="\live627\PHPUnitGarbageCollector\MemoryGuard"/>
    </listeners>
</phpunit>

Ignoring Tests

Sometimes it is necessary to ignore specific tests, where freeing their properties is undesired. For this use case, you have the ability to extend the behaviour of the listener by implementing the IgnoreTestPolicy interface.

As an example, if we hypothetically wanted to ignore all tests which include "Legacy" in their test filename, we could create a custom ignore policy as follows

<?php

use live627\PHPUnitGarbageCollector\IgnoreTestPolicy;

class IgnoreLegacyTestPolicy implements IgnoreTestPolicy {
    public function shouldIgnore(\ReflectionObject $testReflection): bool {
        return strpos($testReflection->getFileName(), 'Legacy') !== false;
    }
}

And pass it to the constructor of our test listener in phpunit.xml configuration

<phpunit>
    <listeners>
        <listener class="\live627\PHPUnitGarbageCollector\MemoryGuard">
            <arguments>
                <object class="\IgnoreLegacyTestPolicy"/>
            </arguments>
        </listener>
    </listeners>
</phpunit>

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-11-15

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固