File: aggregationtest.php

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (121 lines) | stat: -rw-r--r-- 3,929 bytes parent folder | download | duplicates (3)
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
<?php
/*
    SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

require_once('datastoretesthelper.php');
require_once('../src/server/shared/aggregation.php');

class AggregationTest extends PHPUnit\Framework\TestCase
{
    private static $db;

    public static function setUpBeforeClass(): void
    {
        self::$db = DatastoreTestHelper::setup();
    }

    public function testFromJson()
    {
        $json = '[]';
        $aggrs = Aggregation::fromJson(json_decode($json));
        $this->assertCount(0, $aggrs);

        $json = '[
            { "type": "category", "name": "category elem11", "elements": [ { "type": "value", "schemaEntry": "entry1", "schemaEntryElement": "elem11" } ] },
            { "type": "numeric", "name": "size: entry2", "elements": [ { "type": "size", "schemaEntry": "entry2" } ] }
        ]';
        $aggrs = Aggregation::fromJson(json_decode($json));
        $this->assertCount(2, $aggrs);

        $a = $aggrs[0];
        $this->assertInstanceOf(Aggregation::class, $a);
        $this->assertEquals('category', $a->type);
        $this->assertEquals('category elem11', $a->name);
        $this->assertCount(1, $a->elements);
        $a = $aggrs[1];
        $this->assertInstanceOf(Aggregation::class, $a);
        $this->assertEquals('numeric', $a->type);
        $this->assertEquals('size: entry2', $a->name);
        $this->assertCount(1, $a->elements);
    }

    public function testLoad()
    {
        $p = Product::productByName(self::$db, 'org.kde.UnitTest');
        $this->assertNotNull($p);

        $aggrs = Aggregation::aggregationsForProduct(self::$db, $p);
        $this->assertCount(2, $aggrs);

        $a = $aggrs[0];
        $this->assertInstanceOf(Aggregation::class, $a);
        $this->assertEquals('category', $a->type);
        $this->assertCount(1, $a->elements);

        $a = $aggrs[1];
        $this->assertInstanceOf(Aggregation::class, $a);
        $this->assertEquals('numeric', $a->type);
        $this->assertCount(1, $a->elements);
    }

    public function testWrite()
    {
        $p = Product::productByName(self::$db, 'org.kde.UnitTest');
        $this->assertNotNull($p);

        $a = new Aggregation;
        $a->type = 'xy';
        $a->name = 'n1';
        $a->elements = json_decode('[
            { "type": "value", "schemaEntry": "entry2", "schemaEntryElement": "element21" },
            { "type": "value", "schemaEntry": "entry2", "schemaEntryElement": "element22" }
        ]');
        Aggregation::update(self::$db, $p, array(0 => $a));

        $aggrs = Aggregation::aggregationsForProduct(self::$db, $p);
        $this->assertCount(1, $aggrs);

        $a = $aggrs[0];
        $this->assertInstanceOf(Aggregation::class, $a);
        $this->assertEquals('xy', $a->type);
        $this->assertEquals('n1', $a->name);
        $this->assertCount(2, $a->elements);
    }

    public function testDelete()
    {
        $p = Product::productByName(self::$db, 'org.kde.UnitTest');
        $this->assertNotNull($p);

        $aggrs = Aggregation::aggregationsForProduct(self::$db, $p);
        $this->assertNotEmpty($aggrs);

        Aggregation::delete(self::$db, $p);
        $aggrs = Aggregation::aggregationsForProduct(self::$db, $p);
        $this->assertEmpty($aggrs);
    }

    public static function invalidJson_data()
    {
        return [
            'nothing' => [ '' ],
            'object' => [ '{}' ],
            'array of non-objects' => [ '[1, 2, 3]' ],
            'missing type' => [ '[{ "elements": [] }]' ],
            'missing name' => [ '[{ "type": "category", "elements": [] }]' ]
        ];
    }

    /**
     * @dataProvider invalidJson_data
     */
    public function testInvalidJson($input)
    {
        $this->expectException(RESTException::class);
        $this->expectExceptionCode(400);
        $aggrs = Aggregation::fromJson(json_decode($input));
    }
}