Skip to content

Commit

Permalink
add ConfigProvider and UTs
Browse files Browse the repository at this point in the history
  • Loading branch information
mamuz committed Sep 5, 2014
1 parent 20e9746 commit 4fa982d
Show file tree
Hide file tree
Showing 14 changed files with 353 additions and 86 deletions.
36 changes: 12 additions & 24 deletions src/MamuzBlogFeed/Controller/FeedController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use MamuzBlog\Feature\PostQueryInterface;
use MamuzBlogFeed\Feed\Writer\FactoryInterface;
use MamuzBlogFeed\Options\ConfigProviderInterface;
use Zend\EventManager\ListenerAggregateInterface as Listener;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\Mvc\MvcEvent;
Expand All @@ -20,19 +21,25 @@ class FeedController extends AbstractActionController
/** @var FactoryInterface */
private $feedFactory;

/** @var ConfigProviderInterface */
private $configProvider;

/**
* @param PostQueryInterface $postService
* @param Listener $listener
* @param FactoryInterface $feedFactory
* @param PostQueryInterface $postService
* @param Listener $listener
* @param FactoryInterface $feedFactory
* @param ConfigProviderInterface $configProvider
*/
public function __construct(
PostQueryInterface $postService,
Listener $listener,
FactoryInterface $feedFactory
FactoryInterface $feedFactory,
ConfigProviderInterface $configProvider
) {
$this->postService = $postService;
$this->listener = $listener;
$this->feedFactory = $feedFactory;
$this->configProvider = $configProvider;
}

public function onDispatch(MvcEvent $event)
Expand All @@ -52,31 +59,12 @@ public function postsAction()
$posts = $this->postService->findPublishedPosts();
}

$feedOptions = $this->getFeedOptionsBy($tag);
$feedOptions = $this->configProvider->getFor($tag);
$feed = $this->feedFactory->create($feedOptions, $posts);

$feedmodel = new Model\FeedModel;
$feedmodel->setFeed($feed);

return $feedmodel;
}

/**
* @param string|null $tag
* @return array
*/
private function getFeedOptionsBy($tag = null)
{
if (!is_string($tag)) {
$tag = 'default';
}

$config = $this->getServiceLocator()->get('Config')['MamuzBlogFeed'];

if (isset($config[$tag])) {
return (array) $config[$tag];
}

return array();
}
}
6 changes: 5 additions & 1 deletion src/MamuzBlogFeed/Controller/FeedControllerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use MamuzBlogFeed\Extractor\FeedEntry;
use MamuzBlogFeed\Hydrator\Mutator;
use MamuzBlogFeed\Feed\Writer\Factory;
use MamuzBlogFeed\Options\ConfigProvider;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand Down Expand Up @@ -34,6 +35,9 @@ public function createService(ServiceLocatorInterface $serviceLocator)
$postExtractor = new FeedEntry($viewHelperManager->getRenderer());
$feedFactory = new Factory($postExtractor, new Mutator);

return new FeedController($postService, $listenerAggregate, $feedFactory);
$config = $serviceLocator->get('Config')['MamuzBlogFeed'];
$configProvider = new ConfigProvider($config);

return new FeedController($postService, $listenerAggregate, $feedFactory, $configProvider);
}
}
45 changes: 30 additions & 15 deletions src/MamuzBlogFeed/Filter/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
namespace MamuzBlogFeed\Filter;

use Doctrine\ORM\Query as QueryBuilder;
use MamuzBlogFeed\Options\ConfigProviderInterface;
use Zend\Filter\FilterInterface;

class Query implements FilterInterface
{
/** @var array */
private $config = array();
/** @var ConfigProviderInterface */
private $configProvider;

/** @var int */
private $defaultMaxResults;

/**
* @param array $config
* @param ConfigProviderInterface $configProvider
* @param int $defaultMaxResults
*/
public function __construct(array $config = array())
public function __construct(ConfigProviderInterface $configProvider, $defaultMaxResults = 100)
{
$this->config = $config;
$this->configProvider = $configProvider;
$this->defaultMaxResults = $defaultMaxResults;
}

public function filter($value)
Expand All @@ -24,19 +30,28 @@ public function filter($value)
return $value;
}

