PHPUnit Mocking View Helper ZF2 - php

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');
}
}

You can use the following setup if you want to just use ZF2 classes and then just mock your dependency in SousMenuContrat constructor
protected function setUp()
{
$maiContratService = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
->disableOriginalConstructor()
->getMock();
$oContrat = new FMaiContrat($maiContratService);
$stub = $this->returnValue($oContrat);
$maiContratService->expects($this->any())->method('selectById')->will($stub);
Doctype::unsetDoctypeRegistry();
$this->helper = new SousMenuContrat();
$this->renderer = new PhpRenderer;
$this->viewHelperManager = $this->renderer->getHelperPluginManager();
$config = new HelperConfig();
$config->configureServiceManager($this->viewHelperManager);
$this->helper->setView($this->renderer);
parent::setUp();
}

Related

Codecoverage phpunit test issue

I am running phpunit version 9.2 and I would like to know why my method is not covered in the phpunit coverage.
This is my class:
class Methods extends Template
{
const DISABLED_PAYMENT_METHODS_1 = 'free';
const DISABLED_PAYMENT_METHODS_2 = 'adyen_cc';
const DISABLED_PAYMENT_METHODS_3 = 'adyen_oneclick';
protected $paymentMethodList;
protected $storeManager;
protected $logger;
public function __construct(
Context $context,
PaymentMethodList $paymentMethodList,
StoreManagerInterface $storeManager,
LoggerInterface $logger,
array $data = []
) {
$this->paymentMethodList = $paymentMethodList;
$this->storeManager = $storeManager;
$this->logger = $logger;
parent::__construct($context, $data);
}
public function getPaymentMethods()
{
try {
$storeId = $this->storeManager->getStore()->getId();
$paymentList = $this->paymentMethodList->getActiveList($storeId);
$resultPayments = [];
foreach ($paymentList as $payment) {
if ($payment->getCode() !== self::DISABLED_PAYMENT_METHODS_1 &&
$payment->getCode() !== self::DISABLED_PAYMENT_METHODS_2 &&
$payment->getCode() !== self::DISABLED_PAYMENT_METHODS_3
) {
$resultPayments[] = $payment;
}
}
return $resultPayments;
} catch (Exception $e) {
$this->logger->error($e->getMessage());
return false;
}
}
}
and this is my test class:
class MethodsTest extends TestCase
{
private $model;
private function getSimpleMock($originalClassName)
{
return $this->getMockBuilder($originalClassName)
->disableOriginalConstructor()
->getMock();
}
public function setUp() : void
{
$context = $this->getSimpleMock(Context::class);
$paymentMethodList = $this->getSimpleMock(PaymentMethodList::class);
$storeManager = $this->getSimpleMock(StoreManagerInterface::class);
$logger = $this->getSimpleMock(LoggerInterface::class);
$this->model = new Methods(
$context,
$paymentMethodList,
$storeManager,
$logger,
[]
);
}
public function testGetPaymentMethods()
{
$stub = $this->createMock(Methods::class);
$stub->method('getPaymentMethods')
->willReturn([]);
try {
$stub->getPaymentMethods();
$this->fail("Expected exception!");
} catch (\Exception $error) {
$this->assertEquals("Expected exception!", $error->getMessage());
}
}
}
When I run the command to get the coverage. I am getting:
I am really curious why my test is not covered or at least the exception part ? Would you please share you ideas why ? and what can i do in order to fix this ? Right now I got a 29 % and I would like to get at least 60% coverage.
Thank you
On this line $stub = $this->createMock(Methods::class); you are creating a mock of the Methods class, so not actually testing the real class.
You will need to use the object you created in your setUp() method, and set up mock returns on the dependencies you passed in (perhaps converting some of them to be class properties).
You should test the real class, as example:
public function testGetPaymentMethods()
{
// define a payment
$paymentFreeCode = $this->createMock(Payment::class);
$paymentFreeCode->method('getcode')
->willReturn("free");
// define a payment
$payment = $this->createMock(Payment::class);
$payment->method('getcode')
->willReturn("invalid-code");
$paymentList = [
$paymentFreeCode,
$payment,
];
// define a store
$store = $this->createMock(Store::class);
$store->method('getId')
->willReturn("my-store-id");
// return store from the store manager
$this->storeManager->method('getStore')
->willReturn(myStore);
// return the payment list
$this->paymentMethodList->method('getActiveList')->with("my-store-id")
->willReturn($paymentList);
// call the real class instrumented with mocks
$paymentMethods = $this->model->getPaymentMethods();
$this->assertIsArray($paymentMethods);
$this->assertCount($paymentMethods, 1);
}

