aboutsummaryrefslogtreecommitdiffhomepage
path: root/tests/utils/ReferenceHistory.php
blob: 516c9f51ea22d195bf71f70bcc9b786c083beade (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
<?php

use Shaarli\FileUtils;
use Shaarli\History;

/**
 * Populates a reference history
 */
class ReferenceHistory
{
    private $count;

    private $history = [];

    /**
     * Populates the test DB with reference data
     */
    public function __construct()
    {
        $this->addEntry(
            History::DELETED,
            DateTime::createFromFormat('Ymd_His', '20170303_121216'),
            124
        );

        $this->addEntry(
            History::SETTINGS,
            DateTime::createFromFormat('Ymd_His', '20170302_121215')
        );

        $this->addEntry(
            History::UPDATED,
            DateTime::createFromFormat('Ymd_His', '20170301_121214'),
            123
        );

        $this->addEntry(
            History::CREATED,
            DateTime::createFromFormat('Ymd_His', '20170201_121214'),
            124
        );

        $this->addEntry(
            History::CREATED,
            DateTime::createFromFormat('Ymd_His', '20170101_121212'),
            123
        );
    }

    /**
     * Adds a new history entry
     *
     * @param string   $event    Event identifier
     * @param DateTime $datetime creation date
     * @param int      $id       optional: related link ID
     */
    protected function addEntry($event, $datetime, $id = null)
    {
        $link = [
            'event' => $event,
            'datetime' => $datetime,
            'id' => $id,
        ];
        $this->history[] = $link;
        $this->count++;
    }

    /**
     * Writes data to the datastore
     *
     * @param string $filename write history content to.
     */
    public function write($filename)
    {
        FileUtils::writeFlatDB($filename, $this->history);
    }

    /**
     * Returns the number of bookmarks in the reference data
     */
    public function count()
    {
        return $this->count;
    }
}