php 的声明(ticks) 是如何工作的?

发布于 2024-08-15 11:00:29 字数 379 浏览 4 评论 0原文

我使用 pcntl_signal 创建了一个信号处理类,现在我想使用它 对于 sigalrm ,

我遇到的问题是我的 phpunit 测试用于测试信号类有效(其中我仅在信号类中使用声明刻度),但是 用于测试警报类的测试类,而使用信号类则不会 如果我在警报测试中添加声明(ticks=1),它也可以工作

我认为声明刻度仅在信号处理代码中需要,在我的情况下是在信号类中? 但据我所知,调用信号处理代码的代码也需要它 它甚至在我的警报类中不起作用,我必须将它放在我的警报测试类中!?

尽管使用 strace ,信号的传递与刻度无关,

所以任何人都明白为什么我必须在测试中使用声明()(有时)? 或者为什么我还需要在使用它的代码中声明(ticks=1)? 这意味着用户需要知道如何使用声明

i created a signal handling class using pcntl_signal which now i want to use
for sigalrm

the problem i have is that my phpunit test for testing the signalclass works (where im only using declare ticks in the signalclass), but
the testclass for testing the alarm class, which in turn using the signalclass doesnt
if i add declare(ticks=1) in my alarmtests it also works

i thought declare ticks is only needed at the signal handling code, which in my case is in the signalclass?
but as far as i can see it is also needed for the code who calls signal handling code
it doesnt even work in my alarmclass, i have to put it in my alarmtest class!?

altough using strace the signal is delivered independent of ticks

so anyone understands why i have to use declare() in my tests (sometimes)?
or why do i need to declare(ticks=1) also in the code which uses it?
it would mean a user needs to know how to use declare

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

水晶透心 2024-08-22 11:00:29

要在全局范围内使用刻度,您必须将其放在调用脚本的开头。这可能就是您必须重新声明它的原因。如果不知道你的代码,我不能肯定地说。下面是一些适用于单元测试的示例。

您可以通过以下构造在代码中声明您的刻度线

function tick_function() {
    // Do something
}

register_tick_function('tick_function');

declare (ticks=1) {
    // Your code here
}

或者作为工作示例

function profile_memory()
{
     echo '<!--' . memory_get_usage() . '-->';
}

register_tick_function('profile_memory');
declare (ticks=1)
{
     $pass = md5('qwerty'); /* Tick executed */
     $pass = strrev($pass);  /* Tick executed */
     echo $pass;  /* Tick executed */
}

这是在单元测试中工作的自包含刻度函数的工作示例

class TickTest {
    private function profile_memory() {
        static $i;
        ++$i;
        echo "Tick $i\n";
    }
    public function  __construct() {
    }
    public function doTicks() {
        $register_tick_function = register_tick_function(
                array($this,'profile_memory')
            );
        declare (ticks=1) {
            $pass = md5('qwerty'); /* Tick executed */
            $pass = strrev($pass);  /* Tick executed */
        }
    }
}

这是单元测试(是的我知道事实上,这不是一个真正的测试)

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ticks.php';
class TickTestTest extends PHPUnit_Framework_TestCase {
    protected $object;
    protected function setUp() {
        $this->object = new TickTest;
    }
    protected function tearDown() {
    }
    public function testDoTicks() {
        $this->object->doTicks();
    }
}

查看输出,执行单元测试时会调用 tick 函数。

一些参考

To use ticks in the global scope you have to have it in the beginning of the calling script. This is probably why you have to redeclare it. I cannot say for certain without knowing your code. Below are some examples that do work with unit testing.

You can IIRC declare youre ticks in your code by the following construct

function tick_function() {
    // Do something
}

register_tick_function('tick_function');

declare (ticks=1) {
    // Your code here
}

Or as a working example

function profile_memory()
{
     echo '<!--' . memory_get_usage() . '-->';
}

register_tick_function('profile_memory');
declare (ticks=1)
{
     $pass = md5('qwerty'); /* Tick executed */
     $pass = strrev($pass);  /* Tick executed */
     echo $pass;  /* Tick executed */
}

This is a working example of a self contained tick function that is working within a unit test

class TickTest {
    private function profile_memory() {
        static $i;
        ++$i;
        echo "Tick $i\n";
    }
    public function  __construct() {
    }
    public function doTicks() {
        $register_tick_function = register_tick_function(
                array($this,'profile_memory')
            );
        declare (ticks=1) {
            $pass = md5('qwerty'); /* Tick executed */
            $pass = strrev($pass);  /* Tick executed */
        }
    }
}

And this is the unit test (and yes i'm aware of the fact that it's not a real test)

require_once 'PHPUnit/Framework.php';
require_once dirname(__FILE__).'/../ticks.php';
class TickTestTest extends PHPUnit_Framework_TestCase {
    protected $object;
    protected function setUp() {
        $this->object = new TickTest;
    }
    protected function tearDown() {
    }
    public function testDoTicks() {
        $this->object->doTicks();
    }
}

Looking at the output the tick function is called when executing the unit test.

Some references

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文