Mocking services which calls a zend db function and reset() function

So I created a test service set :
class FMaiAffaireServiceTest extends TestCase
{
/**
* #var MyService
*/
private $myService;
private $typeaffaireTable;
private $mockDriver;
private $mockConnection;
private $mockPlatform;
private $mockStatement;
private $adapter;
private $sql;
public function setUp()
{
$this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
$this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
$this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
$this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
$this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
$this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
$this->sql = new Sql($this->adapter);
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array(), array(), '', false);
$maiAffaireTable = $this->getMockBuilder('Maintenance\Model\BDD\FMaiAffaireTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
->getMock();
$stub = $this->returnValue(new ResultSet());
$maiAffaireTable->expects($this->any())->method('listAffaires')->will($stub);
$this->myService = new FMaiAffaireService(
$maiAffaireTable
);
}
public function testListAffaires()
{
$this->myService->listAffaires(1,10);
}
}
My service looks like this, it is a call to my Zend Db function :
class FMaiAffaireService
{
private $maiAffaireTable;
public function __construct(
$maiAffaireTable,
) {
$this->maiAffaireTable = $maiAffaireTable;
}
public function listAffaires($iOffset, $iLimit) {
$aResults = $this->maiAffaireTable->listAffaires($iOffset, $iLimit);
return $aResults->toArray();
}
}
And here is the sample of my Zend DB function :
class FMaiAffaireTable
{
protected $tableGateway;
protected $adapter;
protected $sql;
public function __construct(
TableGateway $tableGateway,
Adapter $adapter,
Sql $sql
) {
$this->tableGateway = $tableGateway;
$this->adapter = $adapter;
$this->sql = $sql;
}
public function listAffaires($iOffset, $iLimit)
{
try {
$resultSet = $this->tableGateway->select(
function (Select $select) use (
$iOffset,
$iLimit
) {
$select->offset($iOffset);
$select->limit($iLimit);
}
);
return $resultSet;
} catch (\Exception $e) {
throw new \Exception($e);
}
}
}
And there is a big problem at the execution of PHPUnit :
1) Directories\FMaiAffaireServiceTest::testListAffaires reset() expects parameter 1 to be array, null given
I don't call reset() ANYWHERE ! That's the problem ... I think it's a PDO function but ... I'm a bit lost.
Thanks.
The problem is here
$stub = $this->returnValue(new ResultSet());
$maiAffaireTable->expects($this->any())->method('listAffaires')->will($stub);
A non-initialized ResultSet will not have a datasource, running toArray() on it (as you do in your service) will first try and reset the datasource, which will be null.
Try
$resultSet = new ResultSet();
$resultSet->initialize(array());
$stub = $this->returnValue($resultSet);

How to mock ServiceLocator ZF2?

