RuleDescriptionParserTest.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests;
  3. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  4. use Mpociot\ApiDoc\Parsers\RuleDescriptionParser;
  5. use Orchestra\Testbench\TestCase;
  6. class RuleDescriptionParserTest extends TestCase
  7. {
  8. public function testReturnsAnEmptyDescriptionIfARuleIsNotParsed()
  9. {
  10. $rule = new RuleDescriptionParser();
  11. $this->assertEmpty($rule->getDescription());
  12. }
  13. public function testProvidesANamedContructor()
  14. {
  15. $this->assertInstanceOf(RuleDescriptionParser::class, RuleDescriptionParser::parse());
  16. }
  17. public function testReturnsADescriptionInTheMainLanguageOfTheApplication()
  18. {
  19. $expected = 'Only alphabetic characters allowed';
  20. $rule = new RuleDescriptionParser('alpha');
  21. $this->assertEquals($expected, $rule->getDescription());
  22. }
  23. public function testReturnsAnEmptyDescriptionIfNotAvailable()
  24. {
  25. $rule = new RuleDescriptionParser('dummy_rule');
  26. $description = $rule->getDescription();
  27. $this->assertEmpty($description);
  28. }
  29. public function testAllowsToPassParametersToTheDescription()
  30. {
  31. $expected = 'Must have an exact length of `2`';
  32. $rule = new RuleDescriptionParser('digits');
  33. $actual = $rule->with(2)->getDescription();
  34. $this->assertEquals($expected, $actual);
  35. }
  36. public function testOnlyPassesParametersIfTheDescriptionAllows()
  37. {
  38. $expected = 'Only alphabetic characters allowed';
  39. $rule = new RuleDescriptionParser('alpha');
  40. $actual = $rule->with("dummy parameter")->getDescription();
  41. $this->assertEquals($expected, $actual);
  42. }
  43. public function testAllowsToPassMultipleParametersToTheDescription()
  44. {
  45. $expected = 'Required if `2 + 2` is `4`';
  46. $rule = new RuleDescriptionParser('required_if');
  47. $actual = $rule->with(['2 + 2', 4])->getDescription();
  48. $this->assertEquals($expected, $actual);
  49. }
  50. /**
  51. * @param \Illuminate\Foundation\Application $app
  52. *
  53. * @return array
  54. */
  55. protected function getPackageProviders($app)
  56. {
  57. return [ApiDocGeneratorServiceProvider::class];
  58. }
  59. /**
  60. * Define environment setup.
  61. *
  62. * @param \Illuminate\Foundation\Application $app
  63. * @return void
  64. */
  65. protected function getEnvironmentSetUp($app)
  66. {
  67. $app['config']->set('app.locale', 'en');
  68. }
  69. }