Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ArchiveWriterTest.php « DataAccess « Unit « PHPUnit « tests - github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3814c0ffc1b8f08994929f469c90dcadf7e34699 (plain)
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
<?php
/**
 * Piwik - free/libre analytics platform
 *
 * @link https://matomo.org
 * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
 */

namespace Piwik\Tests\Unit;

use Piwik\Archive\Chunk;
use Piwik\DataAccess\ArchiveWriter;
use Piwik\DataTable;
use Piwik\Segment;

/**
 * @group ArchiveWriterTest
 * @group Archive
 * @group Core
 */
class ArchiveWriterTest extends \PHPUnit\Framework\TestCase
{
    private $recordName = 'Actions_Action_url';

    public function test_insertBlobRecord_NoBlobsGiven_ShouldInsertNothing()
    {
        $this->assertInsertBlobRecordInsertedRecordsInBulk(array(), array());
    }

    public function test_insertBlobRecord_OnlyRootTableGiven_ShouldNotMoveRootTableIntoAChunk()
    {
        $blobs    = array(0 => $this->getSerializedBlob());
        $expected = array(array($this->recordName, $this->getSerializedBlob()));

        $this->assertInsertBlobRecordInsertedRecordsInBulk($expected, $blobs);
    }

    public function test_insertBlobRecord_RootAndSubTablesGiven_OnlyAfewSubtables()
    {
        $blobs = $this->generateBlobs(0, 45);

        $expectedBlobs = array(
            array($this->recordName, $this->getSerializedBlob('_0')),
            array($this->recordName . '_chunk_0_99', serialize($this->generateBlobs(1, 44)))
        );

        $this->assertInsertBlobRecordInsertedRecordsInBulk($expectedBlobs, $blobs);
    }

    public function test_insertBlobRecord_RootAndSubTablesGiven_ShouldOnlySplitSubtablesIntoAChunk()
    {
        $blobs = $this->generateBlobs(0, 1145);

        $expectedBlobs = array(
            array($this->recordName, $this->getSerializedBlob('_0')),
            array($this->recordName . '_chunk_0_99'     , serialize($this->generateBlobs(1, Chunk::NUM_TABLES_IN_CHUNK - 1))), // does not start with zero as zero is root table
            array($this->recordName . '_chunk_100_199'  , serialize($this->generateBlobs(100, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_200_299'  , serialize($this->generateBlobs(200, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_300_399'  , serialize($this->generateBlobs(300, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_400_499'  , serialize($this->generateBlobs(400, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_500_599'  , serialize($this->generateBlobs(500, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_600_699'  , serialize($this->generateBlobs(600, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_700_799'  , serialize($this->generateBlobs(700, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_800_899'  , serialize($this->generateBlobs(800, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_900_999'  , serialize($this->generateBlobs(900, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_1000_1099', serialize($this->generateBlobs(1000, Chunk::NUM_TABLES_IN_CHUNK))),
            array($this->recordName . '_chunk_1100_1199', serialize($this->generateBlobs(1100, 45))),
        );

        $this->assertInsertBlobRecordInsertedRecordsInBulk($expectedBlobs, $blobs);
    }

    public function test_insertBlobRecord_ShouldInsertASingleRecord_IfNotAnArrayOfBlobsIsGiven()
    {
        $blob = $this->getSerializedBlob('_root');

        $this->assertInsertBlobRecordInsertedASingleRecord($blob, $blob);
    }

    private function generateBlobs($startIndex, $numberOfEntries)
    {
        $blobs = array();

        for ($i = 0; $i < $numberOfEntries; $i++) {
            $subtableId = $startIndex + $i;
            // we need to append something to make sure it actually moves the correct blob into the correct chunk
            $blobs[$subtableId] = $this->getSerializedBlob('_'. $subtableId);
        }

        return $blobs;
    }

    private function getSerializedBlob($appendix = '')
    {
        return 'a:1:{i:0;a:3:{i:0;a:0:{}i:1;a:0:{}i:3;N;}}' . $appendix;
    }

    private function assertInsertBlobRecordInsertedRecordsInBulk($expectedBlobs, $blobs)
    {
        $writer = $this->getMockBuilder('Piwik\DataAccess\ArchiveWriter')
            ->disableOriginalConstructor()
            ->onlyMethods(array('insertRecord', 'compress'))
            ->getMock();
        $writer->expects($this->exactly(count($expectedBlobs)))
               ->method('compress')
               ->will($this->returnArgument(0));

        foreach ($expectedBlobs as $index => $expectedBlob) {
            $writer->expects($this->at($index * 2 + 1))
                ->method('insertRecord')
                ->with($this->equalTo($expectedBlob[0]), $this->equalTo($expectedBlob[1]));
        }

        /** @var ArchiveWriter $writer */
        $writer->insertBlobRecord($this->recordName, $blobs);
    }

    private function assertInsertBlobRecordInsertedASingleRecord($expectedBlob, $blob)
    {
        $writer = $this->getMockBuilder('Piwik\DataAccess\ArchiveWriter')
            ->disableOriginalConstructor()
            ->onlyMethods(array('insertRecord', 'compress'))
            ->getMock();
        $writer->expects($this->once())
               ->method('compress')
               ->will($this->returnArgument(0));
        $writer->expects($this->once())
               ->method('insertRecord')
               ->with($this->recordName, $expectedBlob);

        /** @var ArchiveWriter $writer */
        $writer->insertBlobRecord($this->recordName, $blob);
    }
}