Here is a Factory :
namespace Maintenance\Factory\View\Helper;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
use Maintenance\View\Helper\SousMenuContrat;
class SousMenuContratFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$realServiceLocator = $serviceLocator->getServiceLocator();
$maiContratService = $realServiceLocator->get(
'Maintenance\Service\Model\FMaiContratService'
);
return new SousMenuContrat(
$maiContratService
);
}
}
I have to write some PHPUnit tests, I began to do so :
public function testCreateService()
{
$this->mockDriver = $this->getMock('Zend\Db\Adapter\Driver\DriverInterface');
$this->mockConnection = $this->getMock('Zend\Db\Adapter\Driver\ConnectionInterface');
$this->mockDriver->expects($this->any())->method('checkEnvironment')->will($this->returnValue(true));
$this->mockDriver->expects($this->any())->method('getConnection')->will($this->returnValue($this->mockConnection));
$this->mockPlatform = $this->getMock('Zend\Db\Adapter\Platform\PlatformInterface');
$this->mockStatement = $this->getMock('Zend\Db\Adapter\Driver\StatementInterface');
$this->mockDriver->expects($this->any())->method('createStatement')->will($this->returnValue($this->mockStatement));
$this->adapter = new Adapter($this->mockDriver, $this->mockPlatform);
$this->sql = new Sql($this->adapter);
$mockTableGateway = $this->getMock('Zend\Db\TableGateway\TableGateway', array(), array(), '', false);
$maiContratTable = $this->getMockBuilder('Maintenance\Model\BDD\FMaiContratTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
->getMock();
$smMock = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
->setMethods(array('get'))
->getMock();
$smMock->expects($this->at(0))
->method('get')
->with('Maintenance\Service\Model\FMaiContratService')
->will($this->returnValue(new FMaiContratService($maiContratTable)));
$factory = new SousMenuContratFactory();
$runner = $factory->createService($smMock);
}
But I got some problems. This tells me :
Call to undefined method Mock_ServiceManager_3ed93deb::getServiceLocator()
What have I misunderstood ?
Thanks !
In your factory you call:
$realServiceLocator = $serviceLocator->getServiceLocator();
But you defined:
$smMock->expects($this->at(0))
->method('get')
The ServiceLocator passed to your factory usually does not have the method getServiceLocator because it already is the service locator. (Edit: Scratch that, PluginManagers do!) Instead use:
public function createService(ServiceLocatorInterface $serviceLocator)
{
$maiContratService = $serviceLocator->get(
'Maintenance\Service\Model\FMaiContratService'
);
return new SousMenuContrat(
$maiContratService
);
}
Edit: Plugin factories are another thing, here's the test code:
public function testCreateService()
{
$maiContractServiceMock = $this->getMockBuilder('Maintenance\Service\Model\FMaiContratService')
->disableOriginalConstructor()
->getMock();
// if you do something with FMaiContratService in the constructor of SousMenuContrat,
// mock more methods here
$smMock = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
->setMethods(array('get'))
->getMock();
$smMock->expects($this->at(0))
->method('get')
->with('Maintenance\Service\Model\FMaiContratService')
->will($this->returnValue($maiContractServiceMock));
$hpmMock = $this->getMockBuilder('Zend\View\HelperPluginManager')
->setMethods(array('getServiceLocator'))
->getMock();
$hpmMock->expects($this->any())
->method('getServiceLocator')
->will($this->returnValue($smMock));
$factory = new SousMenuContratFactory();
$runner = $factory->createService($hpmMock);
}
In this case, you need a mock of the plugin manager, returning another service locator if getServiceLocator is called. Désolé!

Zend Framework 2 how to test forward in controller using phpunit?

How can I test a forward in a controller with PHPUnit?
I have two simple modules (A and B), module A call the module B using a forward.
here is a simple code that not work :
ModuleA
class ModuleAController extends AbstractRestfulController
{
protected $em;
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getEntityManager()
{
if (null === $this->em) {
$this->em
$this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
return $this->em;
}
public function getList()
{
$data = array('message' => 'passed by module A');
$forward = $this->forward()->dispatch('ModuleB\Controller\ModuleB');
$data['Message'] = $forward->getVariable('Message');
return new JsonModel($data);
}
}
ModuleB
class ModuleBController extends AbstractRestfulController
{
public function setEntityManager(EntityManager $em)
{
$this->em = $em;
}
public function getEntityManager()
{
if (null === $this->em) {
$this->em = $this->getServiceLocator()->get('doctrine.entitymanager.orm_default');
}
}
public function getList()
{
$data = array('Message'=>'passed by module B');
return new JsonModel($data);
}
}
And this is a test code :
class ModuleAControllerTest extends AbstractHttpControllerTestCase
{
protected $controller;
protected $request;
protected $response;
protected $routeMatch;
protected $event;
protected function setUp()
{
$serviceManager = Bootstrap::getServiceManager();
$this->controller = new ModuleAController();
$this->request = new Request();
$this->routeMatch = new RouteMatch(array());
$this->event = new MvcEvent();
$config = $serviceManager->get('Config');
$routerConfig = isset($config['router']) ? $config['router'] : array();
$router = HttpRouter::factory($routerConfig);
$this->event->setRouter($router);
$this->event->setRouteMatch($this->routeMatch);
$this->controller->setEvent($this->event);
$this->controller->setServiceLocator($serviceManager);
}
public function testModuleAControllerCanBeAccessed()
{
$result = $this->controller->dispatch($this->request);
$response = $this->controller->getResponse();
$this->assertEquals(200, $response->getStatusCode());
$this->assertEquals(200, 1+99+100);
}
}
And this is the error message :
There was 1 error:
1) ModuleATest\Controller\ModuleAControllerTest::testModuleAControllerCanBeAccessed
Zend\ServiceManager\Exception\ServiceNotCreatedException: An exception was raised while creating "forward"; no instance returned
....
Caused by
Zend\ServiceManager\Exception\ServiceNotCreatedException: Zend\Mvc\Controller\Plugin\Service\ForwardFactory requires that the application service manager has been injected; none found
....
FAILURES!
Tests: 1, Assertions: 0, Errors: 1.
Is there any way to make this code work ??Any idea ??
Thank you.
I have not created mock for plugins yet. I don't know how set new plugin to controller. But mock will be like it.
PHPunit test file
public function testControllerWithMock()
{
/* Result from ModuleBController method getList() */
$forwardResult = new JsonModel(array('Message'=>'passed by module B'));
/* Create mock object for forward plugin */
$forwardPluginMock = $this->getMockBuilder('\Zend\Mvc\Controller\Plugin\Forward')
->disableOriginalConstructor()
->getMock();
$forwardPluginMock->expects($this->once())
->method('dispatch') /* Replace method dispatch in forward plugin */
->will($this->returnValue($forwardResult)); /* Dispatch method will return $forwardResult */
/* Need register new plugin (made mock object) */
$controller->setPluginManager(); /* ??? Set new plugin to controller */
I'm thinking how decide it.
Ok, try it.
$controller->getPluginManager()->injectController($forwardPluginMock);
I don't write PHPUnit tests for controllers. Because controllers must return a view and best solution using Selenium for testing view. I usually use PHPUnitSelenium tests for testing it.

PHPUnit Stub does not return needed value

I have the following unit test but I don't get back the needed values. Maybe I don't understand how this works correctly.
class TestClass
{
public function getData()
{
$id = 1123;
return $id;
}
}
class Test_ClassTesting extends PHPUnit_Framework_TestCase
{
public function test_addData()
{
$stub = $this->getMock('TestClass');
$stub
->expects($this->any())
->method('getData')
->will($this->returnValue('what_should_i_put_here_to_get id from TESTCLASS'));
$y = $stub->getData();
}
}
As the commenters have said, you simply return the value desired.
class TestClass
{
public function getData()
{
$id = 1123;
return $id;
}
}
class Test_ClassTesting extends PHPUnit_Framework_TestCase
{
public function test_addData()
{
$stub = $this->getMock('TestClass'); // Original Class is not used now
$stub
->expects($this->any())
->method('getData')
->will($this->returnValue(4444)); // Using different number to show stub works, not actual function
$this->assertEquals(4444, $stub->getData());
}
public function test_addDataWithoutStub()
{
$object = new TestClass();
$this->assertEquals(1123, $object->getData());
}
}

Categories