aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/feed/CachedPageTest.php
blob: 1decfaf3b5c70fd00e31dd1200bcae62f5b892e2 (plain) (blame)
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
<?php
/**
 * PageCache tests
 */
namespace Shaarli\Feed;

/**
 * Unitary tests for cached pages
 */
class CachedPageTest extends \Shaarli\TestCase
{
    // test cache directory
    protected static $testCacheDir = 'sandbox/pagecache';
    protected static $url = 'http://shaar.li/feed/atom';
    protected static $filename;

    /**
     * Create the cache directory if needed
     */
    public static function setUpBeforeClass(): void
    {
        if (!is_dir(self::$testCacheDir)) {
            mkdir(self::$testCacheDir);
        }
        self::$filename = self::$testCacheDir . '/' . sha1(self::$url) . '.cache';
    }

    /**
     * Reset the page cache
     */
    protected function setUp(): void
    {
        if (file_exists(self::$filename)) {
            unlink(self::$filename);
        }
    }

    /**
     * Create a new cached page
     */
    public function testConstruct()
    {
        new CachedPage(self::$testCacheDir, '', true, null);
        new CachedPage(self::$testCacheDir, '', false, null);
        new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/rss', true, null);
        new CachedPage(self::$testCacheDir, 'http://shaar.li/feed/atom', false, null);
        $this->addToAssertionCount(1);
    }

    /**
     * Cache a page's content
     */
    public function testCache()
    {
        $page = new CachedPage(self::$testCacheDir, self::$url, true, null);

        $this->assertFileNotExists(self::$filename);
        $page->cache('<p>Some content</p>');
        $this->assertFileExists(self::$filename);
        $this->assertEquals(
            '<p>Some content</p>',
            file_get_contents(self::$filename)
        );
    }

    /**
     * "Cache" a page's content - the page is not to be cached
     */
    public function testShouldNotCache()
    {
        $page = new CachedPage(self::$testCacheDir, self::$url, false, null);

        $this->assertFileNotExists(self::$filename);
        $page->cache('<p>Some content</p>');
        $this->assertFileNotExists(self::$filename);
    }

    /**
     * Return a page's cached content
     */
    public function testCachedVersion()
    {
        $page = new CachedPage(self::$testCacheDir, self::$url, true, null);

        $this->assertFileNotExists(self::$filename);
        $page->cache('<p>Some content</p>');
        $this->assertFileExists(self::$filename);
        $this->assertEquals(
            '<p>Some content</p>',
            $page->cachedVersion()
        );
    }

    /**
     * Return a page's cached content - the file does not exist
     */
    public function testCachedVersionNoFile()
    {
        $page = new CachedPage(self::$testCacheDir, self::$url, true, null);

        $this->assertFileNotExists(self::$filename);
        $this->assertEquals(
            null,
            $page->cachedVersion()
        );
    }

    /**
     * Return a page's cached content - the page is not to be cached
     */
    public function testNoCachedVersion()
    {
        $page = new CachedPage(self::$testCacheDir, self::$url, false, null);

        $this->assertFileNotExists(self::$filename);
        $this->assertEquals(
            null,
            $page->cachedVersion()
        );
    }

    /**
     * Return a page's cached content within date period
     */
    public function testCachedVersionInDatePeriod()
    {
        $period = new \DatePeriod(
            new \DateTime('yesterday'),
            new \DateInterval('P1D'),
            new \DateTime('tomorrow')
        );
        $page = new CachedPage(self::$testCacheDir, self::$url, true, $period);

        $this->assertFileNotExists(self::$filename);
        $page->cache('<p>Some content</p>');
        $this->assertFileExists(self::$filename);
        $this->assertEquals(
            '<p>Some content</p>',
            $page->cachedVersion()
        );
    }

    /**
     * Return a page's cached content outside of date period
     */
    public function testCachedVersionNotInDatePeriod()
    {
        $period = new \DatePeriod(
            new \DateTime('yesterday noon'),
            new \DateInterval('P1D'),
            new \DateTime('yesterday midnight')
        );
        $page = new CachedPage(self::$testCacheDir, self::$url, true, $period);

        $this->assertFileNotExists(self::$filename);
        $page->cache('<p>Some content</p>');
        $this->assertFileExists(self::$filename);
        $this->assertNull($page->cachedVersion());
    }
}