สำหรับตัวอย่างการใช้งานจะทดสอบการดึงข้อมูลผู้ใช้ และการบันทึกข้อมูลผู้ใช้
ก่อนอื่นเลยสร้างไฟล์ 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';
โค้ด: เลือกทั้งหมด
<?xml version="1.0"?>
<phpunit bootstrap="bootstrap.php"
backupGlobals="false"
processIsolation="true"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
stopOnFailure="true">
</phpunit>
โค้ด: เลือกทั้งหมด
<?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);
}
}
โค้ด: เลือกทั้งหมด
<?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