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

Commit

Permalink
added testa and refactored translated views
Browse files Browse the repository at this point in the history
  • Loading branch information
pablodip committed Oct 25, 2013
1 parent 1d19bb7 commit 5303cf6
Show file tree
Hide file tree
Showing 10 changed files with 476 additions and 80 deletions.
50 changes: 50 additions & 0 deletions Tests/View/DefaultTranslatedViewTest.php
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WhiteOctober\PagerfantaBundle\Tests\View;

class DefaultTranslatedViewTest extends TranslatedViewTest
{
protected function viewClass()
{
return 'Pagerfanta\View\DefaultView';
}

protected function translatedViewClass()
{
return 'WhiteOctober\PagerfantaBundle\View\DefaultTranslatedView';
}

protected function previousMessageOption()
{
return 'previous_message';
}

protected function nextMessageOption()
{
return 'next_message';
}

protected function buildPreviousMessage($text)
{
return sprintf('&#171; %s', $text);
}

protected function buildNextMessage($text)
{
return sprintf('%s &#187;', $text);
}

protected function translatedViewName()
{
return 'default_translated';
}
}
182 changes: 182 additions & 0 deletions Tests/View/TranslatedViewTest.php
@@ -0,0 +1,182 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WhiteOctober\PagerfantaBundle\Tests\View;

abstract class TranslatedViewTest extends \PHPUnit_Framework_TestCase
{
private $view;
private $translator;

private $translatedView;

private $pagerfanta;
private $routeGenerator;

protected function setUp()
{
$this->view = $this->createViewMock();
$this->translator = $this->createTranslatorMock();

$this->translatedView = $this->createTranslatedView();

$this->pagerfanta = $this->createPagerfantaMock();
$this->routeGenerator = $this->createRouteGenerator();
}

private function createViewMock()
{
return $this->getMock($this->viewClass());
}

abstract protected function viewClass();

private function createTranslatorMock()
{
return $this->getMock('Symfony\Component\Translation\TranslatorInterface');
}

private function createTranslatedView()
{
$class = $this->translatedViewClass();

return new $class($this->view, $this->translator);
}

abstract protected function translatedViewClass();

private function createPagerfantaMock()
{
return $this->getMockBuilder('Pagerfanta\Pagerfanta')
->disableOriginalConstructor()
->getMock();
}

private function createRouteGenerator()
{
return function () { };
}

public function testRenderShouldTranslatePreviuosAndNextMessage()
{
$this->translatorExpectsPreviousAt(0);
$this->translatorExpectsNextAt(1);

$options = array();

$this->assertRender($options);
}

public function testRenderAllowsCustomizingPreviousMessageWithOption()
{
$this->translatorExpectsNextAt(0);

$previousMessageOption = $this->previousMessageOption();
$options = array($previousMessageOption => $this->previousMessage());

$this->assertRender($options);
}

public function testRenderAllowsCustomizingNextMessageWithOption()
{
$this->translatorExpectsPreviousAt(0);

$nextMessageOption = $this->nextMessageOption();
$options = array($nextMessageOption => $this->nextMessage());

$this->assertRender($options);
}

private function translatorExpectsPreviousAt($at)
{
$previous = $this->previous();

$this->translator
->expects($this->at($at))
->method('trans')
->with('previous', array(), 'pagerfanta')
->will($this->returnValue($previous));
}

private function translatorExpectsNextAt($at)
{
$next = $this->next();

$this->translator
->expects($this->at($at))
->method('trans')
->with('next', array(), 'pagerfanta')
->will($this->returnValue($next));
}

private function assertRender($options)
{
$previousMessageOption = $this->previousMessageOption();
$nextMessageOption = $this->nextMessageOption();

$previous = $this->previous();
$next = $this->next();

$expectedOptions = array(
$previousMessageOption => $this->buildPreviousMessage($previous),
$nextMessageOption => $this->buildNextMessage($next)
);

$result = new \stdClass();

$this->view
->expects($this->once())
->method('render')
->with($this->pagerfanta, $this->routeGenerator, $expectedOptions)
->will($this->returnvalue($result));

$rendered = $this->translatedView->render($this->pagerfanta, $this->routeGenerator, $options);

$this->assertSame($result, $rendered);
}

abstract protected function previousMessageOption();

abstract protected function nextMessageOption();

private function previous()
{
return 'Anterior';
}

private function next()
{
return 'Siguiente';
}

private function previousMessage()
{
return $this->buildPreviousMessage($this->previous());
}

private function nextMessage()
{
return $this->buildNextMessage($this->next());
}

abstract protected function buildPreviousMessage($text);

abstract protected function buildNextMessage($text);

public function testGetNameShouldReturnTheName()
{
$name = $this-> translatedViewName();

$this->assertSame($name, $this->translatedView->getName());
}

abstract protected function translatedViewName();
}
30 changes: 30 additions & 0 deletions Tests/View/TwitterBootstrap3TranslatedViewTest.php
@@ -0,0 +1,30 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WhiteOctober\PagerfantaBundle\Tests\View;

