Skip to content

Commit 1084b15

Browse files
committed
Refactoring
1 parent 943a1f2 commit 1084b15

9 files changed

Lines changed: 59 additions & 127 deletions

File tree

src/XRobotsTagParser.php

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
use GuzzleHttp;
1919
use vipnytt\XRobotsTagParser\Exceptions\XRobotsTagParserException;
2020
use vipnytt\XRobotsTagParser\Rebuild;
21-
use vipnytt\XRobotsTagParser\URLParser;
2221
use vipnytt\XRobotsTagParser\UserAgentParser;
2322

2423
class XRobotsTagParser
@@ -57,23 +56,20 @@ class XRobotsTagParser
5756
*/
5857
public function __construct($url, $userAgent = self::USERAGENT_DEFAULT, array $config = [])
5958
{
60-
// Parse URL
61-
$urlParser = new URLParser(trim($url));
62-
if (!$urlParser->isValid()) {
59+
$this->url = $url;
60+
if (!filter_var($this->url, FILTER_VALIDATE_URL)) {
6361
throw new XRobotsTagParserException('Invalid URL provided');
6462
}
65-
// Encode URL
66-
$this->url = $urlParser->encode();
6763
// Set any optional configuration options
6864
$this->config = $config;
6965
if (isset($this->config['headers']) && is_array($this->config['headers'])) {
7066
$this->headers = $this->config['headers'];
7167
}
72-
// Parse rules
73-
$this->parse();
7468
// Set User-Agent
7569
$parser = new UserAgentParser($userAgent);
7670
$this->userAgent = $parser->match(array_keys($this->rules), self::USERAGENT_DEFAULT);
71+
// Parse rules
72+
$this->parse();
7773
}
7874

7975
/**
@@ -239,7 +235,7 @@ public function getDirectiveMeaning($directive)
239235
throw new XRobotsTagParserException('Unknown directive');
240236
}
241237
$class = "XRobotsTagParser\\directives\\$directive";
242-
$object = new $class($directive);
238+
$object = new $class($this->directiveClasses()[$directive]);
243239
if (!$object instanceof XRobotsTagParser\directives\directiveInterface) {
244240
throw new XRobotsTagParserException('Unsupported directive class');
245241
}

src/XRobotsTagParser/URLParser.php

Lines changed: 0 additions & 100 deletions
This file was deleted.

tests/DownloadTest.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
namespace vipnytt\XRobotsTagParser\Tests;
3+
4+
use vipnytt\XRobotsTagParser;
5+
6+
class DownloadTest extends \PHPUnit_Framework_TestCase
7+
{
8+
/**
9+
* Get rules
10+
*
11+
* @dataProvider generateDataForTest
12+
* @param string $url
13+
* @param string $userAgent
14+
*/
15+
public function testDownload($url, $userAgent)
16+
{
17+
$parser = new XRobotsTagParser($url, $userAgent);
18+
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
19+
20+
$this->assertTrue($parser->export() == []);
21+
}
22+
23+
/**
24+
* Generate test data
25+
* @return array
26+
*/
27+
public function generateDataForTest()
28+
{
29+
return [
30+
[
31+
'http://example.com/',
32+
'MyCustomBot',
33+
]
34+
];
35+
}
36+
}

tests/ExportTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class ExportTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testExport($url, $bot, $options)
16+
public function testExport($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertTrue($parser->export()['googlebot']['noindex']);

tests/GetRulesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class GetRulesTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testGetRules($url, $bot, $options)
16+
public function testGetRules($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertTrue($parser->getRules(true)['noindex']);

tests/MultiTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class MultiTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testMultipleDirectives($url, $bot, $options)
16+
public function testMultipleDirectives($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertTrue($parser->getRules(true)['all']);

tests/NoindexTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class NoindexTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testNoIndex($url, $bot, $options)
16+
public function testNoIndex($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertTrue($parser->getRules(true)['noindex']);

tests/NoneTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class NoneTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testNone($url, $bot, $options)
16+
public function testNone($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertTrue($parser->getRules(true)['none']);

tests/UnavailableAfterTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ class UnavailableAfterTest extends \PHPUnit_Framework_TestCase
1010
*
1111
* @dataProvider generateDataForTest
1212
* @param string $url
13-
* @param string $bot
13+
* @param string $userAgent
1414
* @param array $options
1515
*/
16-
public function testUnavailableAfter($url, $bot, $options)
16+
public function testUnavailableAfter($url, $userAgent, $options)
1717
{
18-
$parser = new XRobotsTagParser($url, $bot, $options);
18+
$parser = new XRobotsTagParser($url, $userAgent, $options);
1919
$this->assertInstanceOf('vipnytt\XRobotsTagParser', $parser);
2020

2121
$this->assertEquals(['unavailable_after' => 'Saturday, 01-Jul-00 07:00:00 PST'], $parser->getRules(true));

0 commit comments

Comments
 (0)