|
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 GuzzleHttp\Psr7\Response; |
|
15
|
|
|
use VDB\Spider\Tests\TestCase; |
|
16
|
|
|
use VDB\Spider\Uri\DiscoveredUri; |
|
17
|
|
|
use VDB\Spider\Resource; |
|
18
|
|
|
use VDB\Spider\PersistenceHandler\FileSerializedResourcePersistenceHandler; |
|
19
|
|
|
use VDB\Uri\Uri; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* |
|
23
|
|
|
*/ |
|
24
|
|
|
class FileSerializedResourcePersistenceHandlerTest extends TestCase |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Resource |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $resource; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var string |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $html; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @var FileSerializedResourcePersistenceHandler |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
|
|
protected $handler; |
|
41
|
|
|
|
|
42
|
|
|
public function setUp() |
|
43
|
|
|
{ |
|
44
|
|
|
$this->html = file_get_contents(__DIR__ . '/../Fixtures/DownloaderTestHTMLResource.html'); |
|
45
|
|
|
$this->resource = new Resource( |
|
46
|
|
|
new DiscoveredUri(new Uri('/domains/special/test.html', 'http://example.org')), |
|
47
|
|
|
new Response(200, [], $this->html) |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
$this->handler = new FileSerializedResourcePersistenceHandler(sys_get_temp_dir()); |
|
51
|
|
|
$this->handler->setSpiderId('spider-UT'); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @covers VDB\Spider\PersistenceHandler\FileSerializedResourcePersistenceHandler |
|
56
|
|
|
*/ |
|
57
|
|
|
public function testPersist() |
|
58
|
|
|
{ |
|
59
|
|
|
$this->handler->persist($this->resource); |
|
60
|
|
|
|
|
61
|
|
|
$expectedFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'spider-UT' . DIRECTORY_SEPARATOR .'example.org/domains/special/test.html'; |
|
62
|
|
|
$this->assertFileExists($expectedFilePath); |
|
63
|
|
|
|
|
64
|
|
|
$savedResource = unserialize(file_get_contents($expectedFilePath)); |
|
65
|
|
|
$this->assertEquals( |
|
66
|
|
|
$this->html, |
|
67
|
|
|
$savedResource->getResponse()->getBody() |
|
68
|
|
|
); |
|
69
|
|
|
} |
|
70
|
|
|
} |
|
71
|
|
|
|