Movatterモバイル変換


[0]ホーム

URL:


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

Commitea4eade

Browse files
committed
refactor:#10 class extends
1 parent8a1aaa8 commitea4eade

File tree

5 files changed

+56
-38
lines changed

5 files changed

+56
-38
lines changed

‎src/Base.php‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespaceCoding;
4+
5+
useCoding\Exceptions\ValidationException;
6+
useCoding\Handlers\Validator;
7+
8+
abstractclass Base
9+
{
10+
protectedCore$core;
11+
12+
publicfunction__construct(string$token ='',Core$core =null)
13+
{
14+
$this->core =$core ??newCore($token);
15+
}
16+
17+
publicfunctionsetToken(string$token)
18+
{
19+
$this->core->setToken($token);
20+
}
21+
22+
protectedfunctionvalidate(array$data,array$rules)
23+
{
24+
$validator = Validator::getInstance()->make($data,$rules);
25+
if ($validator->fails()) {
26+
thrownewValidationException($validator->errors()->all()[0]);
27+
}
28+
}
29+
}

‎src/Iteration.php‎

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,19 @@
22

33
namespaceCoding;
44

5-
useCoding\Exceptions\ValidationException;
6-
useCoding\Handlers\Validator;
7-
8-
class Iteration
5+
class Iterationextends Base
96
{
10-
privateCore$core;
11-
12-
publicfunction__construct(string$token ='',Core$core =null)
13-
{
14-
$this->core =$core ??newCore($token);
15-
}
16-
177
publicfunctioncreate(array$data)
188
{
19-
$validator = Validator::getInstance()->make($data, [
9+
$this->validate($data, [
2010
'ProjectName' =>'string|required',
2111
'Name' =>'string|required',
2212
'Goal' =>'nullable|string',
2313
'Assignee' =>'nullable|integer',
2414
'StartAt' =>'nullable|date_format:Y-m-d',
2515
'EndAt' =>'nullable|date_format:Y-m-d|after:StartAt',
2616
]);
27-
if ($validator->fails()) {
28-
// TODO Laravel ValidationException no message
29-
thrownewValidationException($validator->errors()->all()[0]);
30-
}
3117
$response =$this->core->request('CreateIteration',$data);
3218
return$response['Iteration'];
3319
}
34-
35-
publicfunctionsetToken(string$token)
36-
{
37-
$this->core->setToken($token);
38-
}
3920
}

‎tests/CoreTest.php‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
useCoding\Exceptions\ApiError;
66
useCoding\Core;
7+
useGuzzleHttp\Client;
78
useGuzzleHttp\Psr7\Response;
89

910
class CoreTestextends TestCase
1011
{
12+
privateClient$clientMock;
13+
protectedbool$needCoreMock =false;
14+
15+
protectedfunctionsetUp():void
16+
{
17+
parent::setUp();
18+
$this->clientMock =$this->getMockBuilder(Client::class)->getMock();
19+
}
20+
1121
publicfunctiontestRequestSuccess()
1222
{
1323
$responseBody =file_get_contents($this->dataPath('CreateIterationResponse.json'));

‎tests/IterationTest.php‎

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class IterationTest extends TestCase
1010
{
1111
publicfunctiontestCreateSuccessWithOnlyRequiredParams()
1212
{
13-
$coreMock = \Mockery::mock(Core::class, [])->makePartial();
14-
1513
$response =json_decode(
1614
file_get_contents($this->dataPath('CreateIterationResponse.json')),
1715
true
@@ -20,20 +18,18 @@ public function testCreateSuccessWithOnlyRequiredParams()
2018
'ProjectName' =>$this->projectName,
2119
'Name' =>$this->faker->title,
2220
];
23-
$coreMock->shouldReceive('request')->times(1)->withArgs([
21+
$this->coreMock->shouldReceive('request')->times(1)->withArgs([
2422
'CreateIteration',
2523
$data
2624
])->andReturn($response);
2725

28-
$iteration =newIteration($this->token,$coreMock);
26+
$iteration =newIteration($this->token,$this->coreMock);
2927
$result =$iteration->create($data);
3028
$this->assertEquals($response['Iteration'],$result);
3129
}
3230

3331
publicfunctiontestCreateSuccessWithRequiredParamsAndNull()
3432
{
35-
$coreMock = \Mockery::mock(Core::class, [])->makePartial();
36-
3733
$response =json_decode(
3834
file_get_contents($this->dataPath('CreateIterationResponse.json')),
3935
true
@@ -43,20 +39,18 @@ public function testCreateSuccessWithRequiredParamsAndNull()
4339
'Name' =>$this->faker->title,
4440
'Goal' =>null,
4541
];
46-
$coreMock->shouldReceive('request')->times(1)->withArgs([
42+
$this->coreMock->shouldReceive('request')->times(1)->withArgs([
4743
'CreateIteration',
4844
$data
4945
])->andReturn($response);
5046

51-
$iteration =newIteration($this->token,$coreMock);
47+
$iteration =newIteration($this->token,$this->coreMock);
5248
$result =$iteration->create($data);
5349
$this->assertEquals($response['Iteration'],$result);
5450
}
5551

5652
publicfunctiontestCreateSuccessWithAllParams()
5753
{
58-
$coreMock = \Mockery::mock(Core::class, [])->makePartial();
59-
6054
$response =json_decode(
6155
file_get_contents($this->dataPath('CreateIterationResponse.json')),
6256
true
@@ -70,12 +64,12 @@ public function testCreateSuccessWithAllParams()
7064
'StartAt' =>$startAt,
7165
'EndAt' =>date('Y-m-d',strtotime($startAt) +$this->faker->randomDigitNotZero() *86400),
7266
];
73-
$coreMock->shouldReceive('request')->times(1)->withArgs([
67+
$this->coreMock->shouldReceive('request')->times(1)->withArgs([
7468
'CreateIteration',
7569
$data
7670
])->andReturn($response);
7771

78-
$iteration =newIteration('',$coreMock);
72+
$iteration =newIteration('',$this->coreMock);
7973
$iteration->setToken($this->token);
8074
$result =$iteration->create($data);
8175
$this->assertEquals($response['Iteration'],$result);

‎tests/TestCase.php‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
namespaceCoding\Tests;
44

5+
useCoding\Core;
56
useFaker\Factory;
67
useFaker\Generator;
7-
useGuzzleHttp\Client;
8-
usePHPUnit\Framework\TestCaseasBaseTestBase;
8+
useMockery\Mock;
9+
usePHPUnit\Framework\TestCaseasPhpUnitTestBase;
910

10-
class TestCaseextendsBaseTestBase
11+
class TestCaseextendsPhpunitTestBase
1112
{
1213
protectedGenerator$faker;
13-
protectedClient$clientMock;
1414
protectedstring$token;
1515
protectedstring$projectName;
16+
protectedbool$needCoreMock =true;
17+
protected$coreMock;
1618

1719
protectedfunctionsetUp():void
1820
{
1921
parent::setUp();
2022
$this->faker = Factory::create();
21-
$this->clientMock =$this->getMockBuilder(Client::class)->getMock();
2223
$this->token =$this->faker->md5;
2324
$this->projectName =$this->faker->slug;
25+
if ($this->needCoreMock) {
26+
$this->coreMock = \Mockery::mock(Core::class, [])->makePartial();
27+
}
2428
}
2529

2630
protectedfunctiondataPath($fileName):string

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp