开发者

how do php's declare(ticks) really work?

开发者 https://www.devze.com 2022-12-14 11:12 出处:网络
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 t开发者_Python百科he signalclass works (where im only u

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 t开发者_Python百科he 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


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

  • Tick functions
  • Ticks
0

精彩评论

暂无评论...
验证码 换一张
取 消