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

ตอบกระทู้

รูปแสดงอารมณ์
:icon_plusone: :like: :plusone: :gfb: :-D :) :( :-o 8O :? 8) :lol: :x :P :oops: :cry: :evil: :twisted: :roll: :wink: :!: :?: :idea: :arrow: :| :mrgreen: :angry: :baa: :biggrin:
รูปแสดงอารมณ์อื่นๆ

BBCode เปิด
[img] เปิด
[url] เปิด
[Smile icon] เปิด

กระทู้แนะนำ
   

มุมมองที่ขยายได้ กระทู้แนะนำ: การทำ Unit Testing บน Joomla 3.3 ด้วย PHPUnit กับ Netbeans 8.0

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

โดย tsukasaz » 28/11/2014 4:33 pm

ศึกษาการใช้งานเบื้องต้นจากกระทู้นี้ก่อนนะครับ การทำ 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 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
joomla-phpunit-netbeans02.png (95.26 KiB) Viewed 1401 times

ข้างบน