ศึกษาการใช้งานเบื้องต้นจากกระทู้นี้ก่อนนะครับ
การทำ Unit Testing ด้วย PHPUnit กับ Netbeans 8.0
สำหรับตัวอย่างการใช้งานจะทดสอบการดึงข้อมูลผู้ใช้ และการบันทึกข้อมูลผู้ใช้
ก่อนอื่นเลยสร้างไฟล์ bootstrap.php ในโฟลเดอร์ Tests เพื่อเรียกไฟล์ที่ต้องใช้ในระบบ Joomla เข้ามาในส่วนของ Tests ด้วย
โค้ด: เลือกทั้งหมด
<?php
define('_JEXEC', 1);
if (file_exists(__DIR__ . '/../defines.php'))
{
include_once __DIR__ . '/../defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__ . '/../administrator');
require_once JPATH_BASE . '/../administrator/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_BASE . '/includes/helper.php';
require_once JPATH_BASE . '/includes/toolbar.php';
$app = JFactory::getApplication('administrator');
require_once JPATH_BASE . '/components/com_users/models/user.php';
require_once JPATH_BASE . '/components/com_users/models/users.php';
และไฟล์ configuration.xml หรือบางที่จะใช้ชื่อไฟล์เป็น phpunit.xml.dist
โค้ด: เลือกทั้งหมด
<?xml version="1.0"?>
<phpunit bootstrap="bootstrap.php"
backupGlobals="false"
processIsolation="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
</phpunit>
โครงสร้างไฟล์ในโฟลเดอร์ Tests
- joomla-phpunit-netbeans01.png (78.97 KiB) Viewed 1401 times
โดยไฟล์ users.php จะใช้ทดสอบดึงข้อมูลผู้ใช้
โค้ด: เลือกทั้งหมด
<?php
class UsersModelUsersTest extends PHPUnit_Framework_TestCase
{
protected $users;
protected function setUp()
{
$session = JFactory::getSession();
$session->set('user', JUser::getInstance('admin'));
JFactory::getApplication()->input->post->set(JSession::getFormToken(),'1');
$this->users = new UsersModelUsers();
}
public function testGetItems()
{
$items = $this->users->getItems();
$this->assertArrayHasKey(0, $items);
}
}
ไฟล์ user.php จะใช้ทดสอบเพิ่มข้อมูลผู้ใช้
โค้ด: เลือกทั้งหมด
<?php
class UsersModelUserTest extends PHPUnit_Framework_TestCase
{
protected $user;
protected function setUp()
{
$session = JFactory::getSession();
$session->set('user', JUser::getInstance('admin'));
JFactory::getApplication()->input->post->set(JSession::getFormToken(),'1');
$this->user = new UsersModelUser();
}
public function testSave()
{
$db = JFactory::getDbo();
$db->transactionStart();
$data = array(
'id' => 0,
'name' => 'Tester',
'username' => 'test',
'password' => 'tester',
'password2' => 'tester',
'email' => '[email protected]',
'groups' => array([0] => 2)
);
$result = $this->user->save($data);
$this->assertTrue($result);
$db->transactionRollback();
}
}
ทดสอบไฟล์ users.php
- joomla-phpunit-netbeans02.png (95.26 KiB) Viewed 1401 times
ศึกษาการใช้งานเบื้องต้นจากกระทู้นี้ก่อนนะครับ [url=https://www.mindphp.com/forums/viewtopic.php?f=85&t=24869]การทำ Unit Testing ด้วย PHPUnit กับ Netbeans 8.0[/url]
สำหรับตัวอย่างการใช้งานจะทดสอบการดึงข้อมูลผู้ใช้ และการบันทึกข้อมูลผู้ใช้
ก่อนอื่นเลยสร้างไฟล์ bootstrap.php ในโฟลเดอร์ Tests เพื่อเรียกไฟล์ที่ต้องใช้ในระบบ Joomla เข้ามาในส่วนของ Tests ด้วย[code=php]<?php
define('_JEXEC', 1);
if (file_exists(__DIR__ . '/../defines.php'))
{
include_once __DIR__ . '/../defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', __DIR__ . '/../administrator');
require_once JPATH_BASE . '/../administrator/includes/defines.php';
}
require_once JPATH_BASE . '/includes/framework.php';
require_once JPATH_BASE . '/includes/helper.php';
require_once JPATH_BASE . '/includes/toolbar.php';
$app = JFactory::getApplication('administrator');
require_once JPATH_BASE . '/components/com_users/models/user.php';
require_once JPATH_BASE . '/components/com_users/models/users.php';[/code]
และไฟล์ configuration.xml หรือบางที่จะใช้ชื่อไฟล์เป็น phpunit.xml.dist[code]<?xml version="1.0"?>
<phpunit bootstrap="bootstrap.php"
backupGlobals="false"
processIsolation="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
</phpunit>[/code]
โครงสร้างไฟล์ในโฟลเดอร์ Tests[attachment=1]joomla-phpunit-netbeans01.png[/attachment]
โดยไฟล์ users.php จะใช้ทดสอบดึงข้อมูลผู้ใช้[code=php]<?php
class UsersModelUsersTest extends PHPUnit_Framework_TestCase
{
protected $users;
protected function setUp()
{
$session = JFactory::getSession();
$session->set('user', JUser::getInstance('admin'));
JFactory::getApplication()->input->post->set(JSession::getFormToken(),'1');
$this->users = new UsersModelUsers();
}
public function testGetItems()
{
$items = $this->users->getItems();
$this->assertArrayHasKey(0, $items);
}
}[/code]
ไฟล์ user.php จะใช้ทดสอบเพิ่มข้อมูลผู้ใช้[code=php]<?php
class UsersModelUserTest extends PHPUnit_Framework_TestCase
{
protected $user;
protected function setUp()
{
$session = JFactory::getSession();
$session->set('user', JUser::getInstance('admin'));
JFactory::getApplication()->input->post->set(JSession::getFormToken(),'1');
$this->user = new UsersModelUser();
}
public function testSave()
{
$db = JFactory::getDbo();
$db->transactionStart();
$data = array(
'id' => 0,
'name' => 'Tester',
'username' => 'test',
'password' => 'tester',
'password2' => 'tester',
'email' => '
[email protected]',
'groups' => array([0] => 2)
);
$result = $this->user->save($data);
$this->assertTrue($result);
$db->transactionRollback();
}
} [/code]
ทดสอบไฟล์ users.php[attachment=0]joomla-phpunit-netbeans02.png[/attachment]