Skip to content

Commit 579979b

Browse files
author
Masiukevich Maksim
committed
component tests
1 parent cff5273 commit 579979b

8 files changed

+86
-140
lines changed

tests/Interaction/InteractionsProviderTest.php

+33-41
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection PhpUnhandledExceptionInspection */
22

33
/**
44
* Telegram Bot API.
@@ -31,19 +31,13 @@ final class InteractionsProviderTest extends TestCase
3131
*/
3232
private $credentials;
3333

34-
/**
35-
* {@inheritdoc}
36-
*/
3734
protected function setUp(): void
3835
{
3936
parent::setUp();
4037

4138
$this->credentials = new TelegramCredentials('25896951:AAGB5PnXUTW-SuI4CIe742FKcTvPEwP82_o');
4239
}
4340

44-
/**
45-
* {@inheritdoc}
46-
*/
4741
protected function tearDown(): void
4842
{
4943
parent::tearDown();
@@ -55,101 +49,97 @@ protected function tearDown(): void
5549

5650
/**
5751
* @test
58-
*
59-
* @return void
6052
*/
6153
public function unknownMethod(): void
6254
{
6355
Loop::run(
64-
function (): \Generator
56+
function(): \Generator
6557
{
6658
/** @var \ServiceBus\TelegramBot\Interaction\Result\Fail $result */
6759
$result = yield (new InteractionsProvider(TestHttpClient::create('{}', 404)))->call(
6860
new TestMethod(),
6961
$this->credentials
7062
);
7163

72-
static::assertInstanceOf(Fail::class, $result);
73-
static::assertSame('Method TestMethod not exists', $result->errorMessage);
64+
self::assertInstanceOf(Fail::class, $result);
65+
self::assertSame('Method TestMethod not exists', $result->errorMessage);
7466
}
7567
);
7668
}
7769

7870
/**
7971
* @test
80-
*
81-
* @return void
8272
*/
8373
public function validationFailed(): void
8474
{
8575
Loop::run(
86-
function (): \Generator
76+
function(): \Generator
8777
{
8878
/** @var \ServiceBus\TelegramBot\Interaction\Result\Fail $result */
8979
$result = yield (new InteractionsProvider(TestHttpClient::create('{}', 404)))->call(
9080
new TestMethod(''),
9181
$this->credentials
9282
);
9383

94-
static::assertInstanceOf(Fail::class, $result);
95-
static::assertSame('Validation failed', $result->errorMessage);
84+
self::assertInstanceOf(Fail::class, $result);
85+
self::assertSame('Validation failed', $result->errorMessage);
86+
87+
Loop::stop();
9688
}
9789
);
9890
}
9991

10092
/**
10193
* @test
102-
*
103-
* @return void
10494
*/
10595
public function internalError(): void
10696
{
10797
Loop::run(
108-
function (): \Generator
98+
function(): \Generator
10999
{
110100
/** @var \ServiceBus\TelegramBot\Interaction\Result\Fail $result */
111101
$result = yield (new InteractionsProvider(TestHttpClient::create('{}', 500)))->call(
112102
new TestMethod(),
113103
$this->credentials
114104
);
115105

116-
static::assertInstanceOf(Fail::class, $result);
117-
static::assertSame('Incorrect server response code: 500', $result->errorMessage);
106+
self::assertInstanceOf(Fail::class, $result);
107+
self::assertSame('Incorrect server response code: 500', $result->errorMessage);
108+
109+
Loop::stop();
118110
}
119111
);
120112
}
121113

122114
/**
123115
* @test
124-
*
125-
* @return void
126116
*/
127117
public function incorrectResponsePayload(): void
128118
{
129119
Loop::run(
130-
function (): \Generator
120+
function(): \Generator
131121
{
132122
/** @var \ServiceBus\TelegramBot\Interaction\Result\Fail $result */
133123
$result = yield (new InteractionsProvider(TestHttpClient::create('{}', 200)))->call(
134124
new TestMethod(),
135125
$this->credentials
136126
);
137127

138-
static::assertInstanceOf(Fail::class, $result);
139-
static::assertSame('Incorrect response payload', $result->errorMessage);
128+
self::assertInstanceOf(Fail::class, $result);
129+
self::assertSame('Incorrect response payload', $result->errorMessage);
130+
131+
Loop::stop();
140132
}
141133
);
142134
}
143135

144136
/**
145137
* @test
146-
*
147-
* @return void
148138
*/
149139
public function successRequest(): void
150140
{
151141
Loop::run(
152-
function (): \Generator
142+
function(): \Generator
153143
{
154144
$expectedResponse = '{"ok":true,"result":{"id":1,"is_bot":true,"first_name":"First","last_name":"","username":"User"}}';
155145

@@ -159,55 +149,57 @@ function (): \Generator
159149
$this->credentials
160150
);
161151

162-
static::assertInstanceOf(Success::class, $result);
152+
self::assertInstanceOf(Success::class, $result);
163153

164154
/** @var User $type */
165155
$type = $result->type;
166156

167-
static::assertSame('1', $type->id->toString());
157+
self::assertSame('1', $type->id->toString());
158+
159+
Loop::stop();
168160
}
169161
);
170162
}
171163

