Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Made bundle work on symfony 5 #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,23 @@ language: php

matrix:
include:
- php: 7.1
- php: 7.3
env:
- SYMFONY_VERSION='4.0.*'
- SYMFONY_VERSION='5.0.*'
- php: 7.4
env:
- SYMFONY_VERSION='5.0.*'
- php: 8.0
env:
- SYMFONY_VERSION='5.0.*'

before_install:
- mkdir -p build/logs
- composer self-update
- sh -c 'if [ "$SYMFONY_VERSION" != "" ]; then composer require --dev --no-update symfony/symfony=$SYMFONY_VERSION; fi;'

install:
- composer require satooshi/php-coveralls '~1.0'
- composer require php-coveralls/php-coveralls '~1.0'

after_success:
- php vendor/bin/coveralls
40 changes: 14 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,30 +28,19 @@ class as a service (`crawler_detect`) to make it easier to use with Symfony
Download the bundle using composer :

```bash
$ composer require nmure/crawler-detect-bundle "^2.0.0"
$ composer require nmure/crawler-detect-bundle
```

For Symfony < 4.0, run :

```bash
$ composer require nmure/crawler-detect-bundle "^1.0.0"
```

then enable the bundle in your AppKernel :
then enable the bundle in your bundles.php:

```php
// app/AppKernel.php
class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
// ...
new Nmure\CrawlerDetectBundle\CrawlerDetectBundle(),
// ...
);
}
}
// config/bundles.php
return [
...
Nmure\CrawlerDetectBundle\CrawlerDetectBundle::class => ['all' => true],
...
];

```

## Usage
Expand All @@ -62,25 +51,24 @@ the Symfony's master request.
To use this service from a controller :

```php
public function indexAction()
use Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect;

public function indexAction(CrawlerDetect $crawlerDetect)
{
if ($this->get('crawler_detect')->isCrawler()) {
if ($crawlerDetect->isCrawler()) {
// this request is from a crawler :)
}

// you can also specify an user agent if you don't want
// to use the one of the master request or if the app
// is accessed by the CLI :
$ua = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)';
if ($this->get('crawler_detect')->isCrawler($ua)) {
if ($crawlerDetect->isCrawler($ua)) {
// this user agent belongs to a crawler :)
}
}
```

You can also inject this service as a dependency
using the `crawler_detect` service id.

## Testing

```bash
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

namespace Nmure\CrawlerDetectBundle\Tests;

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

Expand All @@ -21,6 +23,6 @@ public function registerBundles()
*/
public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config.yml');
$loader->load(__DIR__ . '/config/config.yml');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,13 @@

class CrawlerDetectExtensionTest extends KernelTestCase
{
private $container;

protected function setUp()
protected function setUp() : void
{
self::bootKernel();
$this->container = static::$kernel->getContainer();
}

protected function tearDown()
{
unset($this->container);
}

public function testServiceIsDefined()
{
$this->assertInstanceOf('Jaybizzle\\CrawlerDetect\\CrawlerDetect', $this->container->get('crawler_detect'));
self::assertInstanceOf('Jaybizzle\\CrawlerDetect\\CrawlerDetect', static::$kernel->getContainer()->get('crawler_detect'));
}
}
20 changes: 10 additions & 10 deletions Tests/Unit/CrawlerDetect/CrawlerDetectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Nmure\CrawlerDetectBundle\Tests\Unit\CrawlerDetect;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use PHPUnit\Framework\TestCase;
use Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect;
use Symfony\Component\HttpFoundation\Request;

Expand All @@ -16,43 +16,43 @@ public function testBrowserRequest()
$rsMock = $this->getRequestStackMockWithMasterRequest($this->browserUA);

$crawlerDetect = new CrawlerDetect($rsMock);
$this->assertFalse($crawlerDetect->isCrawler());
self::assertFalse($crawlerDetect->isCrawler());

// overriding the determined UA
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
self::assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
}

public function testCrawlerRequest()
{
$rsMock = $this->getRequestStackMockWithMasterRequest($this->crawlerUA);

$crawlerDetect = new CrawlerDetect($rsMock);
$this->assertTrue($crawlerDetect->isCrawler());
self::assertTrue($crawlerDetect->isCrawler());

// overriding the determined UA
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
self::assertFalse($crawlerDetect->isCrawler($this->browserUA));
}

public function testNoRequest()
{
$rsMock = $this->getRequestStackMock();
$rsMock->expects($this->once())
$rsMock->expects(self::once())
->method('getMasterRequest')
->willReturn(null);

$crawlerDetect = new CrawlerDetect($rsMock);
// when the app is accessed from the CLI
$this->assertFalse($crawlerDetect->isCrawler());
self::assertFalse($crawlerDetect->isCrawler());

// specifying the UA
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA));
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
self::assertFalse($crawlerDetect->isCrawler($this->browserUA));
self::assertTrue($crawlerDetect->isCrawler($this->crawlerUA));
}

private function getRequestStackMockWithMasterRequest($userAgent)
{
$rsMock = $this->getRequestStackMock();
$rsMock->expects($this->once())
$rsMock->expects(self::once())
->method('getMasterRequest')
->willReturn(new Request(array(), array(), array(), array(), array(), array(
'HTTP_USER_AGENT' => $userAgent,
Expand Down
9 changes: 0 additions & 9 deletions Tests/bootstrap.php

This file was deleted.

File renamed without changes.
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,17 @@
}
],
"require": {
"php": ">=7.1",
"symfony/framework-bundle": "~4.0",
"php": ">=7.3",
"symfony/framework-bundle": "^5.0",
"jaybizzle/crawler-detect": "1.*"
},
"autoload": {
"psr-4": { "Nmure\\CrawlerDetectBundle\\": "" }
},
"require-dev": {
"phpunit/phpunit": "^6.5",
"symfony/yaml": "^4.0"
"phpunit/phpunit": "^9.0",
"symfony/yaml": "^5.0",
"symfony/http-kernel": "^5.0",
"symfony/config": "^5.0"
}
}
4 changes: 2 additions & 2 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit bootstrap="./Tests/bootstrap.php" colors="true">
<phpunit bootstrap="vendor/autoload.php" colors="true">

<logging>
<log type="coverage-clover" target="build/logs/clover.xml" />
Expand Down Expand Up @@ -28,6 +28,6 @@
</filter>

<php>
<server name="KERNEL_CLASS" value="\AppKernel" />
<server name="KERNEL_CLASS" value="Nmure\CrawlerDetectBundle\Tests\AppKernel" />
</php>
</phpunit>