1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
/* |
3
|
|
|
* This file is part of FlexPHP. |
4
|
|
|
* |
5
|
|
|
* (c) Freddie Gar <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
namespace FlexPHP\UseCases\Tests\Unit; |
11
|
|
|
|
12
|
|
|
use FlexPHP\Messages\ResponseInterface; |
13
|
|
|
use FlexPHP\UseCases\Exception\NotValidRequestException; |
14
|
|
|
use FlexPHP\UseCases\Exception\UnavailableRepositoryUseCaseException; |
15
|
|
|
use FlexPHP\UseCases\Exception\UndefinedRepositoryUseCaseException; |
16
|
|
|
use FlexPHP\UseCases\Tests\Mocks\GatewayMock; |
17
|
|
|
use FlexPHP\UseCases\Tests\Mocks\RepositoryMock; |
18
|
|
|
use FlexPHP\UseCases\Tests\Mocks\RequestMock; |
19
|
|
|
use FlexPHP\UseCases\Tests\Mocks\RequestNotValidMock; |
20
|
|
|
use FlexPHP\UseCases\Tests\Mocks\UnavailableRepositoryMock; |
21
|
|
|
use FlexPHP\UseCases\Tests\Mocks\UseCaseMock; |
22
|
|
|
use FlexPHP\UseCases\Tests\TestCase; |
23
|
|
|
use FlexPHP\UseCases\UseCaseInterface; |
24
|
|
|
|
25
|
|
|
class UseCaseTest extends TestCase |
26
|
|
|
{ |
27
|
|
|
public function testItUseInterface(): void |
28
|
|
|
{ |
29
|
|
|
$useCase = new UseCaseMock(); |
30
|
|
|
|
31
|
|
|
$this->assertInstanceOf(UseCaseInterface::class, $useCase); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @throws UndefinedRepositoryUseCaseException |
36
|
|
|
*/ |
37
|
|
|
public function testItInitializeWithRepository(): void |
38
|
|
|
{ |
39
|
|
|
$foo = 'Hello'; |
40
|
|
|
$bar = 'World'; |
41
|
|
|
$gateway = new GatewayMock(); |
42
|
|
|
$repository = new RepositoryMock($gateway); |
43
|
|
|
$request = new RequestMock(\compact('foo', 'bar')); |
44
|
|
|
$useCase = new UseCaseMock($repository); |
45
|
|
|
|
46
|
|
|
$response = $useCase->execute($request); |
47
|
|
|
|
48
|
|
|
$this->assertSame($repository, $useCase->getRepository()); |
49
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
50
|
|
|
$this->assertSame(1, $response->response()); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @throws UndefinedRepositoryUseCaseException |
55
|
|
|
*/ |
56
|
|
|
public function testItInitializeRepositoryUsingSetter(): void |
57
|
|
|
{ |
58
|
|
|
$foo = 'Hello'; |
59
|
|
|
$bar = 'World'; |
60
|
|
|
$gateway = new GatewayMock(); |
61
|
|
|
$repository = new RepositoryMock($gateway); |
62
|
|
|
$request = new RequestMock(\compact('foo', 'bar')); |
63
|
|
|
$useCase = new UseCaseMock(); |
64
|
|
|
$useCase->setRepository($repository); |
65
|
|
|
|
66
|
|
|
$response = $useCase->execute($request); |
67
|
|
|
|
68
|
|
|
$this->assertSame($repository, $useCase->getRepository()); |
69
|
|
|
$this->assertInstanceOf(ResponseInterface::class, $response); |
70
|
|
|
$this->assertSame(1, $response->response()); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* @throws NotValidRequestException |
75
|
|
|
*/ |
76
|
|
|
public function testItGetNotValidRequestThrowException(): void |
77
|
|
|
{ |
78
|
|
|
$this->expectException(NotValidRequestException::class); |
79
|
|
|
$this->expectExceptionMessage('Request'); |
80
|
|
|
|
81
|
|
|
$useCase = new UseCaseMock(); |
82
|
|
|
$useCase->execute(new RequestNotValidMock()); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @throws NotValidRequestException |
87
|
|
|
*/ |
88
|
|
|
public function testItGetNotValidRequestNotClassThrowException(): void |
89
|
|
|
{ |
90
|
|
|
$this->expectException(NotValidRequestException::class); |
91
|
|
|
$this->expectExceptionMessage('Request'); |
92
|
|
|
|
93
|
|
|
$useCase = new UseCaseMock(); |
94
|
|
|
$useCase->execute([]); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @throws UndefinedRepositoryUseCaseException |
99
|
|
|
*/ |
100
|
|
|
public function testItGetUndefinedRepositoryThrowException(): void |
101
|
|
|
{ |
102
|
|
|
$this->expectException(UndefinedRepositoryUseCaseException::class); |
103
|
|
|
|
104
|
|
|
$useCase = new UseCaseMock(); |
105
|
|
|
$useCase->getRepository(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* @throws UnavailableRepositoryUseCaseException |
110
|
|
|
*/ |
111
|
|
|
public function testItGetUnavailableRepositoryThrowException(): void |
112
|
|
|
{ |
113
|
|
|
$this->expectException(UnavailableRepositoryUseCaseException::class); |
114
|
|
|
|
115
|
|
|
$repository = new UnavailableRepositoryMock(); |
116
|
|
|
$useCase = new UseCaseMock($repository); |
117
|
|
|
$useCase->execute(new RequestMock()); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|