172164
/**
173165
* @test
174-
*
175-
* @return void
176166
*/
177167
public function failedDownload(): void
178168
{
179169
Loop::run(
180-
function (): \Generator
170+
function(): \Generator
181171
{
182172
/** @var \ServiceBus\TelegramBot\Interaction\Result\Fail $result */
183173
$result = yield (new InteractionsProvider(TestHttpClient::failed('fail message')))->call(
184174
DownloadFile::create('', '', ''),
185175
$this->credentials
186176
);
187177

188-
static::assertInstanceOf(Fail::class, $result);
189-
static::assertSame('fail message', $result->errorMessage);
178+
self::assertInstanceOf(Fail::class, $result);
179+
self::assertSame('fail message', $result->errorMessage);
180+
181+
Loop::stop();
190182
}
191183
);
192184
}
193185

194186
/**
195187
* @test
196-
*
197-
* @return void
198188
*/
199189
public function successDownload(): void
200190
{
201191
Loop::run(
202-
function (): \Generator
192+
function(): \Generator
203193
{
204194
/** @var \ServiceBus\TelegramBot\Interaction\Result\Success $result */
205195
$result = yield (new InteractionsProvider(TestHttpClient::create('', 200)))->call(
206196
DownloadFile::create('', '', ''),
207197
$this->credentials
208198
);
209199

210-
static::assertInstanceOf(Success::class, $result);
200+
self::assertInstanceOf(Success::class, $result);
201+
202+
Loop::stop();
211203
}
212204
);
213205
}

tests/Interaction/TestHttpClient.php

+10-21
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<?php
1+
<?php /** @noinspection PhpUnhandledExceptionInspection */
22

33
/**
44
* Telegram Bot API.
@@ -41,12 +41,6 @@ final class TestHttpClient implements HttpClient
4141
*/
4242
private $expectedFailMessage;
4343

44-
/**
45-
* @param string $payload
46-
* @param int $statusCode
47-
*
48-
* @return self
49-
*/
5044
public static function create(string $payload, int $statusCode): self
5145
{
5246
$self = new self();
@@ -57,11 +51,6 @@ public static function create(string $payload, int $statusCode): self
5751
return $self;
5852
}
5953

60-
/**
61-
* @param string $message
62-
*
63-
* @return self
64-
*/
6554
public static function failed(string $message): self
6655
{
6756
$self = new self();
@@ -71,22 +60,22 @@ public static function failed(string $message): self
7160
return $self;
7261
}
7362

74-
/**
75-
* {@inheritdoc}
76-
*/
7763
public function execute(HttpRequest $requestData, ?RequestContext $context = null): Promise
7864
{
79-
return null === $this->expectedFailMessage
65+
return $this->expectedFailMessage === null
8066
? new Success(new Psr7Response($this->expectedStatusCode, [], $this->expectedPayload))
8167
: new Failure(new HttpClientException($this->expectedFailMessage));
68+
8269
}
8370

84-
/**
85-
* {@inheritdoc}
86-
*/
87-
public function download(string $fileUrl, string $destinationDirectory, string $fileName, ?RequestContext $context = null): Promise
71+
public function download(
72+
string $fileUrl,
73+
string $destinationDirectory,
74+
string $fileName,
75+
?RequestContext $context = null
76+
): Promise
8877
{
89-
return null === $this->expectedFailMessage
78+
return $this->expectedFailMessage === null
9079
? new Success(__FILE__)
9180
: new Failure(new HttpClientException($this->expectedFailMessage));
9281
}

tests/Interaction/TestMethod.php

-15
Original file line numberDiff line numberDiff line change
@@ -27,43 +27,28 @@ final class TestMethod implements TelegramMethod
2727
#[Assert\NotBlank]
2828
private $id;
2929

30-
/**
31-
* @param string $id
32-
*/
3330
public function __construct(string $id = '123')
3431
{
3532
$this->id = $id;
3633
}
3734

38-
/**
39-
* {@inheritdoc}
40-
*/
4135
public function methodName(): string
4236
{
4337
return 'TestMethod';
4438
}
4539

46-
/**
47-
* {@inheritdoc}
48-
*/
4940
public function httpRequestMethod(): string
5041
{
5142
return 'POST';
5243
}
5344

54-
/**
55-
* {@inheritdoc}
56-
*/
5745
public function requestData(): array
5846
{
5947
return [
6048
'id' => $this->id,
6149
];
6250
}
6351

64-
/**
65-
* {@inheritdoc}
66-
*/
6752
public function typeClass(): string
6853
{
6954
return User::class;

0 commit comments

Comments
 (0)