ทำความรู้จัก Test Bootstrap Script ใน การทำ testting ของ Yii Framework กันครับ
ไฟล์
protected/tests/bootstrap.phpไฟล์นี้จะถูกเรียกให้ทำงานเมื่อเรากำหนด YII_DEBUG เป็น true ใน Entry Script
defined('YII_DEBUG') or define('YII_DEBUG',true);
รายละเอียดไฟล์ bootstrap.php
<?php
// change the following paths if necessary
$yiit=dirname(__FILE__).'/../../../../framework/yiit.php';
$config=dirname(__FILE__).'/../config/test.php';
require_once($yiit);
require_once(dirname(__FILE__).'/WebTestCase.php');
Yii::createWebApplication($config);
?>เริ่มต้นด้วยการ include yiit.php จาก Yii framework เข้ามาเพื่อใช้งาน global constants และ test base classes
เมื่อเราสร้าง Web application จากเครื่องมือของ Yii แล้วนอกจากไฟล์ protected/config/main.php แล้วจะได้ ไฟล์ protected/config/test.php ด้วย ถ้าเข้าไปดูในไฟล์นี้จะเห็นว่าจะเรียกใช้ main.php ซึ่งเป็น config ไฟล์ด้วยโดยรวม เราต้องการใช้ความสามารถ fixture ต้องเพิ่มส่วน config นี้คงไฟตามตัวอย่าง
<?php
return CMap::mergeArray(
require(dirname(__FILE__).'/main.php'),
array(
'components'=>array(
'fixture'=>array(
'class'=>'system.test.CDbFixtureManager',
),
'db'=>array(
'connectionString'=>'sqlite:'.dirname(__FILE__).'/../data/blog-test.db',
),
// uncomment the following to use a MySQL database
/*
'db'=>array(
'connectionString' => 'mysql:host=localhost;dbname=blog-test',
'emulatePrepare' => true,
'username' => 'root',
'password' => '',
'charset' => 'utf8',
),
*/
),
)
);
นอกจากนี้แล้วถ้าเราต้องการทดสอบโปรแกรมที่เกี่ยวข้องกับฐานข้อมูลด้วย ให้เราสร้างฐานข้อมูลเพื่อทดสอบขึ้นมาใหม่อีกหนึ่งก่อน และ กำหนด connectionstring ใหม่ ลงไปในไฟล์ protected/config/test.php นี้
การกำหนดค่าให้กับเพื่อทดสอบสำหรับ fixture อ่านได้ที่นี่