Completed
Pull Request — master (#6)
by Robbie
02:33
created

RemoveOldLogEntriesTaskTest::testClassProperties()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 7
rs 9.4285
cc 1
eloc 5
nc 1
nop 0
1
<?php
2
3
namespace SilverLeague\LogViewer\Tests\Task;
4
5
use SilverLeague\LogViewer\Model\LogEntry;
6
use SilverLeague\LogViewer\Task\RemoveOldLogEntriesTask;
7
use SilverStripe\Core\Config\Config;
8
use Silverstripe\ORM\FieldType\DBDatetime;
9
use SilverStripe\Dev\SapphireTest;
10
11
/**
12
 * @coversDefaultClass \SilverLeague\LogViewer\Task\RemoveOldLogEntriesTask
13
 * @package silverstripe-logviewer
14
 * @author  Robbie Averill <[email protected]>
15
 */
16
class RemoveOldLogEntriesTaskTest extends SapphireTest
17
{
18
    /**
19
     * {@inheritDoc}
20
     */
21
    protected $usesDatabase = true;
22
23
    /**
24
     * {@inheritDoc}
25
     */
26
    protected static $fixture_file = 'RemoveOldLogEntriesTaskTest.yml';
27
28
    /**
29
     * {@inheritDoc}
30
     */
31
    public function setUp()
32
    {
33
        parent::setUp();
34
        Config::inst()->nest();
35
        DBDatetime::set_mock_now('2017-01-08 00:58:00');
36
    }
37
38
    /**
39
     * Test that the configuration properties are set correctly
40
     */
41
    public function testClassProperties()
42
    {
43
        $task = new RemoveOldLogEntriesTask;
44
        $this->assertContains('Remove LogEntry', $task->getTitle());
45
        $this->assertContains('Will run as a cron task', $task->getDescription());
46
        $this->assertSame('RemoveOldLogEntriesTask', Config::inst()->get(RemoveOldLogEntriesTask::class, 'segment'));
47
    }
48
49
    /**
50
     * Test that the max log age, cron schedule and cron enabled can be set via YAML configuration
51
     *
52
     * @dataProvider configurableSettingsProvider
53
     */
54
    public function testSettingsAreConfigurable($getter, $setting, $value)
55
    {
56
        Config::inst()->update('LogViewer', $setting, $value);
57
        $this->assertSame($value, (new RemoveOldLogEntriesTask)->{$getter}());
58
    }
59
60
    /**
61
     * @return array[]
62
     */
63
    public function configurableSettingsProvider()
64
    {
65
        return [
66
            ['getMaxAge', 'max_log_age', 9],
67
            ['getCronEnabled', 'cron_enabled', false],
68
            ['getSchedule', 'cron_schedule', '1 2 3 4 5']
69
        ];
70
    }
71
72
    /**
73
     * Test that when in the cron context and the cron task is disabled that nothing happens
74
     */
75
    public function testNothingHappensInCronContextIfCronIsDisabled()
76
    {
77
        $mock = $this
78
            ->getMockBuilder(RemoveOldLogEntriesTask::class)
79
            ->setMethods(['removeOldLogs'])
80
            ->getMock();
81
82
        $mock
83
            ->expects($this->never())
84
            ->method('removeOldLogs');
85
86
        Config::inst()->update('LogViewer', 'cron_enabled', false);
87
88
        $this->assertFalse($mock->process());
89
    }
90
91
    /**
92
     * Test that old log entries are removed from the database according to the max age setting
93
     *
94
     * @covers ::run
95
     * @covers ::process
96
     */
97
    public function testRemoveOldLogEntries()
98
    {
99
        Config::inst()->update('LogViewer', 'max_log_age', 14);
100
101
        ob_start();
102
        $result = (new RemoveOldLogEntriesTask)->process();
103
        $buffer = ob_get_clean();
104
105
        $this->assertTrue($result);
106
        $this->assertContains('Removed 3 logs', $buffer);
107
    }
108
109
    /**
110
     * {@inheritDoc}
111
     */
112
    public function tearDown()
113
    {
114
        Config::inst()->unnest();
115
        parent::tearDown();
116
    }
117
}
118