Navigation Menu

Skip to content

Commit

Permalink
Merge pull request netgen#50 from MarioBlazek/templating_tests
Browse files Browse the repository at this point in the history
Templating tests
  • Loading branch information
emodric committed Feb 8, 2017
2 parents 4fa8eb1 + 3dd7b99 commit 46a54ff
Show file tree
Hide file tree
Showing 7 changed files with 338 additions and 2 deletions.
Expand Up @@ -54,7 +54,14 @@ public function setUp()
->setMethods(array('isRedirect', 'setStatusCode'))
->getMock();

$this->platformUiRequest = new Request(array(), array(), array('_route' => 'netgen_tags_admin'), array(), array(), array('HTTP_X-PJAX' => 'weeee', 'HTTP_X-Requested-With' => 'XMLHttpRequest'));
$this->platformUiRequest = new Request(
array(),
array(),
array('_route' => 'netgen_tags_admin'),
array(),
array(),
array('HTTP_X-PJAX' => 'weeee', 'HTTP_X-Requested-With' => 'XMLHttpRequest')
);

$this->event = $this->getMockBuilder(FilterResponseEvent::class)
->disableOriginalConstructor()
Expand Down
Expand Up @@ -52,7 +52,14 @@ public function setUp()
->setMethods(array('isMasterRequest', 'getRequest'))
->getMock();

$this->request = new Request(array(), array(), array('_route' => 'netgen_tags_admin'), array(), array(), array('HTTP_X-PJAX' => 'weeee', 'HTTP_X-Requested-With' => 'XMLHttpRequest'));
$this->request = new Request(
array(),
array(),
array('_route' => 'netgen_tags_admin'),
array(),
array(),
array('HTTP_X-PJAX' => 'weeee', 'HTTP_X-Requested-With' => 'XMLHttpRequest')
);

$this->listener = new SetAdminPageLayoutRequestListener($this->globalVariable, $this->pageLayoutTemplate);
}
Expand Down
41 changes: 41 additions & 0 deletions Tests/Templating/Twig/AdminGlobalVariableTest.php
@@ -0,0 +1,41 @@
<?php

namespace Netgen\TagsBundle\Tests\Templating\Twig;

use Netgen\TagsBundle\Templating\Twig\AdminGlobalVariable;
use PHPUnit\Framework\TestCase;

class AdminGlobalVariableTest extends TestCase
{
/**
* @var \Netgen\TagsBundle\Templating\Twig\AdminGlobalVariable
*/
protected $adminGlobalVariable;

/**
* @var string
*/
protected $template;

public function setUp()
{
$this->adminGlobalVariable = new AdminGlobalVariable();
$this->template = 'AcmeBundle::pagelayout.html.twig';
}

public function testInstanceOfGlobalAdminVariable()
{
$this->assertInstanceOf(AdminGlobalVariable::class, $this->adminGlobalVariable);
}

public function testGetAndSetPageLayoutTemplate()
{
$this->assertNull($this->adminGlobalVariable->getPageLayoutTemplate());

$this->adminGlobalVariable->setPageLayoutTemplate($this->template);
$this->assertEquals($this->template, $this->adminGlobalVariable->getPageLayoutTemplate());

$this->adminGlobalVariable->setPageLayoutTemplate();
$this->assertNull($this->adminGlobalVariable->getPageLayoutTemplate());
}
}
48 changes: 48 additions & 0 deletions Tests/Templating/Twig/Extension/AdminExtensionTest.php
@@ -0,0 +1,48 @@
<?php

namespace Netgen\TagsBundle\Tests\Templating\Twig\Extension;

use Netgen\TagsBundle\Templating\Twig\AdminGlobalVariable;
use Netgen\TagsBundle\Templating\Twig\Extension\AdminExtension;
use PHPUnit\Framework\TestCase;
use Twig_Extension;
use Twig_Extension_GlobalsInterface;

class AdminExtensionTest extends TestCase
{
/**
* @var \Netgen\TagsBundle\Templating\Twig\Extension\AdminExtension
*/
protected $extension;

/**
* @var \Netgen\TagsBundle\Templating\Twig\AdminGlobalVariable
*/
protected $adminGlobalVariable;

public function setUp()
{
$this->adminGlobalVariable = new AdminGlobalVariable();
$this->extension = new AdminExtension($this->adminGlobalVariable);
}

public function testInstanceOfTwigExtension()
{
$this->assertInstanceOf(Twig_Extension::class, $this->extension);
}

public function testInstanceOfTwigExtensionGlobalsInterface()
{
$this->assertInstanceOf(Twig_Extension_GlobalsInterface::class, $this->extension);
}

public function testGetName()
{
$this->assertEquals('Netgen\TagsBundle\Templating\Twig\Extension\AdminExtension', $this->extension->getName());
}

public function testGetGlobals()
{
$this->assertEquals(array('eztags_admin' => $this->adminGlobalVariable), $this->extension->getGlobals());
}
}
222 changes: 222 additions & 0 deletions Tests/Templating/Twig/Extension/NetgenTagsExtensionTest.php
@@ -0,0 +1,222 @@
<?php

namespace Netgen\TagsBundle\Tests\Templating\Twig\Extension;

use eZ\Publish\API\Repository\Values\Content\Language;
use eZ\Publish\Core\Base\Exceptions\NotFoundException;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\Repository\ContentTypeService;
use eZ\Publish\Core\Repository\LanguageService;
use eZ\Publish\Core\Repository\Values\ContentType\ContentType;
use Netgen\TagsBundle\API\Repository\Values\Tags\Tag;
use Netgen\TagsBundle\Core\Repository\TagsService;
use Netgen\TagsBundle\Templating\Twig\Extension\NetgenTagsExtension;
use PHPUnit\Framework\TestCase;
use Twig_Extension;
use Twig_SimpleFunction;

