DefinitionsTest.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. /**
  34. * @expectedException \OCP\RichObjectStrings\InvalidObjectExeption
  35. * @expectedExceptionMessage Object type is undefined
  36. */
  37. public function testGetDefinitionNotExisting() {
  38. $definitions = new Definitions();
  39. $definitions->getDefinition('NotExistingType');
  40. }
  41. /**
  42. * @dataProvider dataGetDefinition
  43. * @param string $type
  44. * @param array $expected
  45. */
  46. public function testGetDefinition($type, array $expected) {
  47. $definitions = new Definitions();
  48. $definition = $definitions->getDefinition($type);
  49. $this->assertEquals($expected, $definition);
  50. $this->assertArrayHasKey('author', $definition);
  51. $this->assertNotEquals('', $definition['author'], 'Author of definition must not be empty');
  52. $this->assertArrayHasKey('app', $definition);
  53. $this->assertNotEquals('', $definition['app'], 'App of definition must not be empty');
  54. $this->assertArrayHasKey('since', $definition);
  55. $this->assertNotEmpty($definition['since'], 'Since of definition must not be empty');
  56. $this->assertArrayHasKey('parameters', $definition);
  57. $this->assertTrue(is_array($definition['parameters']), 'Parameters of definition must be of type array');
  58. $this->assertNotEmpty($definition['parameters'], 'Parameters of definition must not be empty');
  59. $this->assertArrayHasKey('id', $definition['parameters'], 'Parameter ID must be defined');
  60. $this->assertArrayHasKey('name', $definition['parameters'], 'Parameter name must be defined');
  61. foreach ($definition['parameters'] as $parameter => $data) {
  62. $this->validateParameter($parameter, $data);
  63. }
  64. }
  65. public function validateParameter($parameter, $data) {
  66. $this->assertTrue(is_array($data), 'Parameter ' . $parameter . ' is invalid');
  67. $this->assertArrayHasKey('since', $data);
  68. $this->assertNotEmpty($data['since'], 'Since of parameter ' . $parameter . ' must not be empty');
  69. $this->assertArrayHasKey('required', $data);
  70. $this->assertTrue(is_bool($data['required']), 'Required of parameter ' . $parameter . ' must be a boolean');
  71. if ($parameter === 'id' || $parameter === 'name') {
  72. $this->assertTrue($data['required'], 'Parameter ' . $parameter . ' must be required');
  73. }
  74. $this->assertArrayHasKey('description', $data);
  75. $this->assertNotEquals('', $data['description'], 'Description of parameter ' . $parameter . ' must not be empty');
  76. $this->assertArrayHasKey('example', $data);
  77. $this->assertNotEquals('', $data['example'], 'Example of parameter ' . $parameter . ' must not be empty');
  78. }
  79. }