I created a View Helper :
class SousMenuContrat extends AbstractHelper
{
private $maiContratService;
public function __construct(
FMaiContratService $maiContratService,
) {
$this->maiContratService = $maiContratService;
}
public function __invoke($iMaiContratId, $sActive)
{
$oContrat = $this->maiContratService->selectById($iMaiContratId);
return $this->getView()->partial('maintenance/sousmenucontrat', array(
'oContrat' => $oContrat
));
}
}
So now I need to test it, with PHPUnit :
class SousMenuContratTest extends TestCase
{
private $myService;
public function setUp()
{
$maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
->disableOriginalConstructor()
->getMock();
$oContrat = new FMaiContrat();
$stub = $this->returnValue($oContrat);
$maiContratService->expects($this->any())->method('selectById')->will($stub);
$this->myService = new SousMenuContrat(
$maiContratService
);
}
public function testInvoque()
{
$this->myService->__invoke(2, 'contrat');
}
}
But the test sends an error, because the test doesn't know :
$this->getView()->partial();
Thanks in advance :)
In your test, you need to mock the renderer returned by getView():
/** @var PhpRenderer|\PHPUnit_Framework_MockObject_MockObject $rendererMock */
$rendererMock = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer')
->disableOriginalConstructor()
->getMock();
$rendererMock->expects($this->once())
->method("partial")
->with(array(
'maintenance/sousmenucontrat',
array('oContrat' => new FMaiContrat()),
));
$this->myService->setView($rendererMock);
Best solution would be to use the same FMaiContrat object you instantiate in setUp() in with(), but in this case, this works as well.
Edit: And the complete test code will look like this:
class SousMenuContratTest extends TestCase
{
private $myService;
public function setUp()
{
$maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
->disableOriginalConstructor()
->getMock();
$oContrat = new FMaiContrat();
$stub = $this->returnValue($oContrat);
$maiContratService->expects($this->any())->method('selectById')->will($stub);
$this->myService = new SousMenuContrat(
$maiContratService
);
}
public function testInvoque()
{
/** @var PhpRenderer|\PHPUnit_Framework_MockObject_MockObject $rendererMock */
$rendererMock = $this->getMockBuilder('Zend\View\Renderer\PhpRenderer')
->disableOriginalConstructor()
->getMock();
$rendererMock->expects($this->once())
->method("partial")
->with(array(
'maintenance/sousmenucontrat',
array('oContrat' => new FMaiContrat()),
));
$this->myService->setView($rendererMock);
$this->myService->__invoke(2, 'contrat');
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With