aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/RichObjectStrings/DefinitionsTest.php
blob: efe372a25e8e608999171f38108fa4cb09362460 (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
<?php
/**
 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\RichObjectStrings;

use OCP\RichObjectStrings\Definitions;
use Test\TestCase;

class DefinitionsTest extends TestCase {
	public function dataGetDefinition() {
		$definitions = new Definitions();
		$testsuite = [];
		foreach ($definitions->definitions as $type => $definition) {
			$testsuite[] = [$type, $definition];
		}
		return $testsuite;
	}

	
	public function testGetDefinitionNotExisting() {
		$this->expectException(\OCP\RichObjectStrings\InvalidObjectExeption::class);
		$this->expectExceptionMessage('Object type is undefined');

		$definitions = new Definitions();
		$definitions->getDefinition('NotExistingType');
	}

	/**
	 * @dataProvider dataGetDefinition
	 * @param string $type
	 * @param array $expected
	 */
	public function testGetDefinition($type, array $expected) {
		$definitions = new Definitions();
		$definition = $definitions->getDefinition($type);

		$this->assertEquals($expected, $definition);
		$this->assertArrayHasKey('author', $definition);
		$this->assertNotEquals('', $definition['author'], 'Author of definition must not be empty');
		$this->assertArrayHasKey('app', $definition);
		$this->assertNotEquals('', $definition['app'], 'App of definition must not be empty');
		$this->assertArrayHasKey('since', $definition);
		$this->assertNotEmpty($definition['since'], 'Since of definition must not be empty');
		$this->assertArrayHasKey('parameters', $definition);
		$this->assertTrue(is_array($definition['parameters']), 'Parameters of definition must be of type array');
		$this->assertNotEmpty($definition['parameters'], 'Parameters of definition must not be empty');


		$this->assertArrayHasKey('id', $definition['parameters'], 'Parameter ID must be defined');
		$this->assertArrayHasKey('name', $definition['parameters'], 'Parameter name must be defined');

		foreach ($definition['parameters'] as $parameter => $data) {
			$this->validateParameter($parameter, $data);
		}
	}

	public function validateParameter($parameter, $data) {
		$this->assertTrue(is_array($data), 'Parameter ' . $parameter . ' is invalid');
		$this->assertArrayHasKey('since', $data);
		$this->assertNotEmpty($data['since'], 'Since of parameter ' . $parameter . ' must not be empty');
		$this->assertArrayHasKey('required', $data);
		$this->assertTrue(is_bool($data['required']), 'Required of parameter ' . $parameter . ' must be a boolean');
		if ($parameter === 'id' || $parameter === 'name') {
			$this->assertTrue($data['required'], 'Parameter ' . $parameter . ' must be required');
		}

		$this->assertArrayHasKey('description', $data);
		$this->assertNotEquals('', $data['description'], 'Description of parameter ' . $parameter . ' must not be empty');
		$this->assertArrayHasKey('example', $data);
		$this->assertNotEquals('', $data['example'], 'Example of parameter ' . $parameter . ' must not be empty');
	}
}