Spaces:
Paused

File size: 1,970 Bytes
2ec30d2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<?php

/**
 * Matomo - free/libre analytics platform
 *
 * @link    https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

declare(strict_types=1);

namespace Piwik\Plugins\ArchivingMetrics\tests\Integration;

use Piwik\Common;
use Piwik\Db;
use Piwik\Period;
use Piwik\Plugins\ArchivingMetrics\Clock\Clock;
use Piwik\Plugins\ArchivingMetrics\Context;
use Piwik\Plugins\ArchivingMetrics\Timer;
use Piwik\Plugins\ArchivingMetrics\Writer\DbWriter;
use Piwik\Segment;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;

/**
 * @group ArchivingMetrics
 * @group ArchivingMetrics_TimerDb
 * @group Plugins
 */
class TimerDbTest extends IntegrationTestCase
{
    public function testItWritesAndReadsFromDatabase(): void
    {
        $period = new Period\Day(\Piwik\Date::factory('2020-11-01'));
        $segment = new Segment('', [1]);
        $context = new Context(1, $period, $segment, '');

        $timer = new Timer(true, new Clock(), new DbWriter());
        $timer->start($context);
        $timer->complete($context, [999], false);

        $rows = Db::fetchAll('SELECT * FROM ' . Common::prefixTable('archiving_metrics'));

        self::assertCount(1, $rows, 'Expected archiving_metrics table to have exactly one record.');
        self::assertSame(999, (int) $rows[0]['idarchive']);
        self::assertSame(1, (int) $rows[0]['idsite']);
        self::assertNotEmpty($rows[0]['archive_name']);
        self::assertSame('2020-11-01', $rows[0]['date1']);
        self::assertSame('2020-11-01', $rows[0]['date2']);
        self::assertIsNumeric($rows[0]['period']);
        self::assertNotEmpty($rows[0]['ts_started']);
        self::assertNotEmpty($rows[0]['ts_finished']);
        self::assertIsNumeric($rows[0]['total_time']);
        self::assertIsNumeric($rows[0]['total_time_exclusive']);
        self::assertArrayHasKey('is_temporary', $rows[0]);
        self::assertSame(0, (int) $rows[0]['is_temporary']);
    }
}