DefinitionsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace Test\RichObjectStrings;
  22. use OCP\RichObjectStrings\Definitions;
  23. use Test\TestCase;
  24. class DefinitionsTest extends TestCase {
  25. public function dataGetDefinition() {
  26. $definitions = new Definitions();
  27. $testsuite = [];
  28. foreach ($definitions->definitions as $type => $definition) {
  29. $testsuite[] = [$type, $definition];
  30. }
  31. return $testsuite;
  32. }
  33. public function testGetDefinitionNotExisting() {
  34. $this->expectException(\OCP\RichObjectStrings\InvalidObjectExeption::class);
  35. $this->expectExceptionMessage('Object type is undefined');
  36. $definitions = new Definitions();
  37. $definitions->getDefinition('NotExistingType');
  38. }
  39. /**
  40. * @dataProvider dataGetDefinition
  41. * @param string $type
  42. * @param array $expected
  43. */
  44. public function testGetDefinition($type, array $expected) {
  45. $definitions = new Definitions();
  46. $definition = $definitions->getDefinition($type);
  47. $this->assertEquals($expected, $definition);
  48. $this->assertArrayHasKey('author', $definition);
  49. $this->assertNotEquals('', $definition['author'], 'Author of definition must not be empty');
  50. $this->assertArrayHasKey('app', $definition);
  51. $this->assertNotEquals('', $definition['app'], 'App of definition must not be empty');
  52. $this->assertArrayHasKey('since', $definition);
  53. $this->assertNotEmpty($definition['since'], 'Since of definition must not be empty');
  54. $this->assertArrayHasKey('parameters', $definition);
  55. $this->assertTrue(is_array($definition['parameters']), 'Parameters of definition must be of type array');
  56. $this->assertNotEmpty($definition['parameters'], 'Parameters of definition must not be empty');
  57. $this->assertArrayHasKey('id', $definition['parameters'], 'Parameter ID must be defined');
  58. $this->assertArrayHasKey('name', $definition['parameters'], 'Parameter name must be defined');
  59. foreach ($definition['parameters'] as $parameter => $data) {
  60. $this->validateParameter($parameter, $data);
  61. }
  62. }
  63. public function validateParameter($parameter, $data) {
  64. $this->assertTrue(is_array($data), 'Parameter ' . $parameter . ' is invalid');
  65. $this->assertArrayHasKey('since', $data);
  66. $this->assertNotEmpty($data['since'], 'Since of parameter ' . $parameter . ' must not be empty');
  67. $this->assertArrayHasKey('required', $data);
  68. $this->assertTrue(is_bool($data['required']), 'Required of parameter ' . $parameter . ' must be a boolean');
  69. if ($parameter === 'id' || $parameter === 'name') {
  70. $this->assertTrue($data['required'], 'Parameter ' . $parameter . ' must be required');
  71. }
  72. $this->assertArrayHasKey('description', $data);
  73. $this->assertNotEquals('', $data['description'], 'Description of parameter ' . $parameter . ' must not be empty');
  74. $this->assertArrayHasKey('example', $data);
  75. $this->assertNotEquals('', $data['example'], 'Example of parameter ' . $parameter . ' must not be empty');
  76. }
  77. }