Skip to content

Commit b0018fc

Browse files
OskarStarkfabpot
authored andcommitted
[Notifier] Rework/streamline bridges (5.2)
1 parent 695de7c commit b0018fc

File tree

5 files changed

+67
-63
lines changed

5 files changed

+67
-63
lines changed

LinkedInTransport.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ protected function doSend(MessageInterface $message): SentMessage
6363
if (!$message instanceof ChatMessage) {
6464
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" (instance of "%s" given).', __CLASS__, ChatMessage::class, \get_class($message)));
6565
}
66+
6667
if ($message->getOptions() && !$message->getOptions() instanceof LinkedInOptions) {
6768
throw new LogicException(sprintf('The "%s" transport only supports instances of "%s" for options.', __CLASS__, LinkedInOptions::class));
6869
}
@@ -87,7 +88,7 @@ protected function doSend(MessageInterface $message): SentMessage
8788
$result = $response->toArray(false);
8889

8990
if (!$result['id']) {
90-
throw new TransportException(sprintf('Unable to post the Linkedin message : "%s".', $result['error']), $response);
91+
throw new TransportException(sprintf('Unable to post the Linkedin message: "%s".', $result['error']), $response);
9192
}
9293

9394
$sentMessage = new SentMessage($message, (string) $this);

LinkedInTransportFactory.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,17 @@ class LinkedInTransportFactory extends AbstractTransportFactory
2626
public function create(Dsn $dsn): TransportInterface
2727
{
2828
$scheme = $dsn->getScheme();
29+
30+
if ('linkedin' !== $scheme) {
31+
throw new UnsupportedSchemeException($dsn, 'linkedin', $this->getSupportedSchemes());
32+
}
33+
2934
$authToken = $this->getUser($dsn);
3035
$accountId = $this->getPassword($dsn);
3136
$host = 'default' === $dsn->getHost() ? null : $dsn->getHost();
3237
$port = $dsn->getPort();
3338

34-
if ('linkedin' === $scheme) {
35-
return (new LinkedInTransport($authToken, $accountId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
36-
}
37-
38-
throw new UnsupportedSchemeException($dsn, 'linkedin', $this->getSupportedSchemes());
39+
return (new LinkedInTransport($authToken, $accountId, $this->client, $this->dispatcher))->setHost($host)->setPort($port);
3940
}
4041

4142
protected function getSupportedSchemes(): array

README.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ DSN example
77
-----------
88

99
```
10-
// .env file
11-
LINKEDIN_DSN='linkedin://ACCESS_TOKEN:USER_ID@default'
10+
LINKEDIN_DSN=linkedin://ACCESS_TOKEN:USER_ID@default
1211
```
1312

1413
where:

Tests/LinkedInTransportFactoryTest.php

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,39 +12,45 @@ final class LinkedInTransportFactoryTest extends TestCase
1212
{
1313
public function testCreateWithDsn()
1414
{
15-
$factory = new LinkedInTransportFactory();
15+
$factory = $this->createFactory();
1616

17-
$dsn = 'linkedin://login:pass@default';
18-
$transport = $factory->create(Dsn::fromString($dsn));
19-
$transport->setHost('testHost');
17+
$transport = $factory->create(Dsn::fromString('linkedin://accessToken:UserId@host.test'));
2018

21-
$this->assertSame('linkedin://testHost', (string) $transport);
19+
$this->assertSame('linkedin://host.test', (string) $transport);
2220
}
2321

24-
public function testSupportsLinkedinScheme()
22+
public function testCreateWithOnlyAccessTokenOrUserIdThrowsIncompleteDsnException()
2523
{
26-
$factory = new LinkedInTransportFactory();
24+
$factory = $this->createFactory();
2725

28-
$this->assertTrue($factory->supports(Dsn::fromString('linkedin://host/path')));
29-
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/path')));
26+
$this->expectException(IncompleteDsnException::class);
27+
$factory->create(Dsn::fromString('linkedin://AccessTokenOrUserId@default'));
3028
}
3129

32-
public function testNonLinkedinSchemeThrows()
30+
public function testSupportsReturnsTrueWithSupportedScheme()
3331
{
34-
$factory = new LinkedInTransportFactory();
32+
$factory = $this->createFactory();
3533

36-
$this->expectException(UnsupportedSchemeException::class);
34+
$this->assertTrue($factory->supports(Dsn::fromString('linkedin://host/path')));
35+
}
3736

38-
$dsn = 'foo://login:pass@default';
39-
$factory->create(Dsn::fromString($dsn));
37+
public function testSupportsReturnsFalseWithUnsupportedScheme()
38+
{
39+
$factory = $this->createFactory();
40+
41+
$this->assertFalse($factory->supports(Dsn::fromString('somethingElse://host/path')));
4042
}
4143

42-
public function testIncompleteDsnMissingUserThrows()
44+
public function testUnsupportedSchemeThrowsUnsupportedSchemeException()
4345
{
44-
$factory = new LinkedInTransportFactory();
46+
$factory = $this->createFactory();
4547

46-
$this->expectException(IncompleteDsnException::class);
48+
$this->expectException(UnsupportedSchemeException::class);
49+
$factory->create(Dsn::fromString('somethingElse://accessToken:UserId@default'));
50+
}
4751

48-
$factory->create(Dsn::fromString('somethingElse://host/path'));
52+
private function createFactory(): LinkedInTransportFactory
53+
{
54+
return new LinkedInTransportFactory();
4955
}
5056
}

Tests/LinkedInTransportTest.php

Lines changed: 34 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,25 +16,26 @@
1616

1717
final class LinkedInTransportTest extends TestCase
1818
{
19-
public function testToString()
19+
public function testToStringContainsProperties()
2020
{
21-
$this->assertSame(sprintf('linkedin://host.test'), (string) $this->getTransport());
21+
$transport = $this->createTransport();
22+
23+
$this->assertSame('linkedin://host.test', (string) $transport);
2224
}
2325

2426
public function testSupportsChatMessage()
2527
{
26-
$transport = $this->getTransport();
28+
$transport = $this->createTransport();
2729

2830
$this->assertTrue($transport->supports(new ChatMessage('testChatMessage')));
2931
$this->assertFalse($transport->supports($this->createMock(MessageInterface::class)));
3032
}
3133

3234
public function testSendNonChatMessageThrows()
3335
{
34-
$this->expectException(LogicException::class);
35-
36-
$transport = $this->getTransport();
36+
$transport = $this->createTransport();
3737

38+
$this->expectException(LogicException::class);
3839
$transport->send($this->createMock(MessageInterface::class));
3940
}
4041

@@ -54,7 +55,7 @@ public function testSendWithEmptyArrayResponseThrows()
5455
return $response;
5556
});
5657

57-
$transport = $this->getTransport($client);
58+
$transport = $this->createTransport($client);
5859

5960
$transport->send(new ChatMessage('testMessage'));
6061
}
@@ -77,7 +78,7 @@ public function testSendWithErrorResponseThrows()
7778
return $response;
7879
});
7980

