我希望为调用服务的工厂实现PHPUnit测试。 这是我的工厂代码:
class FMaiAffaireServiceFactory implements FactoryInterface
{
public function createService(ServiceLocatorInterface $serviceLocator)
{
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$tableGateway = new TableGateway(
'f_affaire',
$dbAdapter,
null,
$resultSetPrototype
);
$adapter = $tableGateway->getAdapter();
$sql = new Sql($adapter);
$maiAffaireTable = new FMaiAffaireTable(
$tableGateway,
$adapter,
$sql
);
$typeaffaireService = $serviceLocator->get(
'Intranet\Service\Model\PTypeaffaireService'
);
$etatAffaireService = $serviceLocator->get(
'Intranet\Service\Model\PEtataffaireService'
);
$maiPrestationService = $serviceLocator->get(
'Maintenance\Service\Model\PMaiPrestationService'
);
$maiAffaireService = new FMaiAffaireService(
$maiAffaireTable,
$typeaffaireService,
$etatAffaireService,
$maiPrestationService
);
return $maiAffaireService;
}
这是我的测试代码,但它没有起作用:
class FMaiAffaireServiceFactoryTest extends \PHPUnit_Framework_TestCase
{
public function testCreateService()
{
$sm = new ServiceManager();
$factory = new FMaiAffaireServiceFactory();
$runner = $factory->createService($sm);
}
}
编辑:我的新测试脚本:
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);
$smMock = $this->getMockBuilder('Zend\ServiceManager\ServiceManager')
->getMock();
$maiPrestationTable = $this->getMockBuilder('Maintenance\Model\BDD\PMaiPrestationTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway, $this->adapter, $this->sql))
->getMock();
$smMock->expects($this->any())
->method('get')
->with('Maintenance\Service\Model\PMaiPrestationService')
->will($this->returnValue(new PMaiPrestationService($maiPrestationTable)));
$etatAffaireTable = $this->getMockBuilder('Intranet\Model\BDD\PEtataffaireTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway))
->getMock();
$smMock->expects($this->any())
->method('get')
->with('Intranet\Service\Model\PEtataffaireService')
->will($this->returnValue(new PEtataffaireService($etatAffaireTable)));
$typeaffaireTable = $this->getMockBuilder('Intranet\Model\BDD\PTypeaffaireTable')
->setMethods(array())
->setConstructorArgs(array($mockTableGateway))
->getMock();
$smMock->expects($this->any())
->method('get')
->with('Intranet\Service\Model\PTypeaffaireService')
->will($this->returnValue(new PTypeaffaireService($typeaffaireTable)));
$smMock->expects($this->any())
->method('get')
->with('Zend\Db\Adapter\Adapter')
->will($this->returnValue($this->adapter));
$factory = new FMaiAffaireServiceFactory();
$runner = $factory->createService($smMock);
// assertions here
}
这告诉我:获取无法获取或创建Zend\Db\Adapter\Adapter实例。 编辑:这是服务:
public function createService(ServiceLocatorInterface $serviceLocator)
{
$dbAdapter = $serviceLocator->get('Zend\Db\Adapter\Adapter');
$resultSetPrototype = new ResultSet();
$tableGateway = new TableGateway(
'f_affaire',
$dbAdapter,
null,
$resultSetPrototype
);
$adapter = $tableGateway->getAdapter();
$sql = new Sql($adapter);
$maiAffaireTable = new FMaiAffaireTable(
$tableGateway,
$adapter,
$sql
);
$typeaffaireService = $serviceLocator->get(
'Intranet\Service\Model\PTypeaffaireService'
);
$etatAffaireService = $serviceLocator->get(
'Intranet\Service\Model\PEtataffaireService'
);
$maiPrestationService = $serviceLocator->get(
'Maintenance\Service\Model\PMaiPrestationService'
);
$maiAffaireService = new FMaiAffaireService(
$maiAffaireTable,
$typeaffaireService,
$etatAffaireService,
$maiPrestationService
);
return $maiAffaireService;
}
我该如何让它工作?
谢谢。