Skip to content

Commit

Permalink
Merge pull request #29 from lennerd/symfony-4
Browse files Browse the repository at this point in the history
Fix Travis setup and add tests for Symfony 4
  • Loading branch information
lennerd committed Sep 4, 2018
2 parents a526e39 + f4c0615 commit 3538a89
Show file tree
Hide file tree
Showing 15 changed files with 2,399 additions and 808 deletions.
1 change: 0 additions & 1 deletion .coveralls.yml

This file was deleted.

42 changes: 28 additions & 14 deletions .travis.yml
@@ -1,25 +1,39 @@
language: php

php:
- 5.5.9
- 5.6
- hhvm
- 7
- 7.1
- 7.2

cache:
directories:
- $HOME/.composer/cache

matrix:
allow_failures:
- php: 7
fast_finish: true
include:
- php: 5.6
env: DEPENDENCIES="symfony/lts:^2"
- php: 7.1
env: DEPENDENCIES="symfony/lts:^3"
- php: 7.2
env: DEPENDENCIES="symfony/lts:^3"

before_install:
- echo "memory_limit=4G" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini
- if [ "$DEPENDENCIES" != "" ]; then composer require --no-update $DEPENDENCIES; fi;

before_script:
- wget http://getcomposer.org/composer.phar
- php composer.phar install --dev --prefer-source --no-interaction
install:
# Install dependencies
- composer update --prefer-dist --no-interaction
# Install Coveralls
- wget -c -nc --retry-connrefused --tries=0 https://github.com/satooshi/php-coveralls/releases/download/v1.0.1/coveralls.phar
- chmod +x coveralls.phar
- php coveralls.phar --version

script:
- mkdir -p build/logs
- phpunit --coverage-clover build/logs/clover.xml

after_script: php vendor/bin/coveralls -v
- php vendor/bin/phpunit --coverage-clover build/logs/clover.xml

notifications:
email:
- hello@lennerd.com
after_success:
- travis_retry php vendor/bin/php-coveralls -v
2 changes: 2 additions & 0 deletions Command/TestUserAgentCommand.php
Expand Up @@ -14,6 +14,8 @@

