|
@@ -2,17 +2,37 @@
|
|
|
|
|
|
namespace Mpociot\ApiDoc\Tests;
|
|
|
|
|
|
+use Illuminate\Translation\LoaderInterface;
|
|
|
+use Illuminate\Translation\Translator;
|
|
|
use Mpociot\ApiDoc\ApiDocGeneratorServiceProvider;
|
|
|
use Mpociot\ApiDoc\Parsers\RuleDescriptionParser;
|
|
|
use Orchestra\Testbench\TestCase;
|
|
|
+use Mockery as m;
|
|
|
|
|
|
class RuleDescriptionParserTest extends TestCase
|
|
|
{
|
|
|
+ protected $translatorMock;
|
|
|
+
|
|
|
+ public function setUp()
|
|
|
+ {
|
|
|
+ parent::setUp();
|
|
|
+ $fileLoaderMock = m::mock(LoaderInterface::class);
|
|
|
+ $this->translatorMock = m::mock(Translator::class, [$fileLoaderMock, 'es']);
|
|
|
+ $this->app->instance('translator', $this->translatorMock);
|
|
|
+ }
|
|
|
+
|
|
|
+ public function tearDown()
|
|
|
+ {
|
|
|
+ m::close();
|
|
|
+ }
|
|
|
+
|
|
|
public function testReturnsAnEmptyDescriptionIfARuleIsNotParsed()
|
|
|
{
|
|
|
- $rule = new RuleDescriptionParser();
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->twice()->andReturn(false);
|
|
|
+
|
|
|
+ $description = new RuleDescriptionParser();
|
|
|
|
|
|
- $this->assertEmpty($rule->getDescription());
|
|
|
+ $this->assertEmpty($description->getDescription());
|
|
|
}
|
|
|
|
|
|
public function testProvidesANamedContructor()
|
|
@@ -20,51 +40,57 @@ class RuleDescriptionParserTest extends TestCase
|
|
|
$this->assertInstanceOf(RuleDescriptionParser::class, RuleDescriptionParser::parse());
|
|
|
}
|
|
|
|
|
|
- public function testReturnsADescriptionInTheMainLanguageOfTheApplication()
|
|
|
+ public function testReturnsADescriptionInMainLanguageIfAvailable()
|
|
|
{
|
|
|
- $expected = 'Only alphabetic characters allowed';
|
|
|
- $rule = new RuleDescriptionParser('alpha');
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(true);
|
|
|
+ $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha')->andReturn('Solo caracteres alfabeticos permitidos');
|
|
|
|
|
|
- $this->assertEquals($expected, $rule->getDescription());
|
|
|
+ $description = RuleDescriptionParser::parse('alpha')->getDescription();
|
|
|
+
|
|
|
+ $this->assertEquals('Solo caracteres alfabeticos permitidos', $description);
|
|
|
}
|
|
|
|
|
|
- public function testReturnsAnEmptyDescriptionIfNotAvailable()
|
|
|
+ public function testReturnsDescriptionInDefaultLanguageIfNotAvailableInMainLanguage()
|
|
|
{
|
|
|
- $rule = new RuleDescriptionParser('dummy_rule');
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.alpha')->andReturn(false);
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.alpha', 'en')->andReturn(true);
|
|
|
+ $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.alpha', [], 'en')->andReturn('Only alphabetic characters allowed');
|
|
|
|
|
|
- $description = $rule->getDescription();
|
|
|
+ $description = RuleDescriptionParser::parse('alpha')->getDescription();
|
|
|
|
|
|
- $this->assertEmpty($description);
|
|
|
+ $this->assertEquals('Only alphabetic characters allowed', $description);
|
|
|
}
|
|
|
|
|
|
- public function testAllowsToPassParametersToTheDescription()
|
|
|
+ public function testReturnsAnEmptyDescriptionIfNotAvailable()
|
|
|
{
|
|
|
- $expected = 'Must have an exact length of `2`';
|
|
|
- $rule = new RuleDescriptionParser('digits');
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule')->andReturn(false);
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.dummy_rule', 'en')->andReturn(false);
|
|
|
|
|
|
- $actual = $rule->with(2)->getDescription();
|
|
|
+ $description = RuleDescriptionParser::parse('dummy_rule')->getDescription();
|
|
|
|
|
|
- $this->assertEquals($expected, $actual);
|
|
|
+ $this->assertEmpty($description);
|
|
|
}
|
|
|
|
|
|
- public function testOnlyPassesParametersIfTheDescriptionAllows()
|
|
|
+ public function testAllowsToPassParametersToTheDescription()
|
|
|
{
|
|
|
- $expected = 'Only alphabetic characters allowed';
|
|
|
- $rule = new RuleDescriptionParser('alpha');
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.digits')->andReturn(false);
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.digits', 'en')->andReturn(true);
|
|
|
+ $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.digits', [], 'en')->andReturn('Must have an exact length of `:attribute`');
|
|
|
|
|
|
- $actual = $rule->with('dummy parameter')->getDescription();
|
|
|
+ $description = RuleDescriptionParser::parse('digits')->with(2)->getDescription();
|
|
|
|
|
|
- $this->assertEquals($expected, $actual);
|
|
|
+ $this->assertEquals('Must have an exact length of `2`', $description);
|
|
|
}
|
|
|
|
|
|
public function testAllowsToPassMultipleParametersToTheDescription()
|
|
|
{
|
|
|
- $expected = 'Required if `2 + 2` is `4`';
|
|
|
- $rule = new RuleDescriptionParser('required_if');
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->twice()->with('apidoc::rules.required_if')->andReturn(false);
|
|
|
+ $this->translatorMock->shouldReceive('hasForLocale')->once()->with('apidoc::rules.required_if', 'en')->andReturn(true);
|
|
|
+ $this->translatorMock->shouldReceive('get')->once()->with('apidoc::rules.required_if', [], 'en')->andReturn('Required if `:attribute` is `:attribute`');
|
|
|
|
|
|
- $actual = $rule->with(['2 + 2', 4])->getDescription();
|
|
|
+ $description = RuleDescriptionParser::parse('required_if')->with(['2 + 2', 4])->getDescription();
|
|
|
|
|
|
- $this->assertEquals($expected, $actual);
|
|
|
+ $this->assertEquals('Required if `2 + 2` is `4`', $description);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -80,12 +106,13 @@ class RuleDescriptionParserTest extends TestCase
|
|
|
/**
|
|
|
* Define environment setup.
|
|
|
*
|
|
|
- * @param \Illuminate\Foundation\Application $app
|
|
|
+ * @param \Illuminate\Foundation\Application $app
|
|
|
*
|
|
|
* @return void
|
|
|
*/
|
|
|
protected function getEnvironmentSetUp($app)
|
|
|
{
|
|
|
- $app['config']->set('app.locale', 'en');
|
|
|
+ $app['config']->set('app.locale', 'es'); // Just to be different to default language.
|
|
|
+ $app['config']->set('app.fallback_locale', 'ch'); // Just to be different to default language.
|
|
|
}
|
|
|
}
|