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
|
|
View Code Duplication |
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
|
|
|
/** |
58
|
|
|
* @covers VDB\Spider\PersistenceHandler\FileSerializedResourcePersistenceHandler |
59
|
|
|
* @dataProvider persistenceWithoutFilenameProvider |
60
|
|
|
*/ |
61
|
|
View Code Duplication |
public function testPersistResourcesWithoutFilename($resource, $expectedFilePath, $expectedFileContents) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
$this->handler->persist($resource); |
64
|
|
|
|
65
|
|
|
$this->assertFileExists($expectedFilePath); |
66
|
|
|
|
67
|
|
|
$savedResource = unserialize(file_get_contents($expectedFilePath)); |
68
|
|
|
$this->assertEquals( |
69
|
|
|
$expectedFileContents, |
70
|
|
|
$savedResource->getResponse()->getBody() |
71
|
|
|
); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function persistenceWithoutFilenameProvider() |
75
|
|
|
{ |
76
|
|
|
// This must be set here instead of in setup methods, because providers |
77
|
|
|
// get executed first |
78
|
|
View Code Duplication |
if (is_null($this->persistenceRootPath)) { |
|
|
|
|
79
|
|
|
$this->persistenceRootPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spider-UT' . DIRECTORY_SEPARATOR; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
$data = []; |
83
|
|
|
|
84
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
85
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
86
|
|
|
'http://example.org/domains/Internet/' |
87
|
|
|
); |
88
|
|
|
|
89
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
90
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
91
|
|
|
'http://example.org/domains/Internet/Abuse/' |
92
|
|
|
); |
93
|
|
|
|
94
|
|
|
return $data; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function persistenceProvider() |
98
|
|
|
{ |
99
|
|
|
// This must be set here instead of in setup methods, because providers |
100
|
|
|
// get executed first |
101
|
|
View Code Duplication |
if (is_null($this->persistenceRootPath)) { |
|
|
|
|
102
|
|
|
$this->persistenceRootPath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spider-UT' . DIRECTORY_SEPARATOR; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
$data = []; |
106
|
|
|
|
107
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
108
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
109
|
|
|
'http://example.org/domains/special/test1.html' |
110
|
|
|
); |
111
|
|
|
|
112
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
113
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
114
|
|
|
'http://example.org/domains/special/test2.html' |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
$data[] = $this->buildPersistenceProviderRecord( |
118
|
|
|
__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html', |
119
|
|
|
'http://example.org/domains/special/subdir/test3.html' |
120
|
|
|
); |
121
|
|
|
|
122
|
|
|
return $data; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
protected function buildPersistenceProviderRecord($fixturePath, $uriString) |
126
|
|
|
{ |
127
|
|
|
$resource = $this->buildResourceFromFixture( |
128
|
|
|
$fixturePath, |
129
|
|
|
$uriString |
130
|
|
|
); |
131
|
|
|
$expectedFileContents = $this->getFixtureContent(__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html'); |
132
|
|
|
$expectedFilePath = $this->buildExpectedFilePath($uriString); |
133
|
|
|
|
134
|
|
|
return [$resource, $expectedFilePath, $expectedFileContents]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
protected function buildExpectedFilePath($uriString) |
138
|
|
|
{ |
139
|
|
|
$expectedFilePath = $this->persistenceRootPath . parse_url($uriString)['host'] . parse_url($uriString)['path']; |
140
|
|
|
if (substr($expectedFilePath, -1, 1) === '/') { |
141
|
|
|
$expectedFilePath .= 'index.html'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $expectedFilePath; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.