$tag = $value->getParameter('tag');
if ($tag == null) {
$tag = 'default';
}

if (isset($this->config[$tag]['maxResults'])) {
$maxResults = (int) $this->config[$tag]['maxResults'];
} else {
$maxResults = 100;
}
$maxResults = $this->getMaxResultsFor($value->getParameter('tag'));

$value->setFirstResult(0)->setMaxResults($maxResults);

return $value;
}

/**
* @param mixed $tag
* @return int
*/
private function getMaxResultsFor($tag)
{
$config = $this->configProvider->getFor($tag);

if (isset($config['maxResults'])) {
$maxResults = (int) $config['maxResults'];
} else {
$maxResults = $this->defaultMaxResults;
}

return $maxResults;
}

}
2 changes: 1 addition & 1 deletion src/MamuzBlogFeed/Hydrator/Mutator.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public function hydrate(array $data, $object)
&& is_callable(array($object, $setMethod))
) {
$object->$setMethod($value);
} elseif (method_exists($object, $setMethod)
} elseif (method_exists($object, $addMethod)
&& is_callable(array($object, $addMethod))
) {
$object->$addMethod($value);
Expand Down
5 changes: 4 additions & 1 deletion src/MamuzBlogFeed/Listener/AggregateFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace MamuzBlogFeed\Listener;

use MamuzBlogFeed\Filter\Query;
use MamuzBlogFeed\Options\ConfigProvider;
use Zend\ServiceManager\FactoryInterface;
use Zend\ServiceManager\ServiceLocatorAwareInterface;
use Zend\ServiceManager\ServiceLocatorInterface;
Expand All @@ -20,7 +21,9 @@ public function createService(ServiceLocatorInterface $serviceLocator)
}

$config = $serviceLocator->get('Config')['MamuzBlogFeed'];
$queryFilter = new Query($config);
$configProvider = new ConfigProvider($config);

$queryFilter = new Query($configProvider);

return new Aggregate($queryFilter);
}
Expand Down
35 changes: 35 additions & 0 deletions src/MamuzBlogFeed/Options/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

namespace MamuzBlogFeed\Options;

class ConfigProvider implements ConfigProviderInterface
{
/** @var string */
private $defaultIndex;

/** @var array */
private $config = array();

/**
* @param array $config
* @param string $defaultIndex
*/
public function __construct(array $config, $defaultIndex = 'default')
{
$this->config = $config;
$this->defaultIndex = (string) $defaultIndex;
}

public function getFor($index = null)
{
if (!is_string($index)) {
$index = $this->defaultIndex;
}

if (isset($this->config[$index])) {
return (array) $this->config[$index];
}

return array();
}
}
12 changes: 12 additions & 0 deletions src/MamuzBlogFeed/Options/ConfigProviderInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace MamuzBlogFeed\Options;

