Compare commits
10 Commits
Author | SHA1 | Date | |
---|---|---|---|
|
de0eb1dceb | ||
|
36e74f63b6 | ||
|
374ddd1560 | ||
|
7d3fb69463 | ||
|
abb4354362 | ||
|
e03444314b | ||
|
ffccf63792 | ||
|
61899f1013 | ||
|
d515b270f6 | ||
|
208bedc7ff |
@ -9,7 +9,8 @@
|
|||||||
],
|
],
|
||||||
"require": {
|
"require": {
|
||||||
"phpunit/phpunit-selenium": "^4.1",
|
"phpunit/phpunit-selenium": "^4.1",
|
||||||
"facebook/webdriver": "^1.7"
|
"php-webdriver/webdriver": "^1.7",
|
||||||
|
"spatie/crawler": "^4.6"
|
||||||
},
|
},
|
||||||
"autoload": {
|
"autoload": {
|
||||||
"psr-4": {
|
"psr-4": {
|
||||||
|
@ -9,17 +9,29 @@ namespace JHodges\PHPUnitBase;
|
|||||||
abstract class BrowserTest extends \PHPUnit_Extensions_Selenium2TestCase{
|
abstract class BrowserTest extends \PHPUnit_Extensions_Selenium2TestCase{
|
||||||
|
|
||||||
public function setUp(){
|
public function setUp(){
|
||||||
$this->setHost('localhost');
|
if(! $host=getenv('SeleniumHost')){
|
||||||
$this->setPort(4444);
|
$host='localhost';
|
||||||
$args=explode(',',getenv('SeleniumBrowserArgs'));
|
}
|
||||||
$args[]='screenshot';
|
if(! $port=(int)getenv('SeleniumPort')){
|
||||||
$this->setDesiredCapabilities(['moz:firefoxOptions'=>['args'=>$args]]);
|
$port=4444;
|
||||||
$this->setBrowserUrl(getenv('SeleniumBrowserUrl'));
|
}
|
||||||
|
if($dcj=getenv('SeleniumDesiredCapabilities')){
|
||||||
|
$dc=json_decode($dcj,true);
|
||||||
|
if($dc===null){
|
||||||
|
throw new \Exception("Invalid JSON: $dcj");
|
||||||
|
}
|
||||||
|
$this->setDesiredCapabilities($dc);
|
||||||
|
}
|
||||||
|
$this->setHost($host);
|
||||||
|
$this->setPort($port);
|
||||||
|
if($url=getenv('SeleniumBrowserUrl')){
|
||||||
|
$this->setBrowserUrl($url);
|
||||||
|
}
|
||||||
$this->setBrowser(getenv('SeleniumBrowser'));
|
$this->setBrowser(getenv('SeleniumBrowser'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function tearDown(){
|
public function tearDown(){
|
||||||
if ($this->hasFailed()) {
|
if ($this->getStatus()!==0) {
|
||||||
if($path=getenv('SeleniumScreenshotPath')){
|
if($path=getenv('SeleniumScreenshotPath')){
|
||||||
$filedata = $this->currentScreenshot();
|
$filedata = $this->currentScreenshot();
|
||||||
file_put_contents($path.get_class($this).'::'.$this->getName().'.png', $filedata);
|
file_put_contents($path.get_class($this).'::'.$this->getName().'.png', $filedata);
|
||||||
|
101
src/CrawlObserver.php
Normal file
101
src/CrawlObserver.php
Normal file
@ -0,0 +1,101 @@
|
|||||||
|
<?php
|
||||||
|
namespace JHodges\PHPUnitBase;
|
||||||
|
|
||||||
|
use GuzzleHttp\Exception\RequestException;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\UriInterface;
|
||||||
|
|
||||||
|
use Spatie\Crawler\CrawlObserver as BaseCrawlObserver;
|
||||||
|
|
||||||
|
class CrawlObserver extends BaseCrawlObserver
|
||||||
|
{
|
||||||
|
|
||||||
|
public $results=[];
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the crawler will crawl the url.
|
||||||
|
*
|
||||||
|
* @param \Psr\Http\Message\UriInterface $url
|
||||||
|
*/
|
||||||
|
public function willCrawl(UriInterface $url)
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the crawler has crawled the given url successfully.
|
||||||
|
*
|
||||||
|
* @param \Psr\Http\Message\UriInterface $url
|
||||||
|
* @param \Psr\Http\Message\ResponseInterface $response
|
||||||
|
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
|
||||||
|
*/
|
||||||
|
public function crawled(
|
||||||
|
UriInterface $url,
|
||||||
|
ResponseInterface $response,
|
||||||
|
?UriInterface $foundOnUrl = null
|
||||||
|
){
|
||||||
|
$code=$response->getStatusCode();
|
||||||
|
$type=$response->getHeader('Content-Type')[0]??null;
|
||||||
|
|
||||||
|
// Retrieve both Redirect History headers
|
||||||
|
$fullRedirectReport = [];
|
||||||
|
if($response->getHeader('X-Guzzle-Redirect-History')){
|
||||||
|
$redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History'); // retrieve Redirect URI history
|
||||||
|
$redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History'); // retrieve Redirect HTTP Status history
|
||||||
|
$fullRedirectReport=[$redirectUriHistory,$redirectCodeHistory];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->results[]=[
|
||||||
|
'link'=>(String)$url,
|
||||||
|
'code'=>$code,
|
||||||
|
'type'=>$type,
|
||||||
|
'parent'=>(string)$foundOnUrl,
|
||||||
|
'redirects'=>$fullRedirectReport,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the crawler had a problem crawling the given url.
|
||||||
|
*
|
||||||
|
* @param \Psr\Http\Message\UriInterface $url
|
||||||
|
* @param \GuzzleHttp\Exception\RequestException $requestException
|
||||||
|
* @param \Psr\Http\Message\UriInterface|null $foundOnUrl
|
||||||
|
*/
|
||||||
|
public function crawlFailed(
|
||||||
|
UriInterface $url,
|
||||||
|
RequestException $requestException,
|
||||||
|
?UriInterface $foundOnUrl = null
|
||||||
|
){
|
||||||
|
if($response=$requestException->getResponse()){
|
||||||
|
$code=$response->getStatusCode();
|
||||||
|
$type=$response->getHeader('Content-Type')[0]??null;
|
||||||
|
}else{
|
||||||
|
$code='???';
|
||||||
|
$type='';
|
||||||
|
}
|
||||||
|
|
||||||
|
// Retrieve both Redirect History headers
|
||||||
|
$fullRedirectReport = [];
|
||||||
|
if($response && $response->getHeader('X-Guzzle-Redirect-History')){
|
||||||
|
$redirectUriHistory = $response->getHeader('X-Guzzle-Redirect-History'); // retrieve Redirect URI history
|
||||||
|
$redirectCodeHistory = $response->getHeader('X-Guzzle-Redirect-Status-History'); // retrieve Redirect HTTP Status history
|
||||||
|
$fullRedirectReport=[$redirectUriHistory,$redirectCodeHistory];
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->results[]=[
|
||||||
|
'link'=>(String)$url,
|
||||||
|
'code'=>$code,
|
||||||
|
'type'=>$type,
|
||||||
|
'parent'=>(string)$foundOnUrl,
|
||||||
|
'redirects'=>$fullRedirectReport,
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Called when the crawl has ended.
|
||||||
|
*/
|
||||||
|
public function finishedCrawling() {
|
||||||
|
//print_r($this->results);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
55
src/CrawlTest.php
Normal file
55
src/CrawlTest.php
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
<?php
|
||||||
|
namespace JHodges\PHPUnitBase;
|
||||||
|
|
||||||
|
use \PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
use GuzzleHttp\RequestOptions;
|
||||||
|
use GuzzleHttp\Psr7\Uri;
|
||||||
|
use Psr\Http\Message\ResponseInterface;
|
||||||
|
use Psr\Http\Message\UriInterface;
|
||||||
|
|
||||||
|
use Spatie\Crawler\Crawler;
|
||||||
|
use Spatie\Crawler\CrawlUrl;
|
||||||
|
use Spatie\Crawler\CrawlInternalUrls;
|
||||||
|
|
||||||
|
abstract class CrawlTest extends TestCase{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return string the source domain
|
||||||
|
**/
|
||||||
|
abstract protected function getUrl();
|
||||||
|
|
||||||
|
public function testCrawl(){
|
||||||
|
$observer=new CrawlObserver();
|
||||||
|
|
||||||
|
Crawler::create([
|
||||||
|
RequestOptions::ALLOW_REDIRECTS => [
|
||||||
|
'track_redirects' => true,
|
||||||
|
],
|
||||||
|
])
|
||||||
|
->setCrawlObserver($observer)
|
||||||
|
->setCrawlProfile(new CrawlInternalUrls( $this-> getUrl() ))
|
||||||
|
//->addToCrawlQueue( CrawlUrl::create(new Uri('https://another_entry_point??')) )
|
||||||
|
->startCrawling( $this-> getUrl() )
|
||||||
|
;
|
||||||
|
$this->assertTrue(true);
|
||||||
|
return $observer->results;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @depends testCrawl
|
||||||
|
*/
|
||||||
|
public function testBrokenLinks($results){
|
||||||
|
$errors='';
|
||||||
|
foreach($results as $result){
|
||||||
|
if($result['code']!=200){
|
||||||
|
$errors.="{$result['code']} {$result['link']} (found on {$result['parent']})\n";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if($errors){
|
||||||
|
throw new \Exception("\n".$errors);
|
||||||
|
}else{
|
||||||
|
$this->assertTrue(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user