DefinitionsTest.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\RichObjectStrings;
  7. use OCP\RichObjectStrings\Definitions;
  8. use Test\TestCase;
  9. class DefinitionsTest extends TestCase {
  10. public function dataGetDefinition() {
  11. $definitions = new Definitions();
  12. $testsuite = [];
  13. foreach ($definitions->definitions as $type => $definition) {
  14. $testsuite[] = [$type, $definition];
  15. }
  16. return $testsuite;
  17. }
  18. public function testGetDefinitionNotExisting(): void {
  19. $this->expectException(\OCP\RichObjectStrings\InvalidObjectExeption::class);
  20. $this->expectExceptionMessage('Object type is undefined');
  21. $definitions = new Definitions();
  22. $definitions->getDefinition('NotExistingType');
  23. }
  24. /**
  25. * @dataProvider dataGetDefinition
  26. * @param string $type
  27. * @param array $expected
  28. */
  29. public function testGetDefinition($type, array $expected): void {
  30. $definitions = new Definitions();
  31. $definition = $definitions->getDefinition($type);
  32. $this->assertEquals($expected, $definition);
  33. $this->assertArrayHasKey('author', $definition);
  34. $this->assertNotEquals('', $definition['author'], 'Author of definition must not be empty');
  35. $this->assertArrayHasKey('app', $definition);
  36. $this->assertNotEquals('', $definition['app'], 'App of definition must not be empty');
  37. $this->assertArrayHasKey('since', $definition);
  38. $this->assertNotEmpty($definition['since'], 'Since of definition must not be empty');
  39. $this->assertArrayHasKey('parameters', $definition);
  40. $this->assertTrue(is_array($definition['parameters']), 'Parameters of definition must be of type array');
  41. $this->assertNotEmpty($definition['parameters'], 'Parameters of definition must not be empty');
  42. $this->assertArrayHasKey('id', $definition['parameters'], 'Parameter ID must be defined');
  43. $this->assertArrayHasKey('name', $definition['parameters'], 'Parameter name must be defined');
  44. foreach ($definition['parameters'] as $parameter => $data) {
  45. $this->validateParameter($parameter, $data);
  46. }
  47. }
  48. public function validateParameter($parameter, $data) {
  49. $this->assertTrue(is_array($data), 'Parameter ' . $parameter . ' is invalid');
  50. $this->assertArrayHasKey('since', $data);
  51. $this->assertNotEmpty($data['since'], 'Since of parameter ' . $parameter . ' must not be empty');
  52. $this->assertArrayHasKey('required', $data);
  53. $this->assertTrue(is_bool($data['required']), 'Required of parameter ' . $parameter . ' must be a boolean');
  54. if ($parameter === 'id' || $parameter === 'name') {
  55. $this->assertTrue($data['required'], 'Parameter ' . $parameter . ' must be required');
  56. }
  57. $this->assertArrayHasKey('description', $data);
  58. $this->assertNotEquals('', $data['description'], 'Description of parameter ' . $parameter . ' must not be empty');
  59. $this->assertArrayHasKey('example', $data);
  60. $this->assertNotEquals('', $data['example'], 'Example of parameter ' . $parameter . ' must not be empty');
  61. }
  62. }