interface ConfigProviderInterface
{
/**
* @param mixed $index
* @return array
*/
public function getFor($index = null);
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ public function testCreation()
$sm->shouldReceive('get')->with('MamuzBlog\DomainManager')->andReturn($sm);
$sm->shouldReceive('get')->with('MamuzBlog\Service\PostQuery')->andReturn($queryInterface);
$sm->shouldReceive('get')->with('MamuzBlogFeed\Listener\Aggregate')->andReturn($listener);
$sm->shouldReceive('get')->with('Config')->andReturn(array('MamuzBlogFeed' => array()));

$controller = $this->fixture->createService($sm);

Expand All @@ -55,6 +56,7 @@ public function testCreationWithServiceLocatorAwareness()
$sm->shouldReceive('get')->with('MamuzBlog\DomainManager')->andReturn($sm);
$sm->shouldReceive('get')->with('MamuzBlog\Service\PostQuery')->andReturn($queryInterface);
$sm->shouldReceive('get')->with('MamuzBlogFeed\Listener\Aggregate')->andReturn($listener);
$sm->shouldReceive('get')->with('Config')->andReturn(array('MamuzBlogFeed' => array()));

$controller = $this->fixture->createService($sl);

Expand Down
39 changes: 14 additions & 25 deletions tests/MamuzBlogFeedTest/Controller/FeedControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,20 +50,26 @@ class FeedControllerTest extends \PHPUnit_Framework_TestCase
/** @var \ArrayObject */
protected $posts;

/** @var array */
protected $config = array('MamuzBlogFeed' => array('default' => array(1, 2), 'foo' => array('bar')));
/** @var \MamuzBlogFeed\Options\ConfigProviderInterface |\Mockery\MockInterface */
protected $configProvider;

protected function setUp()
{
$this->posts = new \ArrayObject;
$this->postService = \Mockery::mock('MamuzBlog\Feature\PostQueryInterface');
$this->serviceLocator = \Mockery::mock('Zend\ServiceManager\ServiceLocatorInterface');
$this->serviceLocator->shouldReceive('get')->with('Config')->andReturn($this->config);
$this->feedWriter = \Mockery::mock('Zend\Feed\Writer\Feed');
$this->feedFactory = \Mockery::mock('MamuzBlogFeed\Feed\Writer\FactoryInterface');
$this->listener = \Mockery::mock('Zend\EventManager\ListenerAggregateInterface')->shouldIgnoreMissing();
$this->configProvider = \Mockery::mock('MamuzBlogFeed\Options\ConfigProviderInterface');

$this->fixture = new FeedController(
$this->postService,
$this->listener,
$this->feedFactory,
$this->configProvider
);

$this->fixture = new FeedController($this->postService, $this->listener, $this->feedFactory);
$this->request = new Request();
$this->routeMatch = new RouteMatch(array('controller' => 'posts'));
$this->event = new MvcEvent();
Expand Down Expand Up @@ -92,10 +98,11 @@ public function testPostsCanBeAccessedWithNullTag()
$this->params->shouldReceive('fromRoute')->with('tag')->andReturn(null);

$this->postService->shouldReceive('findPublishedPosts')->andReturn($this->posts);
$this->configProvider->shouldReceive('getFor')->with(null)->andReturn(array(1));

$this->feedFactory
->shouldReceive('create')
->with($this->config['MamuzBlogFeed']['default'], $this->posts)
->with(array(1), $this->posts)
->andReturn($this->feedWriter);

$result = $this->fixture->dispatch($this->request);
Expand All @@ -111,29 +118,11 @@ public function testPostsCanBeAccessedWithTag()
$this->params->shouldReceive('fromRoute')->with('tag')->andReturn('foo');

$this->postService->shouldReceive('findPublishedPostsByTag')->with('foo')->andReturn($this->posts);
$this->configProvider->shouldReceive('getFor')->with('foo')->andReturn(array(1));

$this->feedFactory
->shouldReceive('create')
->with($this->config['MamuzBlogFeed']['foo'], $this->posts)
->andReturn($this->feedWriter);

$result = $this->fixture->dispatch($this->request);
$response = $this->fixture->getResponse();

$this->assertSame($this->feedWriter, $result->getFeed());
$this->assertEquals(200, $response->getStatusCode());
}

public function testPostsCanBeAccessedWithTagWithoutFeedOptions()
{
$this->routeMatch->setParam('action', 'posts');
$this->params->shouldReceive('fromRoute')->with('tag')->andReturn('bar');

$this->postService->shouldReceive('findPublishedPostsByTag')->with('bar')->andReturn($this->posts);

$this->feedFactory
->shouldReceive('create')
->with(array(), $this->posts)
->with(array(1), $this->posts)
->andReturn($this->feedWriter);

$result = $this->fixture->dispatch($this->request);
Expand Down
Loading

0 comments on commit 4fa982d

Please sign in to comment.