This repository has been archived by the owner on Nov 25, 2020. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added testa and refactored translated views
- Loading branch information
Showing
10 changed files
with
476 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('« %s', $text); | ||
} | ||
|
||
protected function buildNextMessage($text) | ||
{ | ||
return sprintf('%s »', $text); | ||
} | ||
|
||
protected function translatedViewName() | ||
{ | ||
return 'default_translated'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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('← %s', $text); | ||
} | ||
|
||
protected function buildNextMessage($text) | ||
{ | ||
return sprintf('%s →', $text); | ||
} | ||
|
||
protected function translatedViewName() | ||
{ | ||
return 'twitter_bootstrap_translated'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<?php | ||
|
||
$loader = require __DIR__.'/../vendor/autoload.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.