Completed
Push — master ( 8878dc...665782 )
by Matthijs
05:53 queued 03:03
created

testPersist()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 12
rs 9.4285
cc 1
eloc 7
nc 1
nop 3
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