1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Spider package. |
5
|
|
|
* |
6
|
|
|
* (c) Matthijs van den Bos <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace VDB\Spider\Tests\Downloader; |
13
|
|
|
|
14
|
|
|
use VDB\Spider\Tests\TestCase; |
15
|
|
|
use VDB\Spider\PersistenceHandler\FileSerializedResourcePersistenceHandler; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* |
19
|
|
|
*/ |
20
|
|
|
class FileSerializedResourcePersistenceHandlerTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var FileSerializedResourcePersistenceHandler |
24
|
|
|
*/ |
25
|
|
|
protected $handler; |
26
|
|
|
|
27
|
|
|
protected $persistenceRootPath; |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
public function setUp() |
31
|
|
|
{ |
32
|
|
|
$this->persistenceRootPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spider-UT' . DIRECTORY_SEPARATOR; |
33
|
|
|
exec('rm -rf ' . $this->persistenceRootPath); |
34
|
|
|
|
35
|
|
|
$this->handler = new FileSerializedResourcePersistenceHandler(sys_get_temp_dir()); |
36
|
|
|
$this->handler->setSpiderId('spider-UT'); |
37
|
|
|
|
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @covers VDB\Spider\PersistenceHandler\FileSerializedResourcePersistenceHandler |
42
|
|
|
* @dataProvider persistenceProvider |
43
|
|
|
*/ |
44
|
|
|
public function testPersist($resource, $expectedFilePath, $expectedFileContents) |
45
|
|
|
{ |
46
|
|
|
$this->handler->persist($resource); |
47
|
|
|
|
48
|
|
|
$this->assertFileExists($expectedFilePath); |
49
|
|
|
|
50
|
|
|
$savedResource = unserialize(file_get_contents($expectedFilePath)); |
51
|
|
|
$this->assertEquals( |
52
|
|
|
$expectedFileContents, |
53
|
|
|
$savedResource->getResponse()->getBody() |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function persistenceProvider() |
58
|
|
|
{ |
59
|
|
|
// This must be set here instead of in setup methods, because this gets |
60
|
|
|
// executed first |
61
|
|
|
if (is_null($this->persistenceRootPath)) { |
62
|
|
|
$this->persistenceRootPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spider-UT' . DIRECTORY_SEPARATOR; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$data = []; |
66
|
|
|
|
67
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
68
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
69
|
|
|
'http://example.org/domains/special/test1.html' |
70
|
|
|
); |
71
|
|
|
|
72
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
73
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
74
|
|
|
'http://example.org/domains/special/test2.html' |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
78
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
79
|
|
|
'http://example.org/domains/special/subdir/test3.html' |
80
|
|
|
); |
81
|
|
|
|
82
|
|
|
return $data; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function buildPersistenceProviderRecord($fixturePath, $uriString) |
86
|
|
|
{ |
87
|
|
|
$resource = $this->buildResourceFromFixture( |
88
|
|
|
$fixturePath, |
89
|
|
|
$uriString |
90
|
|
|
); |
91
|
|
|
$expectedFileContents = $this->getFixtureContent(__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html'); |
92
|
|
|
$expectedFilePath = $this->persistenceRootPath . parse_url($uriString)['host'] . parse_url($uriString)['path']; |
93
|
|
|
|
94
|
|
|
return [$resource, $expectedFilePath, $expectedFileContents]; |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
|