diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..61d2be7 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,41 @@ +language: php + +cache: + directories: + - vendor + +matrix: + # mark as finished before allow_failures are run + fast_finish: true + include: + - php: 5.5 + - php: 5.6 + - php: 7.0 + +# test only master (+ pull requests) +branches: + only: + - master + +# make sure to update composer to latest available version +before_install: + - phpenv config-add travis.php.ini + - phpenv config-rm xdebug.ini + +# install dependencies +install: composer install + +# execute phpunit as the script command +script: + - ./vendor/bin/phpunit -d memory_limit=-1 --colors -c phpunit.xml + +# disable mail notifications +notification: + email: false + +# reduce depth (history) of git checkout +git: + depth: 30 + +# we don't need sudo +sudo: false diff --git a/README.md b/README.md index 6e7697b..8a5bafb 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,8 @@ Netgen Open Graph Bundle ======================== +[![Build Status](https://img.shields.io/travis/netgen/NetgenOpenGraphBundle.svg?style=flat-square)](https://travis-ci.org/netgen/NetgenOpenGraphBundle) + Netgen Open Graph Bundle is an eZ Publish / eZ Platform bundle that provides simple integration with Open Graph protocol. It uses `Content` object which is currently displayed to generate Open Graph meta tags, according to configuration. diff --git a/Tests/Exception/FieldEmptyExceptionTest.php b/Tests/Exception/FieldEmptyExceptionTest.php new file mode 100644 index 0000000..5b8855a --- /dev/null +++ b/Tests/Exception/FieldEmptyExceptionTest.php @@ -0,0 +1,17 @@ +fieldHelper = $this->getMockBuilder(FieldHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('isFieldEmpty')) + ->getMock(); + + $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('getTranslatedField')) + ->getMock(); + + $this->content = new Content(array('versionInfo' => new VersionInfo())); + + $this->variationHandler = $this->getMockBuilder(AliasGenerator::class) + ->disableOriginalConstructor() + ->setMethods(array('getVariation')) + ->getMock(); + + $this->requestStack = $this->getMockBuilder(RequestStack::class) + ->disableOriginalConstructor() + ->setMethods(array('getCurrentRequest')) + ->getMock(); + + $this->logger = $this->getMockBuilder(NullLogger::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->image = new Image($this->fieldHelper, $this->translationHelper, $this->variationHandler, $this->requestStack, $this->logger); + $this->image->setContent($this->content); + + $this->field = new Field(array('value' => new Value())); + } + + public function testInstanceOfHandlerInterface() + { + $this->assertInstanceOf(HandlerInterface::class, $this->image); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. + */ + public function testGettingTagsWithoutFieldIdentifier() + { + $this->image->getMetaTags('some_tag', array()); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. + */ + public function testGettingTagsWithNonExistentField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(null); + + $this->image->getMetaTags('some_tag', array('some_value')); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\Image field type handler does not support field with identifier ''. + */ + public function testGettingTagsWithUnsupportedField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(new Field()); + + $this->image->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithEmptyField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(true); + + $this->image->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithExceptionThrownByVariationHandler() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $request = $this->getMockBuilder(Request::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->requestStack->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request); + + $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); + } + + public function testGettingTags() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(false); + + $variation = new Variation(array('uri' => '/some/uri')); + + $this->variationHandler->expects($this->once()) + ->method('getVariation') + ->willReturn($variation); + + $request = $this->getMockBuilder(Request::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->requestStack->expects($this->exactly(2)) + ->method('getCurrentRequest') + ->willReturn($request); + + $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); + } + + public function testGettingTagsWithVariationServiceThrowsInvalidVariationException() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(false); + + $this->variationHandler->expects($this->once()) + ->method('getVariation') + ->willThrowException(new InvalidVariationException('name', 'type')); + + $request = $this->getMockBuilder(Request::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->requestStack->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request); + + $this->logger->expects($this->once()) + ->method('error'); + + $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); + } + + public function testGettingTagsWithVariationServiceThrowsSourceImageNotFoundException() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(false); + + $this->variationHandler->expects($this->once()) + ->method('getVariation') + ->willThrowException(new SourceImageNotFoundException('message')); + + $request = $this->getMockBuilder(Request::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->requestStack->expects($this->once()) + ->method('getCurrentRequest') + ->willReturn($request); + + $this->logger->expects($this->once()) + ->method('error'); + + $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2', 'some_value_3')); + } + + public function testGettingTagsWithMultipleArgumentsInArray() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->image->getMetaTags('some_tag', array('some_value', 'some_value_2')); + } +} diff --git a/Tests/Handler/FieldType/TextBlockTest.php b/Tests/Handler/FieldType/TextBlockTest.php new file mode 100644 index 0000000..9812746 --- /dev/null +++ b/Tests/Handler/FieldType/TextBlockTest.php @@ -0,0 +1,113 @@ +fieldHelper = $this->getMockBuilder(FieldHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('isFieldEmpty')) + ->getMock(); + + $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('getTranslatedField')) + ->getMock(); + + $this->content = $this->getMockBuilder(Content::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->textBlock = new TextBlock($this->fieldHelper, $this->translationHelper); + $this->textBlock->setContent($this->content); + + $this->field = new Field(array('value' => new Value())); + } + + public function testInstanceOfHandlerInterface() + { + $this->assertInstanceOf(HandlerInterface::class, $this->textBlock); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. + */ + public function testGettingTagsWithoutFieldIdentifier() + { + $this->textBlock->getMetaTags('some_tag', array()); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. + */ + public function testGettingTagsWithNonExistentField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(null); + + $this->textBlock->getMetaTags('some_tag', array('some_value')); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\TextBlock field type handler does not support field with identifier ''. + */ + public function testGettingTagsWithUnsupportedField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(new Field()); + + $this->textBlock->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithEmptyField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(true); + + $this->textBlock->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTags() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->textBlock->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithMultipleArgumentsInArray() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->textBlock->getMetaTags('some_tag', array('some_value', 'some_value_2')); + } +} diff --git a/Tests/Handler/FieldType/TextLineTest.php b/Tests/Handler/FieldType/TextLineTest.php new file mode 100644 index 0000000..f223568 --- /dev/null +++ b/Tests/Handler/FieldType/TextLineTest.php @@ -0,0 +1,113 @@ +fieldHelper = $this->getMockBuilder(FieldHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('isFieldEmpty')) + ->getMock(); + + $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('getTranslatedField')) + ->getMock(); + + $this->content = $this->getMockBuilder(Content::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->textLine = new TextLine($this->fieldHelper, $this->translationHelper); + $this->textLine->setContent($this->content); + + $this->field = new Field(array('value' => new Value())); + } + + public function testInstanceOfHandlerInterface() + { + $this->assertInstanceOf(HandlerInterface::class, $this->textLine); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. + */ + public function testGettingTagsWithoutFieldIdentifier() + { + $this->textLine->getMetaTags('some_tag', array()); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. + */ + public function testGettingTagsWithNonExistentField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(null); + + $this->textLine->getMetaTags('some_tag', array('some_value')); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\TextLine field type handler does not support field with identifier ''. + */ + public function testGettingTagsWithUnsupportedField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(new Field()); + + $this->textLine->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithEmptyField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(true); + + $this->textLine->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTags() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->textLine->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithMultipleArgumentsInArray() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->textLine->getMetaTags('some_tag', array('some_value', 'some_value_2')); + } +} diff --git a/Tests/Handler/FieldType/XmlTextTest.php b/Tests/Handler/FieldType/XmlTextTest.php new file mode 100644 index 0000000..4ccdde9 --- /dev/null +++ b/Tests/Handler/FieldType/XmlTextTest.php @@ -0,0 +1,113 @@ +fieldHelper = $this->getMockBuilder(FieldHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('isFieldEmpty')) + ->getMock(); + + $this->translationHelper = $this->getMockBuilder(TranslationHelper::class) + ->disableOriginalConstructor() + ->setMethods(array('getTranslatedField')) + ->getMock(); + + $this->content = $this->getMockBuilder(Content::class) + ->disableOriginalConstructor() + ->setMethods(array()) + ->getMock(); + + $this->xmlText = new XmlText($this->fieldHelper, $this->translationHelper); + $this->xmlText->setContent($this->content); + + $this->field = new Field(array('value' => new Value())); + } + + public function testInstanceOfHandlerInterface() + { + $this->assertInstanceOf(HandlerInterface::class, $this->xmlText); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field type handlers require at least a field identifier. + */ + public function testGettingTagsWithoutFieldIdentifier() + { + $this->xmlText->getMetaTags('some_tag', array()); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Field 'some_value' does not exist in content. + */ + public function testGettingTagsWithNonExistentField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(null); + + $this->xmlText->getMetaTags('some_tag', array('some_value')); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Netgen\Bundle\OpenGraphBundle\Handler\FieldType\XmlText field type handler does not support field with identifier ''. + */ + public function testGettingTagsWithUnsupportedField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn(new Field()); + + $this->xmlText->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithEmptyField() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->fieldHelper->expects($this->once()) + ->method('isFieldEmpty') + ->willReturn(true); + + $this->xmlText->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTags() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->xmlText->getMetaTags('some_tag', array('some_value')); + } + + public function testGettingTagsWithMultipleArgumentsInArray() + { + $this->translationHelper->expects($this->once()) + ->method('getTranslatedField') + ->willReturn($this->field); + + $this->xmlText->getMetaTags('some_tag', array('some_value', 'some_value_2')); + } +} diff --git a/Tests/Handler/Literal/TextTest.php b/Tests/Handler/Literal/TextTest.php new file mode 100644 index 0000000..4b1b5c4 --- /dev/null +++ b/Tests/Handler/Literal/TextTest.php @@ -0,0 +1,44 @@ +text = new Text(); + } + + public function testInstanceOfHandlerInterface() + { + $this->assertInstanceOf(HandlerInterface::class, $this->text); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument '$params[0]' is invalid: Literal text handler requires the text to output. + */ + public function testGettingTagsWithEmptyParams() + { + $this->text->getMetaTags('some_tag', array()); + } + + public function testGettingTagsWithValidResult() + { + $result = $this->text->getMetaTags('some_tag', array('some_param')); + + $this->assertTrue(is_array($result)); + $this->assertInstanceOf(Item::class, $result[0]); + $this->assertEquals('some_tag', $result[0]->getTagName()); + $this->assertEquals('some_param', $result[0]->getTagValue()); + } +} diff --git a/Tests/Handler/RegistryTest.php b/Tests/Handler/RegistryTest.php new file mode 100644 index 0000000..8acc149 --- /dev/null +++ b/Tests/Handler/RegistryTest.php @@ -0,0 +1,44 @@ +registry = new Registry(); + } + + public function testAddingHandlers() + { + $handler = $this->getMockForAbstractClass(HandlerInterface::class); + $this->registry->addHandler('some_handler', $handler); + } + + public function testGettingHandlers() + { + $handler = $this->getMockForAbstractClass(HandlerInterface::class); + $this->registry->addHandler('some_handler', $handler); + + $returnedHandler = $this->registry->getHandler('some_handler'); + + $this->assertSame($handler, $returnedHandler); + } + + /** + * @expectedException \Netgen\Bundle\OpenGraphBundle\Exception\HandlerNotFoundException + * @expectedExceptionMessage Meta tag handler with 'some_handler' identifier not found. + */ + public function testGettingNonExistentHandler() + { + $this->registry->getHandler('some_handler'); + } +} diff --git a/Tests/MetaTag/CollectorTest.php b/Tests/MetaTag/CollectorTest.php new file mode 100644 index 0000000..0c37038 --- /dev/null +++ b/Tests/MetaTag/CollectorTest.php @@ -0,0 +1,235 @@ +registry = $this->getMockBuilder(Registry::class) + ->disableOriginalConstructor() + ->setMethods(array('getHandler')) + ->getMock(); + + $this->contentTypeService = $this->getMockBuilder(ContentTypeService::class) + ->disableOriginalConstructor() + ->setMethods(array('loadContentType')) + ->getMock(); + + $this->config = $this->getMockBuilder(ConfigResolver::class) + ->disableOriginalConstructor() + ->setMethods(array('hasParameter', 'getParameter')) + ->getMock(); + + $this->collector = new Collector($this->registry, $this->contentTypeService, $this->config); + } + + public function testInstanceOfCollectorInterface() + { + $this->assertInstanceOf(CollectorInterface::class, $this->collector); + } + + public function testCollect() + { + $this->config->expects($this->at(0)) + ->method('hasParameter') + ->willReturn(true); + + $this->config->expects($this->at(1)) + ->method('getParameter') + ->willReturn(array()); + + $this->config->expects($this->at(2)) + ->method('hasParameter') + ->willReturn(true); + + $handlers = array( + 'article' => array( + array( + 'handler' => 'literal/text', + 'tag' => 'og:type', + 'params' => array( + 'article', + ), + ), + ), + ); + + $this->config->expects($this->at(3)) + ->method('getParameter') + ->willReturn($handlers); + + $versionInfo = new VersionInfo( + array( + 'contentInfo' => new ContentInfo( + array( + 'id' => 123, + ) + ), + ) + ); + + $content = new Content( + array( + 'versionInfo' => $versionInfo, + ) + ); + + $contentType = new ContentType( + array( + 'id' => 123, + 'identifier' => 'article', + 'fieldDefinitions' => array( + new FieldDefinition( + array( + 'id' => 'id', + 'identifier' => 'name', + 'fieldTypeIdentifier' => 'eztext', + ) + ), + ), + ) + ); + + $this->contentTypeService->expects($this->once()) + ->method('loadContentType') + ->willReturn($contentType); + + $handler = $this->getMockBuilder(TextLine::class) + ->disableOriginalConstructor() + ->setMethods(array('getMetaTags')) + ->getMock(); + + $items = array( + new Item( + 'og:type', + array('article') + ), + ); + + $handler->expects($this->once()) + ->method('getMetaTags') + ->willReturn($items); + + $this->registry->expects($this->once()) + ->method('getHandler') + ->willReturn($handler); + + $this->collector->collect($content); + } + + /** + * @expectedException \LogicException + * @expectedExceptionMessage 'literal/text' handler returned wrong value. Expected 'Netgen\Bundle\OpenGraphBundle\MetaTag\Item', got 'stdClass'. + */ + public function testCollectWithLogicException() + { + $handlerArray = array( + array( + 'handler' => 'literal/text', + 'tag' => 'og:type', + 'params' => array( + 'article', + ), + ), + ); + + $handlers = array('all_content_types' => $handlerArray); + + $this->config->expects($this->at(0)) + ->method('hasParameter') + ->willReturn(true); + + $this->config->expects($this->at(1)) + ->method('getParameter') + ->willReturn($handlers); + + $this->config->expects($this->at(2)) + ->method('hasParameter') + ->willReturn(false); + + $versionInfo = new VersionInfo( + array( + 'contentInfo' => new ContentInfo( + array( + 'id' => 123, + ) + ), + ) + ); + + $content = new Content( + array( + 'versionInfo' => $versionInfo, + ) + ); + + $contentType = new ContentType( + array( + 'id' => 123, + 'identifier' => 'article', + 'fieldDefinitions' => array( + new FieldDefinition( + array( + 'id' => 'id', + 'identifier' => 'name', + 'fieldTypeIdentifier' => 'eztext', + ) + ), + ), + ) + ); + + $this->contentTypeService->expects($this->once()) + ->method('loadContentType') + ->willReturn($contentType); + + $handler = $this->getMockBuilder(TextLine::class) + ->disableOriginalConstructor() + ->setMethods(array('getMetaTags')) + ->getMock(); + + $handler->expects($this->once()) + ->method('getMetaTags') + ->willReturn(array(new \stdClass())); + + $this->registry->expects($this->once()) + ->method('getHandler') + ->willReturn($handler); + + $this->collector->collect($content); + } +} diff --git a/Tests/MetaTag/ItemTest.php b/Tests/MetaTag/ItemTest.php new file mode 100644 index 0000000..aa97ece --- /dev/null +++ b/Tests/MetaTag/ItemTest.php @@ -0,0 +1,16 @@ +assertEquals('name', $item->getTagName()); + $this->assertEquals('value', $item->getTagValue()); + } +} diff --git a/Tests/MetaTag/RendererTest.php b/Tests/MetaTag/RendererTest.php new file mode 100644 index 0000000..d14ad91 --- /dev/null +++ b/Tests/MetaTag/RendererTest.php @@ -0,0 +1,57 @@ +renderer = new Renderer(); + } + + public function testInstanceOfRendererInterface() + { + $this->assertInstanceOf(RendererInterface::class, $this->renderer); + } + + public function testRenderWithEmptyArray() + { + $result = $this->renderer->render(array()); + + $this->assertEquals('', $result); + } + + /** + * @expectedException \eZ\Publish\Core\Base\Exceptions\InvalidArgumentException + * @expectedExceptionMessage Argument 'metaTags' is invalid: Cannot render meta tag, not an instance of \Netgen\Bundle\OpenGraphBundle\MetaTag\Item + */ + public function testRenderWithInvalidItem() + { + $this->renderer->render(array('test')); + } + + public function testRender() + { + $item = new Item('name', 'value'); + $result = $this->renderer->render(array($item)); + + $this->assertEquals("\n", $result); + } + + public function testItProperlyEscapesValue() + { + $item = new Item('name', 'value'); + $result = $this->renderer->render(array($item)); + + $this->assertEquals("\n", $result); + } +} diff --git a/Tests/NetgenOpenGraphBundleTest.php b/Tests/NetgenOpenGraphBundleTest.php new file mode 100644 index 0000000..804b0f8 --- /dev/null +++ b/Tests/NetgenOpenGraphBundleTest.php @@ -0,0 +1,25 @@ +getMockBuilder(ContainerBuilder::class) + ->disableOriginalConstructor() + ->setMethods(array('addCompilerPass')) + ->getMock(); + + $container->expects($this->once()) + ->method('addCompilerPass') + ->with(new MetaTagHandlersCompilerPass()); + + $bundle = new NetgenOpenGraphBundle(); + $bundle->build($container); + } +} diff --git a/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTest.php b/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTest.php new file mode 100644 index 0000000..0c8fcdd --- /dev/null +++ b/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTest.php @@ -0,0 +1,184 @@ +collector = $this->getMockBuilder(Collector::class) + ->disableOriginalConstructor() + ->setMethods(array('collect')) + ->getMock(); + + $this->renderer = $this->getMockBuilder(Renderer::class) + ->disableOriginalConstructor() + ->setMethods(array('render')) + ->getMock(); + + $this->logger = $this->getMockBuilder(NullLogger::class) + ->disableOriginalConstructor() + ->setMethods(array('error')) + ->getMock(); + + $this->extension = new NetgenOpenGraphExtension($this->collector, $this->renderer, $this->logger); + } + + public function testInstanceOfTwigExtensionInterface() + { + $this->assertInstanceOf(\Twig_ExtensionInterface::class, $this->extension); + } + + public function testGetName() + { + $this->assertEquals('netgen_open_graph', $this->extension->getName()); + } + + public function testSetThrowExceptions() + { + $this->extension->setThrowExceptions(true); + } + + public function testGetFunctions() + { + $functions = array( + new Twig_SimpleFunction( + 'render_netgen_open_graph', + array($this->extension, 'renderOpenGraphTags'), + array('is_safe' => array('html')) + ), + new Twig_SimpleFunction( + 'get_netgen_open_graph', + array($this->extension, 'getOpenGraphTags') + ), + ); + + $result = $this->extension->getFunctions(); + + $this->assertEquals($functions, $result); + } + + public function testGetOpenGraphTags() + { + $items = array( + new Item('tag1', 'some_value'), + ); + + $this->collector->expects($this->once()) + ->method('collect') + ->willReturn($items); + + $result = $this->extension->getOpenGraphTags(new Content()); + + $this->assertEquals($items, $result); + } + + /** + * @expectedException \Exception + */ + public function testGetOpenGraphTagsWithThrowedException() + { + $this->collector->expects($this->once()) + ->method('collect') + ->willThrowException(new \Exception()); + + $this->extension->setThrowExceptions(true); + $this->extension->getOpenGraphTags(new Content()); + } + + public function testGetOpenGraphTagsWithLoggedException() + { + $this->collector->expects($this->once()) + ->method('collect') + ->willThrowException(new \Exception()); + + $this->logger->expects($this->once()) + ->method('error'); + + $this->extension->setThrowExceptions(false); + $this->extension->getOpenGraphTags(new Content()); + } + + public function testRenderOpenGraphTags() + { + $items = array( + new Item('tag1', 'some_value'), + ); + + $resultString = ''; + + $this->collector->expects($this->once()) + ->method('collect') + ->willReturn($items); + + $this->renderer->expects($this->once()) + ->method('render') + ->willReturn($resultString); + + $result = $this->extension->renderOpenGraphTags(new Content()); + + $this->assertEquals($resultString, $result); + } + + /** + * @expectedException \Exception + */ + public function testRenderOpenGraphTagsWithThrowedException() + { + $this->collector->expects($this->once()) + ->method('collect') + ->willReturn(array()); + + $this->renderer->expects($this->once()) + ->method('render') + ->willThrowException(new \Exception()); + + $this->extension->setThrowExceptions(true); + $this->extension->renderOpenGraphTags(new Content()); + } + + public function testRenderOpenGraphTagsWithLoggedException() + { + $this->collector->expects($this->once()) + ->method('collect') + ->willReturn(array()); + + $this->renderer->expects($this->once()) + ->method('render') + ->willThrowException(new \Exception()); + + $this->logger->expects($this->once()) + ->method('error'); + + $this->extension->setThrowExceptions(false); + $this->extension->renderOpenGraphTags(new Content()); + } +} diff --git a/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTwigTest.php b/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTwigTest.php new file mode 100644 index 0000000..5206e2a --- /dev/null +++ b/Tests/Templating/Twig/Extension/NetgenOpenGraphExtensionTwigTest.php @@ -0,0 +1,84 @@ +collector = $this->getMockBuilder(Collector::class) + ->disableOriginalConstructor() + ->setMethods(array('collect')) + ->getMock(); + + $this->collector->method('collect') + ->willReturn($items); + + $this->renderer = $this->getMockBuilder(Renderer::class) + ->disableOriginalConstructor() + ->setMethods(array('render')) + ->getMock(); + + $html = ''; + foreach ($items as $item) { + $html .= "getTagName()}\" content=\"{$item->getTagValue()}\" />\n"; + } + + $this->renderer->method('render') + ->willReturn($html); + + $this->logger = $this->getMockBuilder(NullLogger::class) + ->disableOriginalConstructor() + ->setMethods(array('error')) + ->getMock(); + + $this->extension = new NetgenOpenGraphExtension($this->collector, $this->renderer, $this->logger); + } + + /** + * @return string + */ + protected function getFixturesDir() + { + return __DIR__ . '/_fixtures/'; + } + + /** + * @return \Twig_ExtensionInterface[] + */ + protected function getExtensions() + { + return array($this->extension); + } +} diff --git a/Tests/Templating/Twig/Extension/_fixtures/functions/render_netgen_open_graph.test b/Tests/Templating/Twig/Extension/_fixtures/functions/render_netgen_open_graph.test new file mode 100644 index 0000000..be21082 --- /dev/null +++ b/Tests/Templating/Twig/Extension/_fixtures/functions/render_netgen_open_graph.test @@ -0,0 +1,10 @@ +--TEST-- +"render_netgen_open_graph" function +--TEMPLATE-- +{{ render_netgen_open_graph(content) }} +--DATA-- +return array( +'content' => new \eZ\Publish\Core\Repository\Values\Content\Content(array()), +) +--EXPECT-- + diff --git a/composer.json b/composer.json index a2e063f..629b103 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,9 @@ "require": { "ezsystems/ezpublish-kernel": "*" }, + "require-dev": { + "phpunit/phpunit": "*" + }, "autoload": { "psr-4": {"Netgen\\Bundle\\OpenGraphBundle\\": ""} } diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 0000000..ea11eb5 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,25 @@ + + + + Tests + + + + + . + + DependencyInjection + Resources + Tests + vendor + + + + diff --git a/travis.php.ini b/travis.php.ini new file mode 100644 index 0000000..2d2a9b3 --- /dev/null +++ b/travis.php.ini @@ -0,0 +1,3 @@ +[PHP] + +memory_limit = 2G