Skip to content

Commit

Permalink
Merge pull request #3 from MarioBlazek/master
Browse files Browse the repository at this point in the history
PHPUnit tests
  • Loading branch information
emodric authored Jun 30, 2016
2 parents bb61e5c + f011b80 commit 7a055e9
Show file tree
Hide file tree
Showing 21 changed files with 1,417 additions and 0 deletions.
41 changes: 41 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand Down
17 changes: 17 additions & 0 deletions Tests/Exception/FieldEmptyExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Netgen\Bundle\OpenGraphBundle\Tests\Exception;

use Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException;

class FieldEmptyExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Netgen\Bundle\OpenGraphBundle\Exception\FieldEmptyException
* @expectedExceptionMessage Field with identifier 'test' has empty value.
*/
public function testExceptionThrow()
{
throw new FieldEmptyException('test');
}
}
17 changes: 17 additions & 0 deletions Tests/Exception/HandlerNotFoundExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

namespace Netgen\Bundle\OpenGraphBundle\Tests\Exception;

use Netgen\Bundle\OpenGraphBundle\Exception\HandlerNotFoundException;

class HandlerNotFoundExceptionTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException \Netgen\Bundle\OpenGraphBundle\Exception\HandlerNotFoundException
* @expectedExceptionMessage Meta tag handler with 'test' identifier not found.
*/
public function testExceptionThrow()
{
throw new HandlerNotFoundException('test');
}
}
28 changes: 28 additions & 0 deletions Tests/Handler/FieldType/HandlerBaseTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Netgen\Bundle\OpenGraphBundle\Tests\Handler\FieldType;

use eZ\Publish\API\Repository\Values\Content\Field;

abstract class HandlerBaseTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $fieldHelper;

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

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

/**
* @var Field
*/
protected $field;
}
243 changes: 243 additions & 0 deletions Tests/Handler/FieldType/ImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php

namespace Netgen\Bundle\OpenGraphBundle\Tests\Handler\FieldType;

use eZ\Bundle\EzPublishCoreBundle\Imagine\AliasGenerator;
use eZ\Publish\API\Repository\Exceptions\InvalidVariationException;
use eZ\Publish\Core\Helper\FieldHelper;
use eZ\Publish\Core\Helper\TranslationHelper;
use eZ\Publish\Core\MVC\Exception\SourceImageNotFoundException;
use eZ\Publish\Core\Repository\Values\Content\Content;
use eZ\Publish\API\Repository\Values\Content\Field;
use eZ\Publish\Core\FieldType\Image\Value;
use eZ\Publish\Core\Repository\Values\Content\VersionInfo;
use eZ\Publish\SPI\Variation\Values\Variation;
use Netgen\Bundle\OpenGraphBundle\Handler\FieldType\Image;
use Netgen\Bundle\OpenGraphBundle\Handler\HandlerInterface;
use Psr\Log\NullLogger;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class ImageTest extends HandlerBaseTest
{
/**
* @var Image
*/
protected $image;

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

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

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

public function setUp()
{
$this->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'));
}
}
Loading

0 comments on commit 7a055e9

Please sign in to comment.