1

hello am new to phpunit test and am stuck here.

I've followed this tutorial: Zend Framework 2 : Centralize phpunit test

After that i created a module test

namespace ModulesTests\ServiceProvidersTest\Model;

use PHPUnit_Framework_TestCase;
use ModulesTests\ServiceManagerGrabber;
use User\Service\ServiceProvider; 
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

class TestServiceProviders extends PHPUnit_Framework_TestCase
 {
  protected $serviceManager;
  protected $serviceprovider;

public function setUp()
{
    $serviceManagerGrabber   = new ServiceManagerGrabber();
    $this->serviceManager = $serviceManagerGrabber->getServiceManager();
    $this->serviceprovider = new ServiceProvider() ;
}

public function testSPdetails()
{
    $stack = array('1','2');
    $this->serviceprovider->getDetails($stack);

}
}

In my ServiceProvider class

namespace User\Service;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorAwareTrait;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\Adapter\Adapter;
use Zend\Db\Sql\Sql;

class ServiceProvider implements ServiceLocatorAwareInterface
{
use ServiceLocatorAwareTrait;


public function getModel()
{
    $em = $this->getServiceLocator()-     >get('doctrine.entitymanager.orm_default');
    return  $em->getRepository('User\Entity\ServiceProvider');
}


public function getDetails($data = null,$fields='*') 
{
    $where = 1;
    $company_ids = implode(',',$data);
    if(isset($company_ids)){
        $where = 'sp.id IN('.$company_ids.')';
    }
    if(isset($fields)){

    }
    $db = $this->getServiceLocator()->get('Zend\Db\Adapter\Adapter');
    $query = 'some query';
    .....Rest code.......
    }
}
}

am getting this error :

Call to a member function get() on null in /opt/lampp/htdocs/project/module/User/src/User/Service/ServiceProvider.php

Please help what am missing here..??

2
  • I have the same problem since I moved from Zendframework2 to Zendframework3 Commented Nov 21, 2016 at 13:53
  • 1
    You should be aware ServiceLocatorAwareInterface is deprecated and will soon be removed from the framework, probably best not to use it :) Commented Nov 21, 2016 at 13:54

1 Answer 1

0

So there are a few things I notice here:

1: You have not shown any code showing how you have hooked up your service to the service manager, so it is unclear if this will ever work

2: You directly instantiate your class when you need to be using the service manager grabber you have written

$this->serviceprovider = new ServiceProvider() ;

becomes

$serviceManagerGrabber   = new ServiceManagerGrabber();
$this->serviceManager = $serviceManagerGrabber->getServiceManager();
$this->serviceprovider = $this->serviceManager->get('YOUR_SERVICE_KEY');

3: You probably should start with unit tests not these module integration tests being explained in that article. see https://framework.zend.com/manual/2.3/en/modules/zend.test.phpunit.html

4: The ServiceLocatorAwareInterface is deprecated you should probably use a factory and the factories key of service manager config to inject your dependencies

5: Your code seems to mix doctrine and zend db I don't know why you've done this, but my suggestion is ... it's be a bad idea

Here is an example of how you might put this together:

module.config.php

<?php
namespace Application;

return [
'service_manager' => [
    'factories' => [
        'ServiceProvider' => function ($serviceManager) {
            // This shouldn't be in this anon function, it should be its own
            // factory but I'm lazy and already writing loads of code for this example
            // @see https://framework.zend.com/manual/2.4/en/in-depth-guide/services-and-servicemanager.html#writing-a-factory-class
            $service = new \Application\Service\ServiceProvider(
                $serviceManager->get('doctrine.entitymanager.orm_default')
            );

            return $service;
        },
    ]
],
];

ServiceProvider.php

<?php
namespace Application\Service;

use Doctrine\ORM\EntityManager;

class ServiceProvider
{
    protected $entityManager;

    public function __construct(EntityManager $entityManager)
    {
        $this->entityManager = $entityManager;
    }

    public function getModel()
    {
        return $this->entityManager- >getRepository('User\Entity\ServiceProvider');
    }

    public function getDetails($data = null, $fields='*')
    {
        $serviceProviderRepository = $this->entityManager->getRepository('User\Entity\ServiceProvider');

        return $data;
    }
}

ModuleTest

<?php
namespace ModulesTests\Application\Service;

use PHPUnit_Framework_TestCase;
use ModulesTests\ServiceManagerGrabber;

class ServiceProvidersTest extends PHPUnit_Framework_TestCase
{
    protected $serviceManager;
    protected $serviceprovider;

    public function setUp()
    {
        $serviceManagerGrabber   = new ServiceManagerGrabber();
       $this->serviceManager = $serviceManagerGrabber->getServiceManager();
        $this->serviceprovider = $this->serviceManager->get('ServiceProvider');
    }

    public function testSPdetails()
    {
        $stack = array('1','2');
        $this->serviceprovider->getDetails($stack);
    }
}

unit test:

<?php
namespace ModulesTests\Application\Service;

use PHPUnit_Framework_TestCase;
use Application\Service\ServiceProvider;
use Prophecy\Argument;

class ServiceProvidersUnitTest extends PHPUnit_Framework_TestCase
{
    protected $entityManager;
    protected $serviceprovider;

    public function setUp()
    {
        $this->entityManager = $this->prophesize("Doctrine\\ORM\\EntityManager");
        $this->entityManager->getRepository(Argument::exact('User\Entity\ServiceProvider'))
                        ->willReturn(true);
        $this->serviceprovider = new ServiceProvider($this->entityManager->reveal());
}

    public function testSPdetails()
    {
        $stack = array('1','2');
        $this->serviceprovider->getDetails($stack);
        $this->entityManager->getRepository(Argument::exact('User\Entity\ServiceProvider'))
                        ->shouldHaveBeenCalledTimes(1);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.