Why could you not just place the expected object in the expectOnce() call?
Well Marcus, you just removed a whole lot of lines from my test. To answer your question: Because I hadn't RTFMed!
Is it because you want this to tie up with something another object passes in?
No its because I'm an idiot.
I've kind of lost the plot on this one Sad.
No I have, you're doing just fine.
I have a bit of a thing with mock objects....I read "The mock object pattern" in Jason Sweat's patterns book and he said they help you out when you are experiencing a "chicken and egg scenario" and you can use them to stand in for objects that do not yet exist and that really confused me because obviously Mock::generate() needs that all important first parameter. Annnyyway I think I'm finally getting to grips with them. As I understand it mocks are
- To test an object's interactions with another; without mocks this is difficult
- To simplify complex or expensive emulations with predefined returns such as standing in for a databse
- To make tests less fragile (I only realised today you can mock interfaces which is very nice)
Anyway here's my new code, see much better ahhhh:
Code: Select all
<?php
require_once 'Osis/Form/Widget.php';
require_once 'Osis/Form/Validation/Interface.php';
require_once 'Osis/Form/Container/Master/Interface.php';
require_once 'Osis/Form/Renderer/IsA/Interface.php';
Mock::generate('Osis_Form_Validation_Interface','Mock_Osis_Form_Validation');
Mock::generate('Osis_Form_Container_Master_Interface', 'Mock_Osis_Form_Container_Master_Interface');
Mock::generate('Osis_Form_Retriever_Interface', 'Mock_Osis_Form_Retriever');
Mock::generate('Osis_Form_Renderer_IsA_Interface', 'Mock_Osis_Form_Renderer');
class Osis_Form_WidgetTest extends Osis_UnitTest
{
/**
* @var Osis_Form_Widget
*/
public $inst;
public function setUp()
{
$this->inst = new Osis_Form_Widget(
new Osis_Form_Id('foo'),
$this->mockValidation = new Mock_Osis_Form_Validation()
);
$this->inst->setRetriever(
$this->mockRetriever = new Mock_Osis_Form_Retriever()
);
$this->inst->setRenderer(
$this->mockRenderer = new Mock_Osis_Form_Renderer()
);
}
public function tearDown()
{
$this->mockValidation->tally();
$this->mockRetriever->tally();
$this->mockRenderer->tally();
}
public function testAll()
{
$this->mockRetriever->expectOnce('initialize', array($mockMaster));
$this->mockRetriever->setReturnValue('getInput', $inputReturn = uniqid());
$this->mockRetriever->expectCallCount('getInput', 2);
$this->mockValidation->expectCallCount('dispatch', 2);
$this->mockValidation->expectAt(0, 'dispatch', array(Osis_Form_Widget::EVENT_INIT, $this->inst));
$this->mockValidation->expectAt(1, 'dispatch', array(Osis_Form_Widget::EVENT_RENDER, $this->inst));
$mockMaster = new Mock_Osis_Form_Container_Master_Interface();
$this->inst->initialize($mockMaster);
$this->mockRenderer->setReturnValue('render', $renderReturn = uniqid());
$this->mockRenderer->expectOnce('render');
$this->assertEqual($renderReturn, $this->inst->render());
$this->assertEqual($inputReturn, $this->inst->getInput());
}
}