Skip to content

Commit 69d0221

Browse files
authored
Merge pull request #72 from andreaselia/feature/custom-rule-support
Fix custom rule usage
2 parents 1f7dc84 + 60f9675 commit 69d0221

File tree

6 files changed

+54
-6
lines changed

6 files changed

+54
-6
lines changed

.github/workflows/ci.yml

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
name: CI
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: '0 0 * * *'
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: Install Dependencies
15+
run: php /usr/bin/composer install -q --no-ansi --no-interaction --no-scripts --no-suggest --no-progress --prefer-dist
16+
- name: Execute tests via PHPUnit
17+
run: php vendor/bin/phpunit --stop-on-failure

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
"minimum-stability": "dev",
4646
"prefer-stable": true,
4747
"require-dev": {
48-
"orchestra/testbench": "^6.12"
48+
"orchestra/testbench": "^6.12",
49+
"phpunit/phpunit": "^9.5"
4950
}
5051
}

src/Commands/ExportPostmanCommand.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,6 @@ public function makeRequest($route, $method, $routeHeaders, $requestRules)
310310
*/
311311
protected function parseRulesIntoHumanReadable($attribute, $rules): string
312312
{
313-
314313
// ... bail if user has asked for non interpreted strings:
315314
if (! $this->config['rules_to_human_readable']) {
316315
return is_array($rules) ? implode(', ', $rules) : $this->safelyStringifyClassBasedRule($rules);
@@ -433,10 +432,10 @@ protected function handleEdgeCases(array $messages): array
433432
*/
434433
protected function safelyStringifyClassBasedRule($probableRule): string
435434
{
436-
if (is_object($probableRule) && (is_subclass_of($probableRule, Rule::class) || method_exists($probableRule, '__toString'))) {
437-
return (string) $probableRule;
435+
if (!is_object($probableRule) || is_subclass_of($probableRule, Rule::class) || !method_exists($probableRule, '__toString')) {
436+
return '';
438437
}
439438

440-
return '';
439+
return (string) $probableRule;
441440
}
442441
}

tests/Feature/ExportPostmanTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ public function test_rules_printing_export_to_human_readable_works()
190190
$this->assertCount(1, $fields->where('key', 'field_2')->where('description', 'The field 2 field is required., The field 2 must be an integer.'));
191191
$this->assertCount(1, $fields->where('key', 'field_3')->where('description', '(Optional), The field 3 must be an integer.'));
192192
$this->assertCount(1, $fields->where('key', 'field_4')->where('description', '(Nullable), The field 4 must be an integer.'));
193-
$this->assertCount(1, $fields->where('key', 'field_5')->where('description', 'The field 5 field is required., The field 5 must be an integer., The field 5 may not be greater than 30., The field 5 must be at least 1.'));
193+
$this->assertCount(1, $fields->where('key', 'field_5')->where('description', 'The field 5 field is required., The field 5 must be an integer., The field 5 must not be greater than 30., The field 5 must be at least 1.'));
194194

195195
/** This looks bad, but this is the default message in lang/en/validation.php, you can update to:.
196196
*

tests/Fixtures/ExampleFormRequest.php

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public function rules(): array
1616
'field_4' => 'nullable|integer',
1717
'field_5' => 'required|integer|max:30|min:1',
1818
'field_6' => new In([1, 2, 3]),
19+
'field_7' => new UppercaseRule,
1920
];
2021
}
2122
}

tests/Fixtures/UppercaseRule.php

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace AndreasElia\PostmanGenerator\Tests\Fixtures;
4+
5+
use Illuminate\Contracts\Validation\Rule;
6+
7+
class UppercaseRule implements Rule
8+
{
9+
/**
10+
* Determine if the validation rule passes.
11+
*
12+
* @param string $attribute
13+
* @param mixed $value
14+
* @return bool
15+
*/
16+
public function passes($attribute, $value)
17+
{
18+
return strtoupper($value) === $value;
19+
}
20+
21+
/**
22+
* Get the validation error message.
23+
*
24+
* @return string
25+
*/
26+
public function message()
27+
{
28+
return 'The :attribute must be uppercase.';
29+
}
30+
}

0 commit comments

Comments
 (0)