forked from spatie/crawler
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TestCase.php
76 lines (56 loc) · 2.08 KB
/
TestCase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
namespace Spatie\Crawler\Test;
use Throwable;
use PHPUnit\Framework\TestCase as BaseTestCase;
class TestCase extends BaseTestCase
{
/** @var string logPath */
protected static $logPath;
public function skipIfTestServerIsNotRunning()
{
try {
file_get_contents('http://localhost:8080');
} catch (Throwable $e) {
$this->markTestSkipped('The testserver is not running.');
}
}
protected function getLogContents(): string
{
return file_get_contents(static::$logPath);
}
protected function assertCrawledOnce($urls)
{
$logContent = $this->getLogContents();
foreach ($urls as $url) {
$logMessage = "hasBeenCrawled: {$url['url']}";
if (isset($url['foundOn'])) {
$logMessage .= " - found on {$url['foundOn']}";
}
$logMessage .= PHP_EOL;
$this->assertEquals(1, substr_count($logContent, $logMessage), "Did not find {$logMessage} exactly one time in the log but ".substr_count($logContent, $logMessage)." times. Contents of log\n{$logContent}");
}
}
protected function assertNotCrawled($urls)
{
$logContent = $this->getLogContents();
foreach ($urls as $url) {
$logMessage = "hasBeenCrawled: {$url['url']}";
if (isset($url['foundOn'])) {
$logMessage .= " - found on {$url['foundOn']}";
}
$logMessage .= PHP_EOL;
$this->assertEquals(0, substr_count($logContent, $logMessage), "Did find {$logMessage} in the log");
}
}
protected function assertCrawledUrlCount(int $count)
{
$logContent = file_get_contents(static::$logPath);
$actualCount = substr_count($logContent, 'hasBeenCrawled');
$this->assertEquals($count, $actualCount, "Crawled `{$actualCount}` urls instead of the expected {$count}");
}
public function resetLog()
{
static::$logPath = __DIR__.'/temp/crawledUrls.txt';
file_put_contents(static::$logPath, 'start log'.PHP_EOL);
}
}