File indexing completed on 2024-12-22 03:47:05
0001 <?php 0002 /* 0003 SPDX-FileCopyrightText: 2016 Volker Krause <vkrause@kde.org> 0004 0005 SPDX-License-Identifier: MIT 0006 */ 0007 0008 require_once('datastoretesthelper.php'); 0009 require_once('../src/server/shared/aggregation.php'); 0010 0011 class AggregationTest extends PHPUnit\Framework\TestCase 0012 { 0013 private static $db; 0014 0015 public static function setUpBeforeClass(): void 0016 { 0017 self::$db = DatastoreTestHelper::setup(); 0018 } 0019 0020 public function testFromJson() 0021 { 0022 $json = '[]'; 0023 $aggrs = Aggregation::fromJson(json_decode($json)); 0024 $this->assertCount(0, $aggrs); 0025 0026 $json = '[ 0027 { "type": "category", "name": "category elem11", "elements": [ { "type": "value", "schemaEntry": "entry1", "schemaEntryElement": "elem11" } ] }, 0028 { "type": "numeric", "name": "size: entry2", "elements": [ { "type": "size", "schemaEntry": "entry2" } ] } 0029 ]'; 0030 $aggrs = Aggregation::fromJson(json_decode($json)); 0031 $this->assertCount(2, $aggrs); 0032 0033 $a = $aggrs[0]; 0034 $this->assertInstanceOf(Aggregation::class, $a); 0035 $this->assertEquals('category', $a->type); 0036 $this->assertEquals('category elem11', $a->name); 0037 $this->assertCount(1, $a->elements); 0038 $a = $aggrs[1]; 0039 $this->assertInstanceOf(Aggregation::class, $a); 0040 $this->assertEquals('numeric', $a->type); 0041 $this->assertEquals('size: entry2', $a->name); 0042 $this->assertCount(1, $a->elements); 0043 } 0044 0045 public function testLoad() 0046 { 0047 $p = Product::productByName(self::$db, 'org.kde.UnitTest'); 0048 $this->assertNotNull($p); 0049 0050 $aggrs = Aggregation::aggregationsForProduct(self::$db, $p); 0051 $this->assertCount(2, $aggrs); 0052 0053 $a = $aggrs[0]; 0054 $this->assertInstanceOf(Aggregation::class, $a); 0055 $this->assertEquals('category', $a->type); 0056 $this->assertCount(1, $a->elements); 0057 0058 $a = $aggrs[1]; 0059 $this->assertInstanceOf(Aggregation::class, $a); 0060 $this->assertEquals('numeric', $a->type); 0061 $this->assertCount(1, $a->elements); 0062 } 0063 0064 public function testWrite() 0065 { 0066 $p = Product::productByName(self::$db, 'org.kde.UnitTest'); 0067 $this->assertNotNull($p); 0068 0069 $a = new Aggregation; 0070 $a->type = 'xy'; 0071 $a->name = 'n1'; 0072 $a->elements = json_decode('[ 0073 { "type": "value", "schemaEntry": "entry2", "schemaEntryElement": "element21" }, 0074 { "type": "value", "schemaEntry": "entry2", "schemaEntryElement": "element22" } 0075 ]'); 0076 Aggregation::update(self::$db, $p, array(0 => $a)); 0077 0078 $aggrs = Aggregation::aggregationsForProduct(self::$db, $p); 0079 $this->assertCount(1, $aggrs); 0080 0081 $a = $aggrs[0]; 0082 $this->assertInstanceOf(Aggregation::class, $a); 0083 $this->assertEquals('xy', $a->type); 0084 $this->assertEquals('n1', $a->name); 0085 $this->assertCount(2, $a->elements); 0086 } 0087 0088 public function testDelete() 0089 { 0090 $p = Product::productByName(self::$db, 'org.kde.UnitTest'); 0091 $this->assertNotNull($p); 0092 0093 $aggrs = Aggregation::aggregationsForProduct(self::$db, $p); 0094 $this->assertNotEmpty($aggrs); 0095 0096 Aggregation::delete(self::$db, $p); 0097 $aggrs = Aggregation::aggregationsForProduct(self::$db, $p); 0098 $this->assertEmpty($aggrs); 0099 } 0100 0101 public static function invalidJson_data() 0102 { 0103 return [ 0104 'nothing' => [ '' ], 0105 'object' => [ '{}' ], 0106 'array of non-objects' => [ '[1, 2, 3]' ], 0107 'missing type' => [ '[{ "elements": [] }]' ], 0108 'missing name' => [ '[{ "type": "category", "elements": [] }]' ] 0109 ]; 0110 } 0111 0112 /** 0113 * @dataProvider invalidJson_data 0114 */ 0115 public function testInvalidJson($input) 0116 { 0117 $this->expectException(RESTException::class); 0118 $this->expectExceptionCode(400); 0119 $aggrs = Aggregation::fromJson(json_decode($input)); 0120 } 0121 }