public function testInvalidCallback() { try { $callback_name = null; do { $callback = 'invalidfunction' . md5(rand(0, 100000)); } while (ShutdownHandler::isCallable($callback, false, $callback_name)); $handler = new ShutdownHandler($callback, array()); $handler->run(); $this->assertTrue(false); } catch (\RuntimeException $e) { $this->assertEquals("Callback: '{$callback_name}' is not callable", $e->getMessage()); } }
/** * Test re-registration of keyed shutdown handler. * * @depends testRegister * @depends testReRegister * @depends testShutdown */ public function testReRegisterKey() { self::$testVariable = 0; $handler = new ShutdownHandler(array(get_class($this), 'shutdown'), array(), 'testkey'); $handler->reRegister('testkey'); ShutdownHandler::shutdown(); $this->assertSame(1, self::$testVariable); self::$testVariable = 0; $handler1 = new ShutdownHandler(array(get_class($this), 'shutdown'), array(), 'testkey1'); $handler2 = new ShutdownHandler(array(get_class($this), 'shutdown'), array(), 'testkey2'); $handler2->reRegister('testkey1'); ShutdownHandler::shutdown(); $this->assertSame(1, self::$testVariable); }
/** * Run our shutdown handler upon object destruction. */ public function __destruct() { $this->shutdown->run(); }
<?php namespace Gielfeldt\ShutdownHandler\Example; require 'vendor/autoload.php'; use Gielfeldt\ShutdownHandler\ShutdownHandler; /** * Simple shutdown handler callback. * * @param string $message * Message to display during shutdown. */ function myshutdownhandler($message = '') { echo "Goodbye {$message}\n"; } // Register shutdown handler to be run during PHP shutdown phase. $handler = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('cruel world')); echo "Hello world\n"; // Register shutdown handler. $handler2 = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('for now')); // Don't wait for shutdown phase, just run now. $handler2->run();
<?php namespace Gielfeldt\ShutdownHandler\Example; require 'vendor/autoload.php'; use Gielfeldt\ShutdownHandler\ShutdownHandler; /** * Simple shutdown handler callback. * * @param string $message * Message to display during shutdown. */ function myshutdownhandler($message = '') { echo "Goodbye {$message}\n"; } // Register shutdown handler to be run during PHP shutdown phase. $handler = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('cruel world')); echo "Hello world\n"; // Register shutdown handler. $handler2 = new ShutdownHandler('\\Gielfeldt\\ShutdownHandler\\Example\\myshutdownhandler', array('for now')); // Don't run the first shutdown handler anyways. $handler->unRegister();