File indexing completed on 2024-12-22 03:47:06

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/product.php');
0010 require_once('../src/server/shared/sample.php');
0011 
0012 class SampleTest extends PHPUnit\Framework\TestCase
0013 {
0014     private static $db;
0015 
0016     public static function setUpBeforeClass(): void
0017     {
0018         self::$db = DatastoreTestHelper::setup();
0019     }
0020 
0021     public function testListSampleInsert()
0022     {
0023         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0024         $this->assertNotNull($p);
0025         $p->name = 'org.kde.MyListProduct';
0026         $p->insert(self::$db); // HACK create a new product, so the data tables are created correctly
0027 
0028         $sample = '{
0029             "entry1": {
0030                 "element11": "aString",
0031                 "element12": true
0032             },
0033             "entry2": [
0034                 { "element21": 14, "element22": 1.5 },
0035                 { "element21": 16, "element22": 1.7 }
0036             ]
0037         }';
0038 
0039         Sample::insert(self::$db, $sample, $p);
0040         Sample::insert(self::$db, $sample, $p);
0041 
0042         Sample::echoDataAsJson(self::$db, $p);
0043         $data = json_decode($this->getActualOutput(), false);
0044         $this->assertTrue(is_array($data));
0045         $this->assertCount(2, $data);
0046         $d0 = $data[0];
0047         $this->assertObjectHasAttribute('timestamp', $d0);
0048         $this->assertObjectHasAttribute('entry1', $d0);
0049         $this->assertObjectHasAttribute('entry2', $d0);
0050         $d01 = $d0->{'entry2'};
0051         $this->assertCount(2, $d01);
0052     }
0053 
0054     public function testMapSampleInsert()
0055     {
0056         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0057         $this->assertNotNull($p);
0058         $p->name = 'org.kde.MyMapProduct';
0059         $p->schema[1]->type = SchemaEntry::MAP_TYPE;
0060         $p->insert(self::$db);
0061 
0062         $sample = '{
0063             "entry1": {
0064                 "element11": "aString",
0065                 "element12": true
0066             },
0067             "entry2": {
0068                 "key1": { "element21": 14, "element22": 1.5 },
0069                 "key2": { "element21": 16, "element22": 1.7 }
0070             }
0071         }';
0072 
0073         Sample::insert(self::$db, $sample, $p);
0074 
0075         Sample::echoDataAsJson(self::$db, $p);
0076         $data = json_decode($this->getActualOutput(), false);
0077 
0078         $this->assertTrue(is_array($data));
0079         $this->assertCount(1, $data);
0080         $d0 = $data[0];
0081         $this->assertObjectHasAttribute('timestamp', $d0);
0082         $this->assertObjectHasAttribute('entry1', $d0);
0083         $this->assertObjectHasAttribute('entry2', $d0);
0084         $d01 = $d0->{'entry2'};
0085         $this->assertObjectHasAttribute('key1', $d01);
0086         $this->assertObjectHasAttribute('key2', $d01);
0087     }
0088 
0089     public function testEmptyInsert()
0090     {
0091         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0092         $this->assertNotNull($p);
0093         $p->name = 'org.kde.MyEmptyProduct';
0094         $p->insert(self::$db);
0095 
0096         $sample = '{
0097             "someRandomStuff": "not part of the schema",
0098             "someOtherStuff": 42
0099         }';
0100 
0101         Sample::insert(self::$db, $sample, $p);
0102         Sample::echoDataAsJson(self::$db, $p);
0103         $data = json_decode($this->getActualOutput(), false);
0104         $this->assertTrue(is_array($data));
0105         $this->assertCount(1, $data);
0106         $d0 = $data[0];
0107         $this->assertObjectHasAttribute('timestamp', $d0);
0108         $this->assertObjectNotHasAttribute('entry1', $d0);
0109         $this->assertObjectNotHasAttribute('someRandomStuff', $d0);
0110     }
0111 
0112     public static function invalidInsert_data()
0113     {
0114         return [
0115             'empty' => [ '' ],
0116             'array' => [ '[]' ],
0117             'missing id' => [ '{ "timestamp": "2016-12-18 12:42:35" }' ],
0118             'missing timestamp' => [ '{ "id": 42 }' ]
0119         ];
0120     }
0121 
0122     /**
0123      * @dataProvider invalidInsert_data
0124      */
0125     public function testInvalidInsert($input)
0126     {
0127         $this->expectException(RESTException::class);
0128         $this->expectExceptionCode(400);
0129         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0130         $this->assertNotNull($p);
0131 
0132         Sample::insert(self::$db, $input, $p);
0133     }
0134 
0135     public function testImport()
0136     {
0137         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0138         $this->assertNotNull($p);
0139         $p->name = 'org.kde.MyCleanProduct';
0140         $p->insert(self::$db);
0141 
0142         $input = '[{
0143             "timestamp": "2016-12-18 12:42:35",
0144             "entry1": { "element11": "firstString" },
0145             "entry2": [ { "element21": 12 } ]
0146         }, {
0147             "timestamp": "2016-12-19 15:12:10",
0148             "entry1": { "element12": true },
0149             "entry2": [ { "element22": 1.3 } ]
0150         }]';
0151         Sample::import(self::$db, $input, $p);
0152 
0153         Sample::echoDataAsJson(self::$db, $p);
0154         $jsonOutput = json_decode($this->getActualOutput(), true);
0155         unset($jsonOutput[0]['id']);
0156         unset($jsonOutput[1]['id']);
0157         $this->assertJsonStringEqualsJsonString($input, json_encode($jsonOutput));
0158     }
0159 
0160     public static function invalidImport_data()
0161     {
0162         return [
0163             'nothing' => [ '' ],
0164             'empty' => [ '{}' ],
0165             'object' => [ '{ "id": 42, "timestamp": "2016-12-18 12:42:35" }' ],
0166             'missing timestamp' => [ '[{ "id": 42 }]' ],
0167             'id present' => [ '[{ "id": 42, "timestamp": "2016-12-18 12:42:35" }]' ]
0168         ];
0169     }
0170 
0171     /**
0172      * @dataProvider invalidImport_data
0173      */
0174     public function testInvalidImport($input)
0175     {
0176         $this->expectException(RESTException::class);
0177         $this->expectExceptionCode(400);
0178         $p = Product::productByName(self::$db, 'org.kde.UnitTest');
0179         $this->assertNotNull($p);
0180 
0181         Sample::import(self::$db, $input, $p);
0182     }
0183 }