1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Unit Tests

Discussion in 'XenForo Development Discussions' started by ragtek, Jan 13, 2011.

  1. ragtek

    ragtek Guest

    Anybody using phpunit tests with xenforo?

    I'm creating selenium tests for my add-ons, but some phpunit tests would also be cool.
    I have no clue if/how it would be possible to use phpunit with the proxyclasses,etc...

    I played little bit, and tried to implement a bootsrapfile which runs the framework stuff, but i had no luck with it^^
     
  2. Shadab

    Shadab Well-Known Member

    I've been using phpUnit for testing some of my xenforo-based components. (not addons, though).
    These are the settings I use:

    phpunit.xml
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    
    <phpunit backupGlobals="false"
             backupStaticAttributes="true"
             colors="false"
             convertErrorsToExceptions="true"
             convertNoticesToExceptions="true"
             convertWarningsToExceptions="true"
             processIsolation="false"
             stopOnFailure="false"
             syntaxCheck="false"
             verbose="true"
             strict="true"
             bootstrap="initialize.php"
    >
    
    	<testsuites>
    		<testsuite name="Your Test Suite">
    			<directory>unit</directory>
    		</testsuite>
    	</testsuites>
    
    </phpunit>
    initialize.php
    PHP:
    <?php

    $xfLibrary 
    '/path/to/xenforo/src/upload/library';

    // Initialize the xenforo autoloader
    require_once($xfLibrary '/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($xfLibrary);

    // utf-8 helper functions
    require_once($xfLibrary '/Lgpl/utf8.php');

    unset(
    $xfLibrary);

    Haven't tested any of my dynamically extended classes using phpUnit, but I think you can get it to work by creating the base proxy classes yourself. Like for example:
    PHP:
    class XFCP_Addon1_ControllerPublic_Account extends XenForo_ControllerPublic_Account {}

    class 
    XFCP_Addon2_Model_Thread extends XenForo_Model_Thread {}
     
    thedude and (deleted member) like this.
  3. Naatan

    Naatan Well-Known Member

    Tried the initialize.php shared here to no avail, seems some things have changed since it was posted. I got it working with some small tweaks, hope this saves others some time:

    PHP:
    <?php
     
    // Assuming you have a 'tests' directory in your XF install
    $xfLibrary dirname(__FILE__) . '/../library/';
     
    // Initialize the xenforo autoloader
    require_once($xfLibrary 'XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($xfLibrary);
     
    // Initialize XenForo App
    XenForo_Application::initialize($xfLibrary$xfLibrary);
     
    unset(
    $xfLibrary);
     
    Earl and simbolo like this.
  4. Earl

    Earl Member

    is that code still valid for the latest version? (1.5.5)
    according to @Kier 's docs "XenForo_Application::initialize"s second parameter is root directory right?
    PHP:
        /**
        * Helper function to initialize the application.
        *
        * @param string Path to application configuration directory. See {@link $_configDir}.
        * @param string Path to application root directory. See {@link $_rootDir}.
        * @param boolean True to load default data (config, DB, etc)
        * @param array Changes to the initialization process
        */
    I just wanted to make sure the 2nd parameter is correct.
    is it the same directory ($xfLibrary)? or is it "/../" (in case you are assuming you have a 'tests' directory in your XF install)
    I'm just asking this because I found this code in index.php and its pointing it to root directory.
    PHP:
    <?php

    $startTime 
    microtime(true);
    $fileDir dirname(__FILE__);

    require(
    $fileDir '/library/XenForo/Autoloader.php');
    XenForo_Autoloader::getInstance()->setupAutoloader($fileDir '/library');

    XenForo_Application::initialize($fileDir '/library'$fileDir);
    XenForo_Application::set('page_start_time'$startTime);

    $fc = new XenForo_FrontController(new XenForo_Dependencies_Public());
    $fc->run();
    Anyone can verify this is correct? :unsure:
     
    Last edited: Jan 15, 2016
  5. Earl

    Earl Member

    I did as you say but I'm getting an error, please help :(

    PHP:
    <?php

    class XFCP_MyAddon_ControllerPublic_InlineMod_Post extends XenForo_ControllerPublic_InlineMod_Post {}

    class 
    XFCP_MyAddon_ControllerPublic_InlineMod_PostTest extends PHPUnit_Framework_TestCase
    {
        public function 
    testdeletePost()
        {

            
    $controller = new XFCP_MyAddon_ControllerPublic_InlineMod_Post;

            
    $this->assertEquals('test''test');
        
        }
    }
    Argument 1 passed to XenForo_Controller::__construct() must be an instance of Zend_Controller_Request_Http, none given, called in /var/www/html/tests/tests/XFCP_MyAddon_ControllerPublic_InlineMod_PostTest.php on line 16 and defined
     

Share This Page