PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Sat Aug 18, 2018 9:36 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
PostPosted: Wed May 26, 2010 6:29 am 
Offline
Forum Newbie

Joined: Wed May 26, 2010 6:12 am
Posts: 3
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:
Syntax: [ Download ] [ Hide ]
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:
Syntax: [ Download ] [ Hide ]
/**
 * 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:
Syntax: [ Download ] [ Hide ]
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):
Syntax: [ Download ] [ Hide ]
/**
 * 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


Top
 Profile  
 
PostPosted: Tue Jun 08, 2010 2:34 am 
Offline
Forum Newbie

Joined: Wed May 26, 2010 6:12 am
Posts: 3
turns out that I didn't include some library files that causing this behaviour. Problem is fixed now.

regards,
ethereal1m


Top
 Profile  
 
PostPosted: Wed Jun 16, 2010 11:51 am 
Offline
DevNet Master

Joined: Wed Feb 11, 2004 4:23 pm
Posts: 4872
Location: Palm beach, Florida
Your test case classes can't run on their own, they are the input to a "test runner".

Code:
# phpunit /path/to/testCaseClass.php

Invoke them with the phpunit command. Review the documentation for full usage instructions.


Top
 Profile  
 
PostPosted: Thu Jul 08, 2010 2:47 am 
Offline
Forum Newbie

Joined: Wed May 26, 2010 6:12 am
Posts: 3
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.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group