class TwitterBootstrap3TranslatedView extends TwitterBootstrapTranslatedViewTest
{
protected function viewClass()
{
return 'Pagerfanta\View\TwitterBootstrap3View';
}

protected function translatedViewClass()
{
return 'WhiteOctober\PagerfantaBundle\View\TwitterBootstrap3TranslatedView';
}

protected function translatedViewName()
{
return 'twitter_bootstrap3_translated';
}
}
50 changes: 50 additions & 0 deletions Tests/View/TwitterBootstrapTranslatedViewTest.php
@@ -0,0 +1,50 @@
<?php

/*
* This file is part of the Pagerfanta package.
*
* (c) Pablo Díez <pablodip@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace WhiteOctober\PagerfantaBundle\Tests\View;

class TwitterBootstrapTranslatedViewTest extends TranslatedViewTest
{
protected function viewClass()
{
return 'Pagerfanta\View\TwitterBootstrapView';
}

protected function translatedViewClass()
{
return 'WhiteOctober\PagerfantaBundle\View\TwitterBootstrapTranslatedView';
}

protected function previousMessageOption()
{
return 'prev_message';
}

protected function nextMessageOption()
{
return 'next_message';
}

protected function buildPreviousMessage($text)
{
return sprintf('&larr; %s', $text);
}

protected function buildNextMessage($text)
{
return sprintf('%s &rarr;', $text);
}

protected function translatedViewName()
{
return 'twitter_bootstrap_translated';
}
}
3 changes: 3 additions & 0 deletions Tests/bootstrap.php
@@ -0,0 +1,3 @@
<?php

$loader = require __DIR__.'/../vendor/autoload.php';
36 changes: 13 additions & 23 deletions View/DefaultTranslatedView.php
Expand Up @@ -23,36 +23,26 @@
*
* @author Jérôme Tamarelle <jerome@tamarelle.net>
*/
class DefaultTranslatedView implements ViewInterface
class DefaultTranslatedView extends TranslatedView
{
private $view;
private $translator;
protected function previousMessageOption()
{
return 'previous_message';
}

/**
* Constructor.
*
* @param DefaultViewInterface $view A default view.
* @param TranslatorInterface $translator A translator interface.
*/
public function __construct(DefaultView $view, TranslatorInterface $translator)
protected function nextMessageOption()
{
$this->view = $view;
$this->translator = $translator;
return 'next_message';
}

/**
* {@inheritdoc}
*/
public function render(PagerfantaInterface $pagerfanta, $routeGenerator, array $options = array())
protected function buildPreviousMessage($text)
{
if (!isset($options['previous_message'])) {
$options['previous_message'] = '&#171; '.$this->translator->trans('previous', array(), 'pagerfanta');
}
if (!isset($options['next_message'])) {
$options['next_message'] = $this->translator->trans('next', array(), 'pagerfanta').' &#187;';
}
return sprintf('&#171; %s', $text);
}

return $this->view->render($pagerfanta, $routeGenerator, $options);
protected function buildNextMessage($text)
{
return sprintf('%s &#187;', $text);
}

/**
Expand Down

0 comments on commit 5303cf6

Please sign in to comment.