From 4fa982d61d950bc3ec18d759dc11fe05530c4500 Mon Sep 17 00:00:00 2001 From: mamuz Date: Fri, 5 Sep 2014 23:01:44 +0200 Subject: [PATCH] add ConfigProvider and UTs --- .../Controller/FeedController.php | 36 +++----- .../Controller/FeedControllerFactory.php | 6 +- src/MamuzBlogFeed/Filter/Query.php | 45 ++++++---- src/MamuzBlogFeed/Hydrator/Mutator.php | 2 +- .../Listener/AggregateFactory.php | 5 +- src/MamuzBlogFeed/Options/ConfigProvider.php | 35 ++++++++ .../Options/ConfigProviderInterface.php | 12 +++ .../Controller/FeedControllerFactoryTest.php | 2 + .../Controller/FeedControllerTest.php | 39 ++++----- .../Extractor/FeedEntryTest.php | 82 +++++++++++++++++++ .../Feed/Writer/FactoryTest.php | 52 ++++++++++++ tests/MamuzBlogFeedTest/Filter/QueryTest.php | 37 ++++----- .../Hydrator/MutatorTest.php | 37 +++++++++ .../Options/ConfigProviderTest.php | 49 +++++++++++ 14 files changed, 353 insertions(+), 86 deletions(-) create mode 100644 src/MamuzBlogFeed/Options/ConfigProvider.php create mode 100644 src/MamuzBlogFeed/Options/ConfigProviderInterface.php create mode 100644 tests/MamuzBlogFeedTest/Extractor/FeedEntryTest.php create mode 100644 tests/MamuzBlogFeedTest/Feed/Writer/FactoryTest.php create mode 100644 tests/MamuzBlogFeedTest/Hydrator/MutatorTest.php create mode 100644 tests/MamuzBlogFeedTest/Options/ConfigProviderTest.php diff --git a/src/MamuzBlogFeed/Controller/FeedController.php b/src/MamuzBlogFeed/Controller/FeedController.php index 1c4cbe8..6df349e 100644 --- a/src/MamuzBlogFeed/Controller/FeedController.php +++ b/src/MamuzBlogFeed/Controller/FeedController.php @@ -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; @@ -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) @@ -52,7 +59,7 @@ 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; @@ -60,23 +67,4 @@ public function postsAction() 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(); - } } diff --git a/src/MamuzBlogFeed/Controller/FeedControllerFactory.php b/src/MamuzBlogFeed/Controller/FeedControllerFactory.php index 9c036ca..2933855 100644 --- a/src/MamuzBlogFeed/Controller/FeedControllerFactory.php +++ b/src/MamuzBlogFeed/Controller/FeedControllerFactory.php @@ -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; @@ -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); } } diff --git a/src/MamuzBlogFeed/Filter/Query.php b/src/MamuzBlogFeed/Filter/Query.php index c5d1f69..7fddcf3 100644 --- a/src/MamuzBlogFeed/Filter/Query.php +++ b/src/MamuzBlogFeed/Filter/Query.php @@ -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) @@ -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; + } + } diff --git a/src/MamuzBlogFeed/Hydrator/Mutator.php b/src/MamuzBlogFeed/Hydrator/Mutator.php index 30e8af3..948e1a8 100644 --- a/src/MamuzBlogFeed/Hydrator/Mutator.php +++ b/src/MamuzBlogFeed/Hydrator/Mutator.php @@ -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); diff --git a/src/MamuzBlogFeed/Listener/AggregateFactory.php b/src/MamuzBlogFeed/Listener/AggregateFactory.php index 4055725..6505dde 100644 --- a/src/MamuzBlogFeed/Listener/AggregateFactory.php +++ b/src/MamuzBlogFeed/Listener/AggregateFactory.php @@ -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; @@ -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); } diff --git a/src/MamuzBlogFeed/Options/ConfigProvider.php b/src/MamuzBlogFeed/Options/ConfigProvider.php new file mode 100644 index 0000000..abf2c7d --- /dev/null +++ b/src/MamuzBlogFeed/Options/ConfigProvider.php @@ -0,0 +1,35 @@ +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(); + } +} diff --git a/src/MamuzBlogFeed/Options/ConfigProviderInterface.php b/src/MamuzBlogFeed/Options/ConfigProviderInterface.php new file mode 100644 index 0000000..2ea7240 --- /dev/null +++ b/src/MamuzBlogFeed/Options/ConfigProviderInterface.php @@ -0,0 +1,12 @@ +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); @@ -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); diff --git a/tests/MamuzBlogFeedTest/Controller/FeedControllerTest.php b/tests/MamuzBlogFeedTest/Controller/FeedControllerTest.php index 1a2cf2a..2cfd75b 100644 --- a/tests/MamuzBlogFeedTest/Controller/FeedControllerTest.php +++ b/tests/MamuzBlogFeedTest/Controller/FeedControllerTest.php @@ -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(); @@ -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); @@ -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); diff --git a/tests/MamuzBlogFeedTest/Extractor/FeedEntryTest.php b/tests/MamuzBlogFeedTest/Extractor/FeedEntryTest.php new file mode 100644 index 0000000..8aed9c7 --- /dev/null +++ b/tests/MamuzBlogFeedTest/Extractor/FeedEntryTest.php @@ -0,0 +1,82 @@ + 'hashed', + 'link' => 'href', + 'title' => 'foo', + 'description' => 'bar', + 'content' => 'baz', + 'dateModified' => '2012-12-12', + 'dateCreated' => '2011-12-12', + ); + + protected function setUp() + { + $this->post = \Mockery::mock('MamuzBlog\Entity\Post'); + $this->renderer = \Mockery::mock('Zend\View\Renderer\RendererInterface'); + $this->fixture = new FeedEntry($this->renderer); + } + + protected function prepareMocks() + { + $this->post->shouldReceive('getId')->once()->andReturn($this->exp['id']); + $this->post->shouldReceive('getTitle')->once()->andReturn($this->exp['title']); + $this->post->shouldReceive('getDescription')->once()->andReturn($this->exp['description']); + $this->post->shouldReceive('getContent')->once()->andReturn($this->exp['content']); + $this->post->shouldReceive('getModifiedAt')->once()->andReturn($this->exp['dateModified']); + $this->post->shouldReceive('getCreatedAt')->once()->andReturn($this->exp['dateCreated']); + + $this->renderer->shouldReceive('hashId')->once()->with($this->exp['id'])->andReturn($this->exp['id']); + $this->renderer->shouldReceive('permaLinkPost')->once()->with($this->post)->andReturn($this->exp['link']); + $this->renderer->shouldReceive('markdown')->once() + ->with($this->exp['description'])->andReturn($this->exp['description']); + $this->renderer->shouldReceive('markdown')->once() + ->with($this->exp['content'])->andReturn($this->exp['content']); + } + + public function testImplementingExtractionInterface() + { + $this->assertInstanceOf('Zend\Stdlib\Extractor\ExtractionInterface', $this->fixture); + } + + public function testExtractionWithWrongValue() + { + $this->assertSame(array(), $this->fixture->extract(1)); + } + + public function testExtractionWithoutTags() + { + $this->prepareMocks(); + $this->post->shouldReceive('getTags')->once()->andReturn(array()); + $this->assertSame($this->exp, $this->fixture->extract($this->post)); + } + + public function testExtractionWithTags() + { + $this->exp['categories'] = array(array('term' => 'foo', 'scheme' => 'bar')); + + $tag = \Mockery::mock('MamuzBlog\Entity\Tag'); + $tag->shouldReceive('getName')->andReturn('foo'); + $this->renderer->shouldReceive('permaLinkTag')->with('foo')->andReturn('bar'); + + $this->prepareMocks(); + $this->post->shouldReceive('getTags')->once()->andReturn(array($tag)); + $this->assertSame($this->exp, $this->fixture->extract($this->post)); + } +} diff --git a/tests/MamuzBlogFeedTest/Feed/Writer/FactoryTest.php b/tests/MamuzBlogFeedTest/Feed/Writer/FactoryTest.php new file mode 100644 index 0000000..c52b621 --- /dev/null +++ b/tests/MamuzBlogFeedTest/Feed/Writer/FactoryTest.php @@ -0,0 +1,52 @@ +extractor = \Mockery::mock('Zend\Stdlib\Extractor\ExtractionInterface'); + $this->hydrator = \Mockery::mock('Zend\Stdlib\Hydrator\HydrationInterface'); + + $this->fixture = new Factory($this->extractor, $this->hydrator); + } + + public function testImplementingFactoryInterface() + { + $this->assertInstanceOf('MamuzBlogFeed\Feed\Writer\FactoryInterface', $this->fixture); + } + + public function testCreation() + { + $post = \Mockery::mock('MamuzBlog\Entity\Post'); + $options = array('foo'); + $this->extractor->shouldReceive('extract')->with($post)->andReturn($options); + $this->hydrator->shouldReceive('hydrate')->andReturnUsing( + function ($data, $object) use ($options) { + if ($object instanceof \Zend\Feed\Writer\Feed) { + $this->assertSame('rss', $object->getType()); + } else { + $this->assertInstanceOf('Zend\Feed\Writer\Entry', $object); + } + $this->assertSame($options, $data); + return $object; + } + ); + + $posts = new \ArrayObject(array($post)); + + $this->assertInstanceOf('Zend\Feed\Writer\Feed', $this->fixture->create($options, $posts)); + } +} diff --git a/tests/MamuzBlogFeedTest/Filter/QueryTest.php b/tests/MamuzBlogFeedTest/Filter/QueryTest.php index 69fc5b4..1f8d44d 100644 --- a/tests/MamuzBlogFeedTest/Filter/QueryTest.php +++ b/tests/MamuzBlogFeedTest/Filter/QueryTest.php @@ -12,11 +12,19 @@ class QueryTest extends \PHPUnit_Framework_TestCase /** @var \Doctrine\ORM\Query | \Mockery\MockInterface */ protected $query; + /** @var \MamuzBlogFeed\Options\ConfigProviderInterface |\Mockery\MockInterface */ + protected $configProvider; + + /** @var string */ + protected $tag = 'foo'; + protected function setUp() { $this->query = \Mockery::mock('Doctrine\ORM\AbstractQuery'); $this->query->shouldReceive('setFirstResult')->with(1)->andReturnSelf(); - $this->fixture = new Query(); + $this->query->shouldReceive('getParameter')->with('tag')->andReturnNull($this->tag); + $this->configProvider = \Mockery::mock('MamuzBlogFeed\Options\ConfigProviderInterface'); + $this->fixture = new Query($this->configProvider); } public function testImplementingFilterInterface() @@ -30,36 +38,27 @@ public function testFilteringWrongValueType() $this->assertSame($value, $this->fixture->filter($value)); } - public function testFilteringWithoutTagParameter() + public function testFilteringWithDefaultMaxResults() { - $this->query->shouldReceive('getParameter')->with('tag')->andReturnNull(); + $this->configProvider->shouldReceive('getFor')->with($this->tag)->andReturn(array()); $this->query->shouldReceive('setMaxResults')->with(100); $this->assertSame($this->query, $this->fixture->filter($this->query)); } - public function testFilteringWithoutTagParameterAndDefaultConfig() - { - $this->fixture = new Query(array('default' => array('maxResults' => 200))); - $this->query->shouldReceive('getParameter')->with('tag')->andReturnNull(); - $this->query->shouldReceive('setMaxResults')->with(200); - - $this->assertSame($this->query, $this->fixture->filter($this->query)); - } - - public function testFilteringWithTagParameterAndConfig() + public function testFilteringWithUserDefaultMaxResults() { - $this->fixture = new Query(array('foo' => array('maxResults' => 200))); - $this->query->shouldReceive('getParameter')->with('tag')->andReturn('foo'); - $this->query->shouldReceive('setMaxResults')->with(200); + $this->fixture = new Query($this->configProvider, 89); + $this->configProvider->shouldReceive('getFor')->with($this->tag)->andReturn(array()); + $this->query->shouldReceive('setMaxResults')->with(89); $this->assertSame($this->query, $this->fixture->filter($this->query)); } - public function testFilteringWithTagParameterAndWithoutConfig() + public function testFilteringWithMaxResultsFromConfig() { - $this->query->shouldReceive('getParameter')->with('tag')->andReturn('foo'); - $this->query->shouldReceive('setMaxResults')->with(100); + $this->configProvider->shouldReceive('getFor')->with($this->tag)->andReturn(array('maxResults' => 400)); + $this->query->shouldReceive('setMaxResults')->with(400); $this->assertSame($this->query, $this->fixture->filter($this->query)); } diff --git a/tests/MamuzBlogFeedTest/Hydrator/MutatorTest.php b/tests/MamuzBlogFeedTest/Hydrator/MutatorTest.php new file mode 100644 index 0000000..da75632 --- /dev/null +++ b/tests/MamuzBlogFeedTest/Hydrator/MutatorTest.php @@ -0,0 +1,37 @@ +fixture = new Mutator; + } + + public function testImplementingHydrationInterface() + { + $this->assertInstanceOf('Zend\Stdlib\Hydrator\HydrationInterface', $this->fixture); + } + + public function testHydration() + { + $object = new Entry; + + $data = array( + 'id' => 'foo', + 'author' => array('name' => 'bar'), + ); + + $this->fixture->hydrate($data, $object); + + $this->assertSame($data['id'], $object->getId()); + $this->assertSame(array($data['author']), $object->getAuthors()); + } +} diff --git a/tests/MamuzBlogFeedTest/Options/ConfigProviderTest.php b/tests/MamuzBlogFeedTest/Options/ConfigProviderTest.php new file mode 100644 index 0000000..60e28f8 --- /dev/null +++ b/tests/MamuzBlogFeedTest/Options/ConfigProviderTest.php @@ -0,0 +1,49 @@ + array(12, 13), 'default' => array(1, 3)); + + protected function setUp() + { + $this->fixture = new ConfigProvider($this->config); + } + + public function testImplementingConfigProviderInterface() + { + $this->assertInstanceOf('MamuzBlogFeed\Options\ConfigProviderInterface', $this->fixture); + } + + public function testGettingDefault() + { + $this->assertSame($this->config['default'], $this->fixture->getFor()); + $this->assertSame($this->config['default'], $this->fixture->getFor(132)); + $this->assertSame($this->config['default'], $this->fixture->getFor(array())); + } + + public function testGettingUserDefault() + { + $this->fixture = new ConfigProvider($this->config, 'foo'); + $this->assertSame($this->config['foo'], $this->fixture->getFor()); + $this->assertSame($this->config['foo'], $this->fixture->getFor(132)); + $this->assertSame($this->config['foo'], $this->fixture->getFor(array())); + } + + public function testGetting() + { + $this->assertSame($this->config['foo'], $this->fixture->getFor('foo')); + } + + public function testGettingEmpty() + { + $this->assertSame(array(), $this->fixture->getFor('bar')); + } +}