Skip to content

Commit 953b5c1

Browse files
authored
Merge pull request #17 from DavidGoodwin/master
Bump phpunit; try and avoid PHP notice ...
2 parents 5183d43 + 60d8470 commit 953b5c1

File tree

13 files changed

+74
-14
lines changed

13 files changed

+74
-14
lines changed

.travis.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
language: php
22
php:
3+
- 7.2
4+
- 7.1
35
- 7.0
46
- 5.6
5-
- 5.5
6-
- 5.4
77

88
before_script: composer install
9-
script: phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml
9+
script: vendor/bin/phpunit -c phpunit.xml --coverage-clover build/logs/clover.xml
1010

1111
after_script:
1212
- vendor/bin/test-reporter

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"php": ">=5.4.0"
1717
},
1818
"require-dev": {
19-
"phpunit/phpunit": "3.7.*",
19+
"phpunit/phpunit": "^4.0",
2020
"codeclimate/php-test-reporter": "dev-master"
2121
},
2222
"autoload": {
@@ -29,4 +29,4 @@
2929
"tomverran\\Robot\\": ["src/Robot/", "tests/"]
3030
}
3131
}
32-
}
32+
}

src/Robot/AccessRules.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class AccessRules
77
private $rules;
88

99
/**
10-
* @param Boolean[] $rules A map of path => allowed
10+
* @param string[] $rules A map of path => allowed
1111
*/
1212
public function __construct(array $rules)
1313
{
@@ -45,4 +45,4 @@ public function isAllowed($url)
4545
}
4646
return empty($matches) || array_shift($matches);
4747
}
48-
}
48+
}

src/Robot/RobotsFile.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ public function __construct($content)
4444
public function current()
4545
{
4646
$cur = $this->lineIterator->current();
47+
48+
if (!is_array($cur) || !array_key_exists(1, $cur)) {
49+
return '';
50+
}
4751
return count($cur) > 1 ? $cur[1] : '';
4852
}
4953

src/Robot/RobotsTxt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class RobotsTxt
2424
*/
2525
public function __construct($contents)
2626
{
27-
$this->parseFile(new RobotsFile($contents, [self::USER_AGENT, self::ALLOW, self::DISALLOW]));
27+
$this->parseFile(new RobotsFile($contents));
2828
}
2929

3030
/**

tests/RobotTest.php

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,15 +178,20 @@ public function testCaseSensitivePaths()
178178
*/
179179
public function testHandlesEmptyUserAgentLine()
180180
{
181-
$invalid_text = <<<EOF
182-
User-agent: foo
183-
User-agent:
184-
Disallow: /
185-
EOF;
186181

187-
$g = new RobotsTxt($invalid_text);
182+
$g = self::getRobotsTxt('empty-useragent');
188183

189184
$this->assertFalse($g->isAllowed('foo', '/something'));
190185
$this->assertTrue($g->isAllowed('Googlebot', '/something'));
191186
}
187+
188+
/**
189+
* Sometimes we might get rubbish with a :: in it, which will confuse the parser...
190+
*/
191+
public function testHtmlAndCSS() {
192+
193+
$g = self::getRobotsTxt('html-with-css');
194+
195+
$this->assertTrue($g->isAllowed('foo', '/something'));
196+
}
192197
}

tests/RobotsFileTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,4 +31,35 @@ public function givenCrOnlyLineBreaks_pickUpTwoLines($newline)
3131
$test->next();
3232
$this->assertEquals(['Disallow', '/'], [$test->key(), $test->current()]);
3333
}
34+
35+
36+
/**
37+
* Get a RobotsTxt class loaded up with google's robots.txt
38+
* which is like, the most complex one I've seen.
39+
* @param string $file The filename
40+
* @return RobotsTxt
41+
*/
42+
private static function getRobotsFile($file)
43+
{
44+
$d = DIRECTORY_SEPARATOR;
45+
return new RobotsFile(file_get_contents(dirname(__FILE__) . $d . 'files' . $d . $file . '.txt'));
46+
}
47+
48+
public function testInvalidDataIn()
49+
{
50+
$robots = self::getRobotsFile('invalid-data-in');
51+
$lines = implode(',', iterator_to_array($robots));
52+
$this->assertEquals('SomeUserAgent,x,', $lines);
53+
}
54+
55+
public function testEmptyfile()
56+
{
57+
$robots = self::getRobotsFile('empty-file');
58+
$this->assertEmpty(iterator_to_array($robots));
59+
}
60+
61+
public function testTestContent() {
62+
$robots = self::getRobotsFile('test');
63+
$this->assertEquals(['Test' => ''], iterator_to_array($robots));
64+
}
3465
}

tests/files/empty-file.txt

Whitespace-only changes.

tests/files/empty-useragent.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
User-agent: foo
2+
User-agent:
3+
Disallow: /

tests/files/html-with-css.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<!doctype html>
2+
<head><title>test</title>
3+
<style>
4+
* {
5+
}
6+
*::before, <-- THIS
7+
....
8+
</style>
9+
</head>
10+
</html>

0 commit comments

Comments
 (0)