Page 1 of 1

phpunit: my test doesn't return anything

Posted: Wed May 26, 2010 6:29 am
by ethereal1m_
Dear all,
I'm in the process of learning of using phpunit and Zend Framework by executing tests code from book example. I don't know if the code is outdated or I miss anything, when I execute the test, it doesn't output anything to stdout. Below if my my test, called AllTest.php:

Code: Select all

if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'SF_Unit_AllTests::main');
}

/**
 * TestHelper
 */
require_once dirname(__FILE__) . '/../TestHelper.php';

/**
 * Include unit tests
 */
require_once 'unit/Model/ModelAbstractTest.php';

class SF_Unit_AllTests
{
    public static function main()
    {
        PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function suite()
    {
        $suite = new PHPUnit_Framework_TestSuite('Storefront Unit Tests');
        $suite->addTestSuite('ModelAbstractTest');

        return $suite;
    }
}

if (PHPUnit_MAIN_METHOD == 'SF_Unit_AllTests::main') {
    SF_Unit_AllTests::main();
}                     
Bellow is the TestHelper.php that contains Zend Framework portion:

Code: Select all

/**
 * Get PHPUnit
 */
require_once 'PHPUnit/Framework.php';
require_once 'PHPUnit/Framework/IncompleteTestError.php';
require_once 'PHPUnit/Framework/TestCase.php';
require_once 'PHPUnit/Framework/TestSuite.php';
require_once 'PHPUnit/Runner/Version.php';
require_once 'PHPUnit/TextUI/TestRunner.php';
require_once 'PHPUnit/Util/Filter.php';

/*
 * Set error reporting level
 */
error_reporting( E_ALL | E_STRICT );

/**
 * Default timezone
 */
date_default_timezone_set('Europe/London');

/*
 * Set the include path
 */
$root  = realpath(dirname(__FILE__) . '/../');
$paths = array(
    get_include_path(),
    "$root/library/Incu",
    "$root/library",
    "$root/tests",
    "$root/application"
);
set_include_path(implode(PATH_SEPARATOR, $paths));

defined('APPLICATION_PATH')
    or define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));

/**
 * Autoloader helpers
 */
function _SF_Autloader_SetUp() {
    require_once 'Zend/Loader/Autoloader.php';
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->registerNamespace('SF_');
}

function _SF_Autloader_TearDown() {
    Zend_Loader_Autoloader::resetInstance();
    $loader = Zend_Loader_Autoloader::getInstance();
    $loader->registerNamespace('SF_');
}

/**
 * Init autoloader
 */
_SF_Autloader_SetUp();

/**
 * Start session now!
 */
//Zend_Session::$_unitTestEnabled = true;

Zend_Session::start();

/**
 * Ignore folders from code coverage etc
 */
PHPUnit_Util_Filter::addDirectoryToFilter("$root/tests");
PHPUnit_Util_Filter::addDirectoryToFilter("$root/library/Zend");

And the following is AllTests.php that contains in unit (test) folder, which is called by AllTest.php in parent folder:

Code: Select all

if (!defined('PHPUnit_MAIN_METHOD')) {
    define('PHPUnit_MAIN_METHOD', 'SF_Unit_AllTests::main');
}

/**
 * TestHelper
 */
require_once dirname(__FILE__) . '/../TestHelper.php';

/**
 * Include unit tests
 */
require_once 'unit/Model/ModelAbstractTest.php';

class SF_Unit_AllTests
{
    public static function main()
    {
        PHPUnit_TextUI_TestRunner::run(self::suite());
    }

    public static function suite()
    {
        $suite = new PHPUnit_Framework_TestSuite('Storefront Unit Tests');
        $suite->addTestSuite('ModelAbstractTest');

        return $suite;
    }
}

if (PHPUnit_MAIN_METHOD == 'SF_Unit_AllTests::main') {
    SF_Unit_AllTests::main();
}                     
And finally the ModelAbstractTest.php that containing ModelAbstractTest that is called by AllTest.php (of unit test):

Code: Select all

/**
 * TestHelper
 */
require_once dirname(__FILE__) . '/../../TestHelper.php';

/**
 * Concrete class for testing
 */
class My_TestModel extends SF_Model_Abstract
{
    protected $_testSetter;

    public function setTest($value)
    {
        $this->_testSetter = $value;
    }
}

/**
 * Test resources to load
 */
class My_Resource_Test {}
class My_Resource_Test_Me {}
class My_Form_Test {}
class My_Form_Test_Me {}

/**
 * Test case for Model_Abstract
 */
class ModelAbstractTest extends PHPUnit_Framework_TestCase
{
    /**
     * @var SF_Model_Interface
     */
    protected $_model;
    
    protected function setUp()
    {
        $this->_model = new My_TestModel();
    }
    
    protected function tearDown()
    {
        $this->_model = null;
    }

    public function test_Constructor_Can_Set_Options()
    {
        $options = array('test' => true);
        $model = new My_TestModel($options);
        $this->assertTrue($this->readAttribute($model, '_testSetter'));
    }

    public function test_GetResource_Tries_To_Instantiate_The_Correct_Resource()
    {
        $this->assertType('My_Resource_Test', $this->_model->getResource('test'));
    }

    public function test_GetForm_Tries_To_Instantiate_The_Correct_Form()
    {
        $this->assertType('My_Form_Test', $this->_model->getForm('test'));
    }

    public function test_GetMethods_Inflect_As_Expected()
    {
        $this->assertType('My_Form_Test_Me', $this->_model->getForm('testMe'));
        $this->assertType('My_Resource_Test_Me', $this->_model->getResource('testMe'));
    }
}
This code should be straight forward, but I cannot pint point what the problem is. Any ideas?

Regards,
ethereal1m

Re: phpunit: my test doesn't return anything

Posted: Tue Jun 08, 2010 2:34 am
by ethereal1m_
turns out that I didn't include some library files that causing this behaviour. Problem is fixed now.

regards,
ethereal1m

Re: phpunit: my test doesn't return anything

Posted: Wed Jun 16, 2010 11:51 am
by josh
Your test case classes can't run on their own, they are the input to a "test runner".

Code: Select all

# phpunit /path/to/testCaseClass.php
Invoke them with the phpunit command. Review the documentation for full usage instructions.

Re: phpunit: my test doesn't return anything

Posted: Thu Jul 08, 2010 2:47 am
by ethereal1m_
My initial answer regarding to the problem saying that "some library files is not included" is nothing to do with the problem. Turns out that the previous run of phpunit outputing an empty xml file that holding phpunit from execution. Therefore at the time phpunit was not executed at all. The problem is fixed when the empty xml file is deleted.