. , -, . session_start - . "prerender".
'stub',
class My_Form_Element_HashStub extends Zend_Form_Element_Hash
{
public function __construct(){}
}
-.
class MyForm extends Zend_Form{
protected $_hashElement;
public function setHashElement( Zend_Form_Hash_Element $hash )
{
$this->_hashElement = $hash;
return $this;
}
protected function _getHashElement( $name = 'hashElement' )
{
if( !isset( $this->_hashElement )
{
if( isset( $name ) )
{
$element = new Zend_Form_Element_Hash( $name,
array( 'id' => $name ) );
}
else
{
$element = new Zend_Form_Element_Hash( 'hashElement',
array( 'id' => 'hashElement' ) );
}
$this->setHashElement( $element );
return $this->_hashElement;
}
}
public function init()
{
$this->addElement( $this->_getHashElement( 'myotherhashelementname' );
}
set . , , , phpunit .
class My_Form_LoginTest
extends PHPUnit_Framework_TestCase
{
/**
*
* @var My_Form_Login
*/
protected $_form;
/**
*
* @var PHPUnit_Framework_MockObject_MockObject
*/
protected $_hash;
public function setUp()
{
parent::setUp();
$this->_hash = $this->getMock( 'My_Form_Element_HashStub' );
$this->_form = new My_Form_Login( array(
'action' => '/',
'hashElement' => $this->_hash
}
public function testTrue()
{
//The hash element will now always validate to true
$this->_hash
->expects( $this->any() )
->method( 'isValid' )
->will( $this->returnValue( true ) );
//OR if you need it to validate to false
$this->_hash
->expects( $this->any() )
->method( 'isValid' )
->will( $this->returnValue( true ) );
}
. phpunit getMockObject, -, - "" .
! , .
, setHashElement ( get) FormAbstract.
REMEMBER, in phpunit you need to pass a hash element during the construction of the form. If you do not, your init method will be called before your hash stub can be set using the set method, and you will end up using a regular hash element. You will find out that you are using a regular hash element because you are likely to get some session error if you are NOT connected to the database.
Let me know if you find it useful or use it.
source
share