PHP ဖွံ့ဖြိုးတိုးတက်မှုတွင် session_register_shutdown () function ကိုအသုံးပြုသည်။ ဤလုပ်ဆောင်မှုသည် PHP ၏အချို့သောအဟောင်းများဗားရှင်းအချို့ရှိခဲ့သော်လည်း, အထူးသဖြင့်အမွေကုဒ်ကုဒ်ကိုထိန်းသိမ်းထားသည့်အခြေအနေများတွင်သင်ယူနစ်ကိုစစ်ဆေးရန်လိုအပ်သည့်အခြေအနေများပြုလုပ်ရန်လိုအပ်သည့်အခြေအနေများတွင်ပါ 0 င်သည်။
Session_register_shutdown () သူ့ဟာသူ PHP session ကိုယန္တရားနှင့်ဆက်စပ်သောကြောင့်၎င်းသည်၎င်း၏ callback အပြုအမူကိုတိုက်ရိုက်စမ်းသပ်ရန်ခက်ခဲသည်။ ဤဆောင်းပါးသည်စမ်းသပ်ထားသောကုဒ်အဆောက်အအုံများကိုမည်သို့ရေးရမည်, PHP ၏ယူနစ်စစ်ဆေးခြင်းမူဘောင်ကိုထိရောက်သောစမ်းသပ်ခြင်းအတွက်ထိရောက်သောစစ်ဆေးမှုအတွက်သို့မဟုတ်အစားထိုးရန် PHP ၏ ယူနစ် စမ်းသပ်ခြင်းမူဘောင်ကိုအသုံးပြုပါမည်။
Session_register_shutdown () သည် PHP script execution အဆုံးတွင်မှတ်ပုံတင်ထားသော callback function ကိုခေါ်ဆိုလိမ့်မည်။ ပုံမှန်အသုံးပြုမှုမှာအောက်ပါအတိုင်းဖြစ်သည် -
<?php
session_register_shutdown(function() {
// session တစ်ခုပိတ်သောအခါယုတ္တိဗေဒထုတ်ယူခြင်း
file_put_contents('session_log.txt', 'Session closed at ' . date('Y-m-d H:i:s'));
});
?>
ဤ callback function ကိုတောင်းဆိုမှုအဆုံးတွင်အလိုအလျောက်ကွပ်မျက်ခံရလိမ့်မည်။
အမှန်တကယ်စီးပွားရေးယုတ္တိဗေဒကိုသီးခြားနည်းလမ်းဖြင့် encapsate လုပ်ရန်အကြံပြုသည်။ Session_register_shutdown () သည် callback ကိုမှတ်ပုံတင်ရန်သာတာဝန်ရှိသည်, စမ်းသပ်မှုကာလအတွင်းစီးပွားရေးယုတ္တိဗေဒနည်းလမ်းကိုသာခေါ်ဆိုခြင်းအတွက်သာတာဝန်ရှိသည်။
<?php
class SessionHandler
{
public function registerShutdown()
{
session_register_shutdown([$this, 'onSessionShutdown']);
}
public function onSessionShutdown()
{
// စီးပွားရေးယုတ္တိဗေဒ
file_put_contents('session_log.txt', 'Session closed at ' . date('Y-m-d H:i:s'));
}
}
?>
စမ်းသပ်ခြင်းတွင်ကျွန်ုပ်တို့သည် မှတ်ပုံတင်ခြင်း () ကိုပြန်လည် ခေါ်ဆိုခြင်း () ကို တိုက်ရိုက်စစ်ဆေးရန်မခေါ်ပါ။
ဥပမာစမ်းသပ်မှုများမှာအောက်ပါအတိုင်းဖြစ်သည် -
<?php
use PHPUnit\Framework\TestCase;
class SessionHandlerTest extends TestCase
{
protected $handler;
protected $logFile = __DIR__ . '/session_log.txt';
protected function setUp(): void
{
$this->handler = new SessionHandler();
// မှတ်တမ်းဖိုင်မတည်ရှိပါစေ
if (file_exists($this->logFile)) {
unlink($this->logFile);
}
}
public function testOnSessionShutdownCreatesLog()
{
$this->handler->onSessionShutdown();
$this->assertFileExists($this->logFile);
$content = file_get_contents($this->logFile);
$this->assertStringContainsString('Session closed at', $content);
}
protected function tearDown(): void
{
if (file_exists($this->logFile)) {
unlink($this->logFile);
}
}
}
?>
စီးပွားရေးဆိုင်ရာယုတ္တိဗေဒနည်းစနစ်များကိုတိုက်ရိုက်ခေါ်ဆိုခြင်းအားဖြင့်ပြန်ခေါ်သည့်ယန္တရားကြောင့်ဖြစ်ရတဲ့စမ်းသပ်မှုရှုပ်ထွေးမှုကိုရှောင်ရှားသည်။
သင်မှတ်ပုံတင်ထားသော function ကိုစစ်ဆေးရန်သေချာစေရန်သင်သေချာစွာစစ်ဆေးရန်လိုပါက function override သို့မဟုတ် mock နည်းပညာကိုသုံးနိုင်သည်။ သို့သော် Session_register_shutdown နည်းပညာကိုသင်အသုံးပြုနိုင်သည်။ အကြံပြုထားသောအခြားနည်းလမ်းများမှာ
မှတ်ပုံတင်ထားသောလုပ်ဆောင်မှုကို Outscied function များကိုအခြားရွေးချယ်စရာမှီခိုသို့၎င်း,
ထုတ်လုပ်မှုပတ် 0 န်းကျင်ရှိစစ်မှန်သောလုပ်ဆောင်မှုကိုခေါ်ပါ။ စစ်ဆေးမှုပတ်ဝန်းကျင်တွင်အကောင်အထည်ဖော်မှုဖြင့်အစားထိုးပါ။
ဥပမာ -
<?php
class SessionHandler
{
private $registerShutdownCallback;
public function __construct(callable $registerShutdownCallback = null)
{
$this->registerShutdownCallback = $registerShutdownCallback ?: 'session_register_shutdown';
}
public function registerShutdown()
{
call_user_func($this->registerShutdownCallback, [$this, 'onSessionShutdown']);
}
public function onSessionShutdown()
{
// စီးပွားရေးယုတ္တိဗေဒ
}
}
?>
ဘေးထွက်ဆိုးကျိုးများကိုရှောင်ရှားရန်စမ်းသပ်မှုကာလအတွင်း session_register_shutdown ကို အစားထိုးရန်စိတ်ကြိုက်ပြန်လည်ခေါ်ဆိုမှုကိုဖြတ်ပါ။
Session_register_shutdown () ၏ callback Execute ကိုတိုက်ရိုက်စစ်ဆေးရန်မထောက်ခံပါ။
စီးပွားရေးယုတ္တိဗေဒကိုခွဲပြီး callback function ကိုသီးခြားစီစစ်ဆေးပါ။
Testing ပြောင်းလွယ်ပြင်လွယ်မှုကိုတိုးမြှင့်ခြင်းဖြင့်တည်ဆောက်မှုထိုးသွင်းခြင်းဖြင့်အစားထိုးခြင်းဖြင့်အစားထိုးခြင်းဖြင့်တိုးတက်အောင်လုပ်ပါ။
စီးပွားရေးယုတ္တိဗေဒဆိုင်ရာမှန်ကန်မှုကိုအတည်ပြုရန် Phpunit နှင့်အခြားစမ်းသပ်ခြင်းမူဘောင်များကိုသုံးပါ။
အထက်ပါနည်းလမ်းကိုအသုံးပြုခြင်းအားဖြင့် session_register_shutdown () ပါ 0 င်သည့်ကုဒ်သည်ထိရောက်စွာစစ်ဆေးပြီး,
<?php
class SessionHandler
{
private $registerShutdownCallback;
public function __construct(callable $registerShutdownCallback = null)
{
$this->registerShutdownCallback = $registerShutdownCallback ?: 'session_register_shutdown';
}
public function registerShutdown()
{
call_user_func($this->registerShutdownCallback, [$this, 'onSessionShutdown']);
}
public function onSessionShutdown()
{
// မှတ်တမ်းရေးသားခြင်းကိုတုပပါ
file_put_contents('https://m66.net/session_log.txt', 'Session closed at ' . date('Y-m-d H:i:s'));
}
}
// ထုတ်လုပ်မှုပတ်ဝန်းကျင်ခေါ်ဆိုမှု
$handler = new SessionHandler();
$handler->registerShutdown();
?>
<?php
use PHPUnit\Framework\TestCase;
class SessionHandlerTest extends TestCase
{
protected $handler;
protected $logFile = __DIR__ . '/session_log.txt';
protected function setUp(): void
{
$this->handler = new SessionHandler(function ($callback) {
// Simulation မည်သည့်စစ်ဆင်ရေးကိုမလုပ်ဆောင်ဘူး,ဘေးထွက်ဆိုးကျိုးများကိုရှောင်ပါ
});
if (file_exists($this->logFile)) {
unlink($this->logFile);
}
}
public function testOnSessionShutdownCreatesLog()
{
$this->handler->onSessionShutdown();
$this->assertFileExists('https://m66.net/session_log.txt');
$content = file_get_contents('https://m66.net/session_log.txt');
$this->assertStringContainsString('Session closed at', $content);
}
protected function tearDown(): void
{
if (file_exists('https://m66.net/session_log.txt')) {
unlink('https://m66.net/session_log.txt');
}
}
}
?>