RuleDescriptionParserTest.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Mpociot\ApiDoc\Tests;
  3. use Illuminate\Translation\LoaderInterface;
  4. use Illuminate\Translation\Translator;
  5. use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
  6. use Mpociot\ApiDoc\Parsers\RuleDescriptionParser;
  7. use Orchestra\Testbench\TestCase;
  8. use Mockery as m;
  9. class RuleDescriptionParserTest extends TestCase
  10. {
  11. protected $translatorMock;
  12. public function setUp()
  13. {
  14. parent::setUp();
  15. $fileLoaderMock = m::mock(LoaderInterface::class);
  16. $this->translatorMock = m::mock(Translator::class, [$fileLoaderMock, 'es']);
  17. $this->app->instance('translator', $this->translatorMock);
  18. }
  19. public function tearDown()
  20. {
  21. m::close();
  22. }
  23. public function testReturnsAnEmptyDescriptionIfARuleIsNotParsed()
  24. {
  25. $this->translatorMock->shouldReceive('hasForLocale')->twice()->andReturn(false);
  26. $description = new RuleDescriptionParser();
  27. $this->assertEmpty($description->getDescription());
  28. }
  29. public function testProvidesANamedContructor()
  30. {
  31. $this->assertInstanceOf(RuleDescriptionParser::class, RuleDescriptionParser::parse());
  32. }
  33. public function testReturnsADescriptionInMainLanguageIfAvailable()
  34. {
  35. $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(true);
  36. $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha')->andReturn('Solo caracteres alfabeticos permitidos');
  37. $description = RuleDescriptionParser::parse('alpha')->getDescription();
  38. $this->assertEquals('Solo caracteres alfabeticos permitidos', $description);
  39. }
  40. public function testReturnsDescriptionInDefaultLanguageIfNotAvailableInMainLanguage()
  41. {
  42. $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(false);
  43. $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.alpha', 'en')->andReturn(true);
  44. $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha', [], 'en')->andReturn('Only alphabetic characters allowed');
  45. $description = RuleDescriptionParser::parse('alpha')->getDescription();
  46. $this->assertEquals('Only alphabetic characters allowed', $description);
  47. }
  48. public function testReturnsAnEmptyDescriptionIfNotAvailable()
  49. {
  50. $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule')->andReturn(false);
  51. $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule', 'en')->andReturn(false);
  52. $description = RuleDescriptionParser::parse('dummy_rule')->getDescription();
  53. $this->assertEmpty($description);
  54. }
  55. public function testAllowsToPassParametersToTheDescription()
  56. {
  57. $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.digits')->andReturn(false);
  58. $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.digits', 'en')->andReturn(true);
  59. $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.digits', [], 'en')->andReturn('Must have an exact length of `:attribute`');
  60. $description = RuleDescriptionParser::parse('digits')->with(2)->getDescription();
  61. $this->assertEquals('Must have an exact length of `2`', $description);
  62. }
  63. public function testAllowsToPassMultipleParametersToTheDescription()
  64. {
  65. $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.required_if')->andReturn(false);
  66. $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.required_if', 'en')->andReturn(true);
  67. $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.required_if', [], 'en')->andReturn('Required if `:attribute` is `:attribute`');
  68. $description = RuleDescriptionParser::parse('required_if')->with(['2 + 2', 4])->getDescription();
  69. $this->assertEquals('Required if `2 + 2` is `4`', $description);
  70. }
  71. /**
  72. * @param \Illuminate\Foundation\Application $app
  73. *
  74. * @return array
  75. */
  76. protected function getPackageProviders($app)
  77. {
  78. return [ApiDocGeneratorServiceProvider::class];
  79. }
  80. /**
  81. * Define environment setup.
  82. *
  83. * @param \Illuminate\Foundation\Application $app
  84. *
  85. * @return void
  86. */
  87. protected function getEnvironmentSetUp($app)
  88. {
  89. $app['config']->set('app.locale', 'es'); // Just to be different to default language.
  90. $app['config']->set('app.fallback_locale', 'ch'); // Just to be different to default language.
  91. }
  92. }