class NetgenTagsExtensionTest extends TestCase
{
/**
* @var \Netgen\TagsBundle\Templating\Twig\Extension\NetgenTagsExtension
*/
protected $extension;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $tagsService;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $translationHelper;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $languageService;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $contentTypeService;

/**
* @var \Netgen\TagsBundle\API\Repository\Values\Tags\Tag
*/
protected $tag;

/**
* @var \eZ\Publish\Core\Repository\Values\ContentType\ContentType
*/
protected $contentType;

public function setUp()
{
$this->tagsService = $this->getMockBuilder(TagsService::class)
->disableOriginalConstructor()
->setMethods(array('loadTag'))
->getMock();

$this->translationHelper = $this->getMockBuilder(TranslationHelper::class)
->disableOriginalConstructor()
->setMethods(array('getTranslatedByMethod'))
->getMock();

$this->languageService = $this->getMockBuilder(LanguageService::class)
->disableOriginalConstructor()
->setMethods(array('loadLanguage'))
->getMock();

$this->contentTypeService = $this->getMockBuilder(ContentTypeService::class)
->disableOriginalConstructor()
->setMethods(array('loadContentType'))
->getMock();

$this->extension = new NetgenTagsExtension(
$this->tagsService,
$this->translationHelper,
$this->languageService,
$this->contentTypeService
);

$this->tag = new Tag();
$this->contentType = new ContentType(array('fieldDefinitions' => array()));
}

public function testInstanceOfTwigExtension()
{
$this->assertInstanceOf(Twig_Extension::class, $this->extension);
}

public function testGetName()
{
$this->assertEquals(NetgenTagsExtension::class, $this->extension->getName());
}

public function testGetFunctions()
{
$functions = array(
new Twig_SimpleFunction(
'netgen_tags_tag_keyword',
array($this->extension, 'getTagKeyword')
),
new Twig_SimpleFunction(
'netgen_tags_language_name',
array($this->extension, 'getLanguageName')
),
new Twig_SimpleFunction(
'netgen_tags_content_type_name',
array($this->extension, 'getContentTypeName')
),
);

$this->assertEquals($functions, $this->extension->getFunctions());
}

public function testGetTagKeywordWithNotFoundException()
{
$this->tagsService->expects($this->once())
->method('loadTag')
->willThrowException(new NotFoundException('tag', 'tag'));

$this->assertEmpty($this->extension->getTagKeyword(1));
}

public function testGetTagKeywordWithNonTagArgument()
{
$translated = 'translated';

$this->tagsService->expects($this->once())
->method('loadTag')
->willReturn($this->tag);

$this->translationHelper->expects($this->once())
->method('getTranslatedByMethod')
->with($this->tag)
->willReturn($translated);

$this->assertEquals($translated, $this->extension->getTagKeyword(1));
}

public function testGetTagKeywordWithTagArgument()
{
$translated = 'translated';

$this->tagsService->expects($this->never())
->method('loadTag');

$this->translationHelper->expects($this->once())
->method('getTranslatedByMethod')
->with($this->tag)
->willReturn($translated);

$this->assertEquals($translated, $this->extension->getTagKeyword($this->tag));
}

public function testGetLanguageName()
{
$language = new Language(
array(
'id' => 123,
'languageCode' => 'eng-EU',
'name' => 'English',
)
);

$this->languageService->expects($this->once())
->method('loadLanguage')
->with($language->languageCode)
->willReturn($language);

$name = $this->extension->getLanguageName($language->languageCode);

$this->assertEquals($language->name, $name);
}

public function testGetContentTypeNameWithNotFoundException()
{
$contentType = 'content_type';

$this->contentTypeService->expects($this->once())
->method('loadContentType')
->with($contentType)
->willThrowException(new NotFoundException($contentType, $contentType));

$this->translationHelper->expects($this->never())
->method('getTranslatedByMethod');

$this->assertEmpty($this->extension->getContentTypeName('content_type'));
}

public function testGetContentTypeNameWithNonContentTypeAsArgument()
{
$contentType = 'content_type';

$this->contentTypeService->expects($this->once())
->method('loadContentType')
->with($contentType)
->willReturn($this->contentType);

$this->translationHelper->expects($this->once())
->method('getTranslatedByMethod')
->with($this->contentType, 'getName')
->willReturn('Translated name');

$this->assertEquals('Translated name', $this->extension->getContentTypeName('content_type'));
}

public function testGetContentTypeNameWithContentTypeAsArgument()
{
$this->contentTypeService->expects($this->never())
->method('loadContentType');

$this->translationHelper->expects($this->once())
->method('getTranslatedByMethod')
->with($this->contentType, 'getName')
->willReturn('Translated name');

$this->assertEquals('Translated name', $this->extension->getContentTypeName($this->contentType));
}
}
8 changes: 8 additions & 0 deletions codecov.yml
@@ -0,0 +1,8 @@
coverage:
status:
patch: false
changes: false
project:
default:
target: auto
comment: off
3 changes: 3 additions & 0 deletions phpunit.xml
Expand Up @@ -16,6 +16,9 @@
<testsuite name="Netgen\TagsBundle\Tests\PlatformUI">
<directory>Tests/PlatformUI</directory>
</testsuite>
<testsuite name="Netgen\TagsBundle\Tests\Templating">
<directory>Tests/Templating</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
Expand Down

0 comments on commit 46a54ff

Please sign in to comment.