Skip to content

Commit

Permalink
Merge pull request #14 from weretyczx/bug-fix
Browse files Browse the repository at this point in the history
fix Str::startsWith need string error
  • Loading branch information
mtrajano committed Dec 6, 2019
2 parents 56813cb + a204e3a commit 4b07bd8
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/Parameters/Concerns/GeneratesFromRules.php
Expand Up @@ -62,11 +62,11 @@ protected function getEnumValues(array $paramRules)
private function getInParameter(array $paramRules)
{
foreach ($paramRules as $rule) {
if (Str::startsWith($rule, 'in:')) {
if ((is_string($rule) || method_exists($rule , '__toString')) && Str::startsWith($rule, 'in:')) {
return $rule;
}
}

return false;
}
}
}
59 changes: 59 additions & 0 deletions tests/Parameters/BodyParameterGeneratorTest.php
Expand Up @@ -2,8 +2,10 @@

namespace Mtrajano\LaravelSwagger\Tests\Parameters;

use Illuminate\Validation\Rule;
use Mtrajano\LaravelSwagger\Tests\TestCase;
use Mtrajano\LaravelSwagger\Parameters\BodyParameterGenerator;
use Mtrajano\LaravelSwagger\Tests\Stubs\Rules\Uppercase as UppercaseRule;

class BodyParameterGeneratorTest extends TestCase
{
Expand Down Expand Up @@ -153,6 +155,63 @@ public function testSingleObjectSyntax()
], $bodyParameters['schema']['properties']);
}

public function testResolvesRuleEnum()
{

$bodyParameters = $this->getBodyParameters([
'type' => [
Rule::in(1,2,3),
'integer',
]
]);

$this->assertEquals([
'type' => [
'type' => 'integer',
'enum' => ['"1"','"2"','"3"'], //using Rule::in parameters are cast to string
]
], $bodyParameters['schema']['properties']);

}

public function testIgnoresRuleObject()
{

$bodyParameters = $this->getBodyParameters([
'name' => [
'string',
new UppercaseRule
],
]);

$this->assertEquals([
'name' => [
'type' => 'string',
]
], $bodyParameters['schema']['properties']);

}

public function testIgnoresClosureRules()
{
$bodyParameters = $this->getBodyParameters([
'name' => [
'string',
function ($attribute, $value, $fail) {
if ($value === 'foo') {
$fail($attribute.' is invalid.');
}
},
],
]);

$this->assertEquals([
'name' => [
'type' => 'string',
]
], $bodyParameters['schema']['properties']);
}

private function getBodyParameters(array $rules)
{
$bodyParameters = (new BodyParameterGenerator($rules))->getParameters();
Expand Down
30 changes: 30 additions & 0 deletions tests/Stubs/Rules/Uppercase.php
@@ -0,0 +1,30 @@
<?php

namespace Mtrajano\LaravelSwagger\Tests\Stubs\Rules;

use Illuminate\Contracts\Validation\Rule;

class Uppercase implements Rule
{
/**
* Determine if the validation rule passes.
*
* @param string $attribute
* @param mixed $value
* @return bool
*/
public function passes($attribute, $value)
{
return strtoupper($value) === $value;
}

/**
* Get the validation error message.
*
* @return string
*/
public function message()
{
return 'The :attribute must be uppercase.';
}
}

0 comments on commit 4b07bd8

Please sign in to comment.