Skip to content

Commit 588dcb0

Browse files
committed
refactor: #10 class extends
1 parent 8a1aaa8 commit 588dcb0

File tree

5 files changed

+56
-38
lines changed

5 files changed

+56
-38
lines changed

src/Base.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
namespace Coding;
4+
5+
use Coding\Exceptions\ValidationException;
6+
use Coding\Handlers\Validator;
7+
8+
abstract class Base
9+
{
10+
protected Core $core;
11+
12+
public function __construct(string $token = '', Core $core = null)
13+
{
14+
$this->core = $core ?? new Core($token);
15+
}
16+
17+
public function setToken(string $token)
18+
{
19+
$this->core->setToken($token);
20+
}
21+
22+
protected function validate(array $data, array $rules)
23+
{
24+
$validator = Validator::getInstance()->make($data, $rules);
25+
if ($validator->fails()) {
26+
throw new ValidationException($validator->errors()->all()[0]);
27+
}
28+
}
29+
}

src/Iteration.php

+2-21
Original file line numberDiff line numberDiff line change
@@ -2,38 +2,19 @@
22

33
namespace Coding;
44

5-
use Coding\Exceptions\ValidationException;
6-
use Coding\Handlers\Validator;
7-
8-
class Iteration
5+
class Iteration extends Base
96
{
10-
private Core $core;
11-
12-
public function __construct(string $token = '', Core $core = null)
13-
{
14-
$this->core = $core ?? new Core($token);
15-
}
16-
177
public function create(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-
throw new ValidationException($validator->errors()->all()[0]);
30-
}
3117
$response = $this->core->request('CreateIteration', $data);
3218
return $response['Iteration'];
3319
}
34-
35-
public function setToken(string $token)
36-
{
37-
$this->core->setToken($token);
38-
}
3920
}

tests/CoreTest.php

+10
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,20 @@
44

55
use Coding\Exceptions\ApiError;
66
use Coding\Core;
7+
use GuzzleHttp\Client;
78
use GuzzleHttp\Psr7\Response;
89

910
class CoreTest extends TestCase
1011
{
12+
private Client $clientMock;
13+
protected bool $needCoreMock = false;
14+
15+
protected function setUp(): void
16+
{
17+
parent::setUp();
18+
$this->clientMock = $this->getMockBuilder(Client::class)->getMock();
19+
}
20+
1121
public function testRequestSuccess()
1222
{
1323
$responseBody = file_get_contents($this->dataPath('CreateIterationResponse.json'));

tests/IterationTest.php

+6-12
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ class IterationTest extends TestCase
1010
{
1111
public function testCreateSuccessWithOnlyRequiredParams()
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 = new Iteration($this->token, $coreMock);
26+
$iteration = new Iteration($this->token, $this->coreMock);
2927
$result = $iteration->create($data);
3028
$this->assertEquals($response['Iteration'], $result);
3129
}
3230

3331
public function testCreateSuccessWithRequiredParamsAndNull()
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 = new Iteration($this->token, $coreMock);
47+
$iteration = new Iteration($this->token, $this->coreMock);
5248
$result = $iteration->create($data);
5349
$this->assertEquals($response['Iteration'], $result);
5450
}
5551

5652
public function testCreateSuccessWithAllParams()
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 = new Iteration('', $coreMock);
72+
$iteration = new Iteration('', $this->coreMock);
7973
$iteration->setToken($this->token);
8074
$result = $iteration->create($data);
8175
$this->assertEquals($response['Iteration'], $result);

tests/TestCase.php

+9-5
Original file line numberDiff line numberDiff line change
@@ -2,25 +2,29 @@
22

33
namespace Coding\Tests;
44

5+
use Coding\Core;
56
use Faker\Factory;
67
use Faker\Generator;
7-
use GuzzleHttp\Client;
8-
use PHPUnit\Framework\TestCase as BaseTestBase;
8+
use Mockery\Mock;
9+
use PHPUnit\Framework\TestCase as PhpUnitTestBase;
910

10-
class TestCase extends BaseTestBase
11+
class TestCase extends PhpunitTestBase
1112
{
1213
protected Generator $faker;
13-
protected Client $clientMock;
1414
protected string $token;
1515
protected string $projectName;
16+
protected bool $needCoreMock = true;
17+
protected $coreMock;
1618

1719
protected function setUp(): 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
protected function dataPath($fileName): string

0 commit comments

Comments
 (0)