-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmockObject.php
138 lines (111 loc) · 2.83 KB
/
mockObject.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
interface Service
{
public function make(): string;
}
class ServiceImplementation implements Service
{
public function make(): string
{
return __METHOD__;
}
}
interface Logger
{
public function write(string $message): void;
}
class LoggerImplementation implements Logger
{
public function write(string $message): void
{
echo 'Log: ' . $message . '<br>';
}
}
class ActionClientUseCase
{
public function __construct(
private Service $service,
private Logger $logger
) {
}
public function execute(): string
{
$text = $this->service->make();
$this->logger->write($text);
return $text;
}
}
/**
**************************************************************************************************************
* Mocking
**************************************************************************************************************
*/
class StubService implements Service
{
public function make(): string
{
return 'Test';
}
}
interface Mock
{
public function mockObjectMethodWasExecutedCorrectly(): bool;
}
class MockLogger implements Logger, Mock
{
private bool $isExecuted = false;
public function write(string $message): void
{
$this->setIsExecutedTrue();
}
private function setIsExecutedTrue(): void
{
$this->isExecuted = true;
}
public function mockObjectMethodWasExecutedCorrectly(): bool
{
return $this->isExecuted;
}
}
class ActionClientUseCaseTest
{
private MockLogger $logger;
private ActionClientUseCase $actionClientUseCase;
public function run(): void
{
$this->setUp();
$this->test_succeed_client_action();
}
protected function setUp(): void
{
$service = new StubService();
$this->logger = new MockLogger();
$this->actionClientUseCase = new ActionClientUseCase($service, $this->logger);
}
protected function test_succeed_client_action(): void
{
$response = $this->actionClientUseCase->execute();
$this->assertTrue($this->logger->mockObjectMethodWasExecutedCorrectly());
$this->assertEquals('Test', $response);
}
protected function assertTrue(mixed $condition): void
{
$this->assertEquals(true, $condition);
}
protected function assertEquals(mixed $expected, mixed $actual): void
{
if ($expected === $actual) {
echo '<p style="color: green;">OK</p>';
return;
}
echo '<p style="color: red;">Error</p>';
}
}
/**
* Client
*
* $actionClientUseCase = new ActionClientUseCase(new ServiceImplementation(), new LoggerImplementation());
* echo $actionClientUseCase->execute();
*/
$actionClientUseCaseTest = new ActionClientUseCaseTest();
$actionClientUseCaseTest->run();