80-
$transport = $this->getTransport($client);
81+
$transport = $this->createTransport($client);
8182

8283
$transport->send(new ChatMessage('testMessage'));
8384
}
@@ -98,19 +99,19 @@ public function testSendWithOptions()
9899

99100
$expectedBody = json_encode([
100101
'specificContent' => [
101-
'com.linkedin.ugc.ShareContent' => [
102-
'shareCommentary' => [
103-
'attributes' => [],
104-
'text' => 'testMessage',
105-
],
106-
'shareMediaCategory' => 'NONE',
107-
],
102+
'com.linkedin.ugc.ShareContent' => [
103+
'shareCommentary' => [
104+
'attributes' => [],
105+
'text' => 'testMessage',
108106
],
109-
'visibility' => [
110-
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
107+
'shareMediaCategory' => 'NONE',
111108
],
109+
],
110+
'visibility' => [
111+
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
112+
],
112113
'lifecycleState' => 'PUBLISHED',
113-
'author' => 'urn:li:person:MyLogin',
114+
'author' => 'urn:li:person:AccountId',
114115
]);
115116

116117
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use (
@@ -121,7 +122,7 @@ public function testSendWithOptions()
121122

122123
return $response;
123124
});
124-
$transport = $this->getTransport($client);
125+
$transport = $this->createTransport($client);
125126

126127
$transport->send(new ChatMessage($message));
127128
}
@@ -145,19 +146,19 @@ public function testSendWithNotification()
145146

146147
$expectedBody = json_encode([
147148
'specificContent' => [
148-
'com.linkedin.ugc.ShareContent' => [
149-
'shareCommentary' => [
150-
'attributes' => [],
151-
'text' => 'testMessage',
152-
],
153-
'shareMediaCategory' => 'NONE',
154-
],
149+
'com.linkedin.ugc.ShareContent' => [
150+
'shareCommentary' => [
151+
'attributes' => [],
152+
'text' => 'testMessage',
155153
],
156-
'visibility' => [
157-
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
154+
'shareMediaCategory' => 'NONE',
158155
],
156+
],
157+
'visibility' => [
158+
'com.linkedin.ugc.MemberNetworkVisibility' => 'PUBLIC',
159+
],
159160
'lifecycleState' => 'PUBLISHED',
160-
'author' => 'urn:li:person:MyLogin',
161+
'author' => 'urn:li:person:AccountId',
161162
]);
162163

163164
$client = new MockHttpClient(function (string $method, string $url, array $options = []) use (
@@ -169,7 +170,7 @@ public function testSendWithNotification()
169170
return $response;
170171
});
171172

172-
$transport = $this->getTransport($client);
173+
$transport = $this->createTransport($client);
173174

174175
$transport->send($chatMessage);
175176
}
@@ -182,17 +183,13 @@ public function testSendWithInvalidOptions()
182183
return $this->createMock(ResponseInterface::class);
183184
});
184185

185-
$transport = $this->getTransport($client);
186+
$transport = $this->createTransport($client);
186187

187188
$transport->send(new ChatMessage('testMessage', $this->createMock(MessageOptionsInterface::class)));
188189
}
189190

190-
public function getTransport($client = null)
191+
private function createTransport(?HttpClientInterface $client = null): LinkedInTransport
191192
{
192-
return (new LinkedInTransport(
193-
'MyToken',
194-
'MyLogin',
195-
$client ?? $this->createMock(HttpClientInterface::class)
196-
))->setHost('host.test');
193+
return (new LinkedInTransport('AuthToken', 'AccountId', $client ?? $this->createMock(HttpClientInterface::class)))->setHost('host.test');
197194
}
198195
}

0 commit comments

Comments
 (0)