Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 51b531e

Browse files
feat: #4 params validation
1 parent 668e585 commit 51b531e

File tree

8 files changed

+107
-4
lines changed

8 files changed

+107
-4
lines changed

‎composer.json‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"require": {
66
"php": ">=7.4",
77
"ext-json": "*",
8-
"guzzlehttp/guzzle": "^7.4"
8+
"guzzlehttp/guzzle": "^7.4",
9+
"illuminate/validation": "^8.67"
910
},
1011
"require-dev": {
1112
"fakerphp/faker": "^1.16",

‎src/Core.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Coding;
44

5+
use Coding\Exceptions\ApiError;
56
use GuzzleHttp\Client;
67

78
class Core

‎src/ApiError.php‎ renamed to ‎src/Exceptions/ApiError.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Coding;
3+
namespace Coding\Exceptions;
44

55
use Exception;
66

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
3+
namespace Coding\Exceptions;
4+
5+
use Exception;
6+
7+
class ValidationException extends Exception
8+
{
9+
}

‎src/Handlers/Validator.php‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Coding\Handlers;
4+
5+
use Illuminate\Filesystem\Filesystem;
6+
use Illuminate\Translation\FileLoader;
7+
use Illuminate\Translation\Translator;
8+
use Illuminate\Validation\Factory;
9+
10+
class Validator extends Factory
11+
{
12+
public static function getInstance(): Factory
13+
{
14+
static $validator = null;
15+
if ($validator === null) {
16+
$fileLoader = new FileLoader(new Filesystem(), __DIR__ . '/../../lang');
17+
$translator = new Translator($fileLoader, 'en');
18+
$validator = new Factory($translator);
19+
}
20+
return $validator;
21+
}
22+
}

‎src/Iteration.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
namespace Coding;
44

5+
use Coding\Exceptions\ValidationException;
6+
use Coding\Handlers\Validator;
7+
58
class Iteration
69
{
710
private Core $core;
@@ -13,6 +16,18 @@ public function __construct(string $token, Core $core = null)
1316

1417
public function create(array $data)
1518
{
19+
$validator = Validator::getInstance()->make($data, [
20+
'ProjectName' => 'string|required',
21+
'Name' => 'string|required',
22+
'Goal' => 'string',
23+
'Assignee' => 'integer',
24+
'StartAt' => 'date_format:Y-m-d',
25+
'EndAt' => 'date_format:Y-m-d|after:StartAt',
26+
]);
27+
if ($validator->fails()) {
28+
// TODO Laravel ValidationException no message
29+
throw new ValidationException($validator->errors()->all()[0]);
30+
}
1631
$response = $this->core->request('CreateIteration', $data);
1732
return $response['Iteration'];
1833
}

‎tests/CoreTest.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Coding\Tests;
44

5-
use Coding\ApiError;
5+
use Coding\Exceptions\ApiError;
66
use Coding\Core;
77
use GuzzleHttp\Psr7\Response;
88

‎tests/IterationTest.php‎

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@
33
namespace Coding\Tests;
44

55
use Coding\Core;
6+
use Coding\Exceptions\ValidationException;
67
use Coding\Iteration;
78

89
class IterationTest extends TestCase
910
{
10-
public function testCreateSuccess()
11+
public function testCreateSuccessWithOnlyRequiredParams()
1112
{
1213
$coreMock = \Mockery::mock(Core::class, [])->makePartial();
1314

@@ -28,4 +29,58 @@ public function testCreateSuccess()
2829
$result = $iteration->create($data);
2930
$this->assertEquals($response['Iteration'], $result);
3031
}
32+
33+
public function testCreateSuccessWithAllParams()
34+
{
35+
$coreMock = \Mockery::mock(Core::class, [])->makePartial();
36+
37+
$response = json_decode(
38+
file_get_contents($this->dataPath('CreateIterationResponse.json')),
39+
true
40+
)['Response'];
41+
$startAt = $this->faker->date();
42+
$data = [
43+
'ProjectName' => $this->projectName,
44+
'Name' => $this->faker->title,
45+
'Goal' => $this->faker->sentence,
46+
'Assignee' => $this->faker->randomNumber(),
47+
'StartAt' => $startAt,
48+
'EndAt' => date('Y-m-d', strtotime($startAt) + $this->faker->randomDigitNotZero() * 86400),
49+
];
50+
$coreMock->shouldReceive('request')->times(1)->withArgs([
51+
'CreateIteration',
52+
$data
53+
])->andReturn($response);
54+
55+
$iteration = new Iteration($this->token, $coreMock);
56+
$result = $iteration->create($data);
57+
$this->assertEquals($response['Iteration'], $result);
58+
}
59+
60+
public function testCreateFailedRequired()
61+
{
62+
$data = [
63+
'ProjectName' => $this->projectName,
64+
];
65+
66+
$iteration = new Iteration($this->token);
67+
$this->expectException(ValidationException::class);
68+
$this->expectExceptionMessage('The name field is required.');
69+
$iteration->create($data);
70+
}
71+
72+
public function testCreateFailedBefore()
73+
{
74+
$data = [
75+
'ProjectName' => $this->projectName,
76+
'Name' => $this->faker->title,
77+
'StartAt' => '2021年10月23日',
78+
'EndAt' => '2021年10月22日',
79+
];
80+
81+
$iteration = new Iteration($this->token);
82+
$this->expectException(ValidationException::class);
83+
$this->expectExceptionMessage('The end at must be a date after start at.');
84+
$iteration->create($data);
85+
}
3186
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /