/Tests/Parser/ValidationParserTest.php

https://gitlab.com/prometech/tag-and-trace-server-doc · PHP · 196 lines · 176 code · 9 blank · 11 comment · 4 complexity · 064acbec77cdcb5acf76a3819eaf7f28 MD5 · raw file

  1. <?php
  2. /*
  3. * This file is part of the NelmioApiDocBundle.
  4. *
  5. * (c) Nelmio <hello@nelm.io>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace NelmioApiDocBundle\Tests\Parser;
  11. use Nelmio\ApiDocBundle\DataTypes;
  12. use Nelmio\ApiDocBundle\Tests\WebTestCase;
  13. use Nelmio\ApiDocBundle\Parser\ValidationParser;
  14. use Nelmio\ApiDocBundle\Parser\ValidationParserLegacy;
  15. use Symfony\Component\HttpKernel\Kernel;
  16. class ValidationParserTest extends WebTestCase
  17. {
  18. protected $handler;
  19. public function setUp()
  20. {
  21. $container = $this->getContainer();
  22. if ($container->has('validator.mapping.class_metadata_factory')) {
  23. $factory = $container->get('validator.mapping.class_metadata_factory');
  24. } else {
  25. $factory = $container->get('validator');
  26. }
  27. if (version_compare(Kernel::VERSION, '2.2.0', '<')) {
  28. $this->parser = new ValidationParserLegacy($factory);
  29. } else {
  30. $this->parser = new ValidationParser($factory);
  31. }
  32. }
  33. /**
  34. * @dataProvider dataTestParser
  35. */
  36. public function testParser($property, $expected)
  37. {
  38. $result = $this->parser->parse(array('class' => 'Nelmio\ApiDocBundle\Tests\Fixtures\Model\ValidatorTest'));
  39. foreach ($expected as $name => $value) {
  40. $this->assertArrayHasKey($property, $result);
  41. $this->assertArrayHasKey($name, $result[$property]);
  42. $this->assertEquals($result[$property][$name], $expected[$name]);
  43. }
  44. }
  45. public function dataTestParser()
  46. {
  47. return array(
  48. array(
  49. 'property' => 'length10',
  50. 'expected' => array(
  51. 'format' => '{length: min: 10}',
  52. 'default' => 'validate this',
  53. )
  54. ),
  55. array(
  56. 'property' => 'length1to10',
  57. 'expected' => array(
  58. 'format' => '{length: min: 1, max: 10}',
  59. 'default' => null,
  60. )
  61. ),
  62. array(
  63. 'property' => 'notblank',
  64. 'expected' => array(
  65. 'required' => true,
  66. 'default' => null,
  67. )
  68. ),
  69. array(
  70. 'property' => 'notnull',
  71. 'expected' => array(
  72. 'required' => true,
  73. 'default' => null,
  74. )
  75. ),
  76. array(
  77. 'property' => 'type',
  78. 'expected' => array(
  79. 'dataType' => 'DateTime',
  80. 'actualType' => DataTypes::DATETIME,
  81. 'default' => null,
  82. )
  83. ),
  84. array(
  85. 'property' => 'date',
  86. 'expected' => array(
  87. 'format' => '{Date YYYY-MM-DD}',
  88. 'actualType' => DataTypes::DATE,
  89. 'default' => null,
  90. )
  91. ),
  92. array(
  93. 'property' => 'dateTime',
  94. 'expected' => array(
  95. 'format' => '{DateTime YYYY-MM-DD HH:MM:SS}',
  96. 'actualType' => DataTypes::DATETIME,
  97. 'default' => null,
  98. )
  99. ),
  100. array(
  101. 'property' => 'time',
  102. 'expected' => array(
  103. 'format' => '{Time HH:MM:SS}',
  104. 'actualType' => DataTypes::TIME,
  105. 'default' => null,
  106. )
  107. ),
  108. array(
  109. 'property' => 'email',
  110. 'expected' => array(
  111. 'format' => '{email address}',
  112. 'default' => null,
  113. )
  114. ),
  115. array(
  116. 'property' => 'url',
  117. 'expected' => array(
  118. 'format' => '{url}',
  119. 'default' => 'https://github.com',
  120. )
  121. ),
  122. array(
  123. 'property' => 'ip',
  124. 'expected' => array(
  125. 'format' => '{ip address}',
  126. 'default' => null,
  127. )
  128. ),
  129. array(
  130. 'property' => 'singlechoice',
  131. 'expected' => array(
  132. 'format' => '[a|b]',
  133. 'actualType' => DataTypes::ENUM,
  134. 'default' => null,
  135. )
  136. ),
  137. array(
  138. 'property' => 'multiplechoice',
  139. 'expected' => array(
  140. 'format' => '{choice of [x|y|z]}',
  141. 'actualType' => DataTypes::COLLECTION,
  142. 'subType' => DataTypes::ENUM,
  143. 'default' => null,
  144. )
  145. ),
  146. array(
  147. 'property' => 'multiplerangechoice',
  148. 'expected' => array(
  149. 'format' => '{min: 2 max: 3 choice of [foo|bar|baz|qux]}',
  150. 'actualType' => DataTypes::COLLECTION,
  151. 'subType' => DataTypes::ENUM,
  152. 'default' => null,
  153. )
  154. ),
  155. array(
  156. 'property' => 'regexmatch',
  157. 'expected' => array(
  158. 'format' => '{match: /^\d{1,4}\w{1,4}$/}',
  159. 'default' => null,
  160. )
  161. ),
  162. array(
  163. 'property' => 'regexnomatch',
  164. 'expected' => array(
  165. 'format' => '{not match: /\d/}',
  166. 'default' => null,
  167. )
  168. ),
  169. array(
  170. 'property' => 'multipleassertions',
  171. 'expected' => array(
  172. 'required' => true,
  173. 'dataType' => 'string',
  174. 'format' => '{email address}',
  175. 'default' => null,
  176. )
  177. ),
  178. array(
  179. 'property' => 'multipleformats',
  180. 'expected' => array(
  181. 'format' => '{url}, {length: min: 10}',
  182. 'default' => null,
  183. )
  184. )
  185. );
  186. }
  187. }