การทำ Unit Testing บน Joomla 3.3 ด้วย PHPUnit กับ Netbeans 8.0

Software testing ความรู้ สำหรับ Tester ผู้ทดสอบ เป็นส่วนสำคัญในการ test โปรแกรม การ ทดสอบโปรแกรมมีความรู้แนะนำไว้ในหมวดนี้

Moderator: mindphp, ผู้ดูแลกระดาน

ภาพประจำตัวสมาชิก
tsukasaz
PHP VIP Members
PHP VIP Members
โพสต์: 21997
ลงทะเบียนเมื่อ: 18/04/2012 9:39 am

การทำ Unit Testing บน Joomla 3.3 ด้วย PHPUnit กับ Netbeans 8.0

โพสต์ที่ยังไม่ได้อ่าน โดย tsukasaz »

ศึกษาการใช้งานเบื้องต้นจากกระทู้นี้ก่อนนะครับ การทำ 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
joomla-phpunit-netbeans01.png (78.97 KiB) Viewed 1379 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
joomla-phpunit-netbeans02.png (95.26 KiB) Viewed 1379 times
The last bug isn't fixed until the last user is dead. (Sidney Markowitz, 1995)
  • Similar Topics
    ตอบกลับ
    แสดง
    โพสต์ล่าสุด

ผู้ใช้งานขณะนี้

สมาชิกกำลังดูบอร์ดนี้: ไม่มีสมาชิกใหม่ และบุคลทั่วไป 68