Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/LoggerAbstractServiceFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
*/

namespace Zend\Log;

use Zend\ServiceManager\AbstractFactoryInterface;
use Zend\ServiceManager\ServiceLocatorInterface;

/**
* Logger abstract service factory.
*
* Allow to configure multiple loggers for application.
*/
class LoggerAbstractServiceFactory implements AbstractFactoryInterface
{
/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return bool
*/
public function canCreateServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return isset($config['log'][$requestedName]);
}

/**
* @param ServiceLocatorInterface $serviceLocator
* @param string $name
* @param string $requestedName
* @return \Zend\Log\Logger
*/
public function createServiceWithName(ServiceLocatorInterface $serviceLocator, $name, $requestedName)
{
$config = $serviceLocator->get('Config');
return new Logger($config['log'][$requestedName]);
}
}
88 changes: 88 additions & 0 deletions test/LoggerAbstractServiceFactoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Zend Framework (http://framework.zend.com/)
*
* @link http://github.com/zendframework/zf2 for the canonical source repository
* @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
* @license http://framework.zend.com/license/new-bsd New BSD License
* @package Zend_Log
*/

namespace ZendTest\Log;

use Zend\ServiceManager\ServiceManager;
use Zend\Mvc\Service\ServiceManagerConfig;

/**
* @category Zend
* @package Zend_Log
* @subpackage UnitTests
* @group Zend_Log
*/
class LoggerAbstractServiceFactoryTeset extends \PHPUnit_Framework_TestCase
{
/**
* @var \Zend\ServiceManager\ServiceLocatorInterface
*/
private $serviceManager;

/**
* Set up LoggerAbstractServiceFactory and loggers configuration.
*
* @see PHPUnit_Framework_TestCase::setUp()
*/
protected function setUp()
{
$this->serviceManager = new ServiceManager(new ServiceManagerConfig(array(
'abstract_factories' => array('Zend\Log\LoggerAbstractServiceFactory'),
)));

$this->serviceManager->setService('Config', array(
'log' => array(
'Application\Frontend\Logger' => array(),
'Application\Backend\Logger' => array(),
),
));
}

/**
* @return array
*/
public function providerValidLoggerService()
{
return array(
array('Application\Frontend\Logger'),
array('Application\Backend\Logger'),
);
}

/**
* @return array
*/
public function providerInvalidLoggerService()
{
return array(
array('Application\Unknown\Logger'),
);
}

/**
* @param string $service
* @dataProvider providerValidLoggerService
*/
public function testValidLoggerService($service)
{
$actual = $this->serviceManager->get($service);
$this->assertInstanceOf('Zend\Log\Logger', $actual);
}

/**
* @param string $service
* @dataProvider providerInvalidLoggerService
* @expectedException \Zend\ServiceManager\Exception\ServiceNotFoundException
*/
public function testInvalidLoggerService($service)
{
$actual = $this->serviceManager->get($service);
}
}

0 comments on commit c1290fa

Please sign in to comment.