/**
* Test a user agent with the Symfony CLI.
*
* @codeCoverageIgnore
*/
class TestUserAgentCommand extends ContainerAwareCommand
{
Expand Down
2 changes: 1 addition & 1 deletion README.md
@@ -1,6 +1,6 @@
# VipxBotDetectBundle

This bundle adds a detector service to your Syfmony 2 project for detecting different crawlers and spiders.
This bundle adds the [`vipx-bot-detect`](https://github.com/lennerd/vipx-bot-detect) detector service to your Syfmony project for detecting different bots like crawlers and spiders.

[![Build Status](https://secure.travis-ci.org/lennerd/VipxBotDetectBundle.png)](http://travis-ci.org/lennerd/VipxBotDetectBundle)
[![Coverage Status](https://img.shields.io/coveralls/lennerd/VipxBotDetectBundle.svg)](https://coveralls.io/r/lennerd/VipxBotDetectBundle?branch=master)
Expand Down
2 changes: 1 addition & 1 deletion Security/BotAuthenticationListener.php
Expand Up @@ -11,10 +11,10 @@

namespace Vipx\BotDetectBundle\Security;

use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Vipx\BotDetect\BotDetectorInterface;

/**
Expand Down
90 changes: 90 additions & 0 deletions Tests/BotDetectorTest.php
@@ -0,0 +1,90 @@
<?php

/*
* This file is part of the VipxBotDetectBundle package.
*
* (c) Lennart Hildebrandt <http://github.com/lennerd>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vipx\BotDetectBundle\Tests;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpFoundation\Request;
use Vipx\BotDetect\Metadata\Metadata;
use Vipx\BotDetect\Metadata\MetadataCollection;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetectBundle\BotDetector;

class BotDetectorTest extends TestCase
{
private $loader;
private $metadataCollection;

public function testDetectBotFromRequest()
{
$request = new Request(array(), array(), array(), array(), array(), array(
'HTTP_USER_AGENT' => 'Googlebot',
'REMOTE_ADDR' => '127.0.0.1',
));

$detector = $this->createDetector();

$this->assertInstanceOf(
MetadataInterface::class,
$detector->detectFromRequest($request)
);
}

private function createDetector()
{
return new BotDetector($this->getLoader(), '/my/vipx/bot/file.yml');
}

private function getLoader()
{
if (null === $this->loader) {
/** @var LoaderInterface|MockObject $loader */
$loader = $this->getMockBuilder(LoaderInterface::class)
->getMock();

$loader->expects($this->any())
->method('load')
->will($this->returnValue($this->getMetadataCollection()));

$this->loader = $loader;
}

return $this->loader;
}

private function getMetadataCollection()
{
if (null === $this->metadataCollection) {
$googleBot = new Metadata('Googlebot', 'Googlebot', '127.0.0.1');

$metadatas = array(
$googleBot,
);

$this->metadataCollection = $this->getMockBuilder(MetadataCollection::class)
->getMock();

$this->metadataCollection->expects($this->any())
->method('getIterator')
->will($this->returnCallback(function() use ($metadatas) {
return new \ArrayIterator($metadatas);
}));

$this->metadataCollection->expects($this->any())
->method('getMetadatas')
->willReturn($metadatas);
}

return $this->metadataCollection;
}
}
7 changes: 5 additions & 2 deletions Tests/Config/MetadataFileLocatorTest.php
Expand Up @@ -11,9 +11,11 @@

namespace Vipx\BotDetectBundle\Tests\Config;

use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\KernelInterface;
use Vipx\BotDetectBundle\Config\MetadataFileLocator;

class MetadataFileLocatorTest extends \PHPUnit_Framework_TestCase
class MetadataFileLocatorTest extends TestCase
{

public function testLocate()
Expand All @@ -25,7 +27,8 @@ public function testLocate()

private function getFileLocator()
{
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
/** @var KernelInterface $kernel */
$kernel = $this->getMockBuilder(KernelInterface::class)->getMock();

return new MetadataFileLocator($kernel);
}
Expand Down
Expand Up @@ -11,12 +11,13 @@

namespace Vipx\BotDetectBundle\Tests\DependencyInjection;

use Vipx\BotDetectBundle\DependencyInjection\Compiler\MetadataResolverPass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Vipx\BotDetectBundle\DependencyInjection\Compiler\MetadataResolverPass;

class MetadataResolverPassTest extends \PHPUnit_Framework_TestCase
class MetadataResolverPassTest extends TestCase
{

const SERVICE_ID = 'test_service';
Expand Down
5 changes: 3 additions & 2 deletions Tests/DependencyInjection/VipxBotDetectExtensionTest.php
Expand Up @@ -11,11 +11,12 @@

namespace Vipx\BotDetectBundle\Tests\DependencyInjection;

use Vipx\BotDetectBundle\DependencyInjection\VipxBotDetectExtension;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Vipx\BotDetectBundle\DependencyInjection\VipxBotDetectExtension;

class VipxBotDetectExtensionTest extends \PHPUnit_Framework_TestCase
class VipxBotDetectExtensionTest extends TestCase
{

const METADATA_FILE = 'test_metadata_file';
Expand Down
42 changes: 29 additions & 13 deletions Tests/Security/BotAuthenticationListenerTest.php
Expand Up @@ -11,40 +11,56 @@

namespace Vipx\BotDetectBundle\Tests\Security;

use Vipx\BotDetectBundle\Security\BotAuthenticationListener;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Vipx\BotDetect\BotDetectorInterface;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetectBundle\Security\BotAuthenticationListener;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Vipx\BotDetectBundle\Security\BotToken;

class BotAuthenticationListenerTest extends \PHPUnit_Framework_TestCase
class BotAuthenticationListenerTest extends TestCase
{

public function testSettingToken()
{
$tokenStorage = $this->getMock(
'Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface'
);
$anonymousToken = new AnonymousToken(null, 'anon.');
/** @var TokenStorageInterface|MockObject $tokenStorage */
$tokenStorage = $this->getMockBuilder(TokenStorageInterface::class)->getMock();

if (version_compare(Kernel::VERSION, '4.0', '>=')) {
$anonymousToken = new AnonymousToken('secret', 'user', array('anon.'));
} else {
$anonymousToken = new AnonymousToken(null, 'anon.');
}


$tokenStorage->expects($this->any())
->method('getToken')
->will($this->returnValue($anonymousToken));

$tokenStorage->expects($this->any())
$tokenStorage->expects($this->once())
->method('setToken')
->with($this->isInstanceOf('Vipx\BotDetectBundle\Security\BotToken'));
->with($this->isInstanceOf(BotToken::class));

$botDetector = $this->getMock('Vipx\BotDetect\BotDetectorInterface');
$metaData = $this->getMock('Vipx\BotDetect\Metadata\MetadataInterface');
/** @var BotDetectorInterface|MockObject $botDetector */
$botDetector = $this->getMockBuilder(BotDetectorInterface::class)
->disableOriginalConstructor()
->getMock();
$metaData = $this->getMockBuilder(MetadataInterface::class)->getMock();

$botDetector->expects($this->any())
->method('detectFromRequest')
$botDetector->expects($this->once())
->method('detect')
->will($this->returnValue($metaData));

$listener = new BotAuthenticationListener($tokenStorage, $botDetector);

$kernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
/** @var HttpKernelInterface $kernel */
$kernel = $this->getMockBuilder(HttpKernelInterface::class)->getMock();
$event = new GetResponseEvent($kernel, new Request(), HttpKernelInterface::MASTER_REQUEST);

$listener->onKernelRequest($event);
Expand Down
24 changes: 17 additions & 7 deletions Tests/Security/BotTokenTest.php
Expand Up @@ -11,15 +11,25 @@

namespace Vipx\BotDetectBundle\Tests\Security;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Security\Core\Authentication\Token\AnonymousToken;
use Symfony\Component\Security\Core\Role\Role;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetectBundle\Security\BotToken;

class BotTokenTest extends \PHPUnit_Framework_TestCase
class BotTokenTest extends TestCase
{

public function testBotRole()
{
$metadata = $this->getMock('Vipx\BotDetect\Metadata\MetadataInterface');
/** @var MetadataInterface $metadata */
$metadata = $this->getMockBuilder(MetadataInterface::class)->getMock();
$token = new BotToken('test', $metadata);

$this->assertEquals($metadata, $token->getMetadata());

/** @var Role[] $roles */
$roles = $token->getRoles();

$contains = false;
Expand All @@ -30,16 +40,16 @@ public function testBotRole()
}
}

if (!$contains) {
$this->fail('The BotToken has no role "ROLE_BOT"');
}
$this->assertTrue($contains, 'The BotToken has no role "ROLE_BOT"');
}

public function testCanCreateBotTokenFromAnonymousToken()
{
$metadata = $this->getMock('Vipx\BotDetect\Metadata\MetadataInterface');
/** @var MetadataInterface $metadata */
$metadata = $this->getMockBuilder(MetadataInterface::class)->getMock();

$anonymousToken = $this->getMockBuilder('Symfony\Component\Security\Core\Authentication\Token\AnonymousToken')
/** @var AnonymousToken|MockObject $anonymousToken */
$anonymousToken = $this->getMockBuilder(AnonymousToken::class)
->disableOriginalConstructor()
->getMock();

Expand Down
38 changes: 38 additions & 0 deletions Tests/VipxBotDetectBundleTest.php
@@ -0,0 +1,38 @@
<?php

/*
* This file is part of the VipxBotDetectBundle package.
*
* (c) Lennart Hildebrandt <http://github.com/lennerd>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Vipx\BotDetectBundle\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Vipx\BotDetectBundle\DependencyInjection\Compiler\MetadataResolverPass;
use Vipx\BotDetectBundle\VipxBotDetectBundle;

class VipxBotDetectBundleTest extends TestCase
{
public function testContainerHasCompilerPass()
{
$bundle = new VipxBotDetectBundle();
$container = new ContainerBuilder();

$bundle->build($container);
$passes = $container->getCompiler()->getPassConfig()->getBeforeOptimizationPasses();

$passAdded = false;
foreach ($passes as $pass) {
if ($pass instanceof MetadataResolverPass) {
$passAdded = true;
}
}

$this->assertTrue($passAdded, 'Resolver pass has not been added.');
}
}

0 comments on commit 3538a89

Please sign in to comment.