-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Nicolas MURE
committed
Nov 24, 2016
1 parent
07aa099
commit 1fb92fc
Showing
5 changed files
with
127 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<?php | ||
|
||
namespace Nmure\CrawlerDetectBundle\CrawlerDetect; | ||
|
||
use Jaybizzle\CrawlerDetect\CrawlerDetect as BaseCrawlerDetect; | ||
use Symfony\Component\HttpFoundation\RequestStack; | ||
|
||
/** | ||
* Class extending the BaseCrawlerDetect to adapt it for Symfony. | ||
*/ | ||
class CrawlerDetect extends BaseCrawlerDetect | ||
{ | ||
/** | ||
* Initialise the BaseCrawlerDetect using the master request | ||
* from the $requestStack. | ||
* | ||
* @param RequestStack $requestStack | ||
*/ | ||
public function __construct(RequestStack $requestStack) | ||
{ | ||
if ($request = $requestStack->getMasterRequest()) { | ||
// the app is accessed by a HTTP request | ||
$headers = $request->server->all(); | ||
$userAgent = $request->headers->get('User-Agent'); | ||
} else { | ||
// the app is accessed by the CLI | ||
$headers = $userAgent = null; | ||
} | ||
|
||
parent::__construct($headers, $userAgent); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace Nmure\CrawlerDetectBundle\Tests\Unit\CrawlerDetect; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Tests\TestCase; | ||
use Nmure\CrawlerDetectBundle\CrawlerDetect\CrawlerDetect; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
class CrawlerDetectTest extends TestCase | ||
{ | ||
private $browserUA = 'Mozilla/5.0 (X11; Fedora; Linux x86_64; rv:50.0) Gecko/20100101 Firefox/50.0'; | ||
private $crawlerUA = 'Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)'; | ||
|
||
public function testRequestNotFromACrawler() | ||
{ | ||
$rsMock = $this->getRequestStackMockWithMasterRequest($this->browserUA); | ||
|
||
$crawlerDetect = new CrawlerDetect($rsMock); | ||
$this->assertFalse($crawlerDetect->isCrawler()); | ||
|
||
// overriding the determined UA | ||
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA)); | ||
} | ||
|
||
public function testRequestFromACrawler() | ||
{ | ||
$rsMock = $this->getRequestStackMockWithMasterRequest($this->crawlerUA); | ||
|
||
$crawlerDetect = new CrawlerDetect($rsMock); | ||
$this->assertTrue($crawlerDetect->isCrawler()); | ||
|
||
// overriding the determined UA | ||
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA)); | ||
} | ||
|
||
public function testNoRequest() | ||
{ | ||
$rsMock = $this->getRequestStackMock(); | ||
$rsMock->expects($this->once()) | ||
->method('getMasterRequest') | ||
->willReturn(null); | ||
|
||
$crawlerDetect = new CrawlerDetect($rsMock); | ||
// when the app is accessed from the CLI | ||
$this->assertFalse($crawlerDetect->isCrawler()); | ||
|
||
// specifying the UA | ||
$this->assertFalse($crawlerDetect->isCrawler($this->browserUA)); | ||
$this->assertTrue($crawlerDetect->isCrawler($this->crawlerUA)); | ||
} | ||
|
||
private function getRequestStackMockWithMasterRequest($userAgent) | ||
{ | ||
$rsMock = $this->getRequestStackMock(); | ||
$rsMock->expects($this->once()) | ||
->method('getMasterRequest') | ||
->willReturn(new Request(array(), array(), array(), array(), array(), array( | ||
'HTTP_USER_AGENT' => $userAgent, | ||
))); | ||
|
||
return $rsMock; | ||
} | ||
|
||
private function getRequestStackMock() | ||
{ | ||
return $this->getMockBuilder('Symfony\Component\HttpFoundation\RequestStack') | ||
->disableOriginalConstructor() | ||
->getMock(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters