forked from TYPO3GmbH/site-intercept
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSlackServiceTest.php
More file actions
106 lines (81 loc) · 4.29 KB
/
Copy pathSlackServiceTest.php
File metadata and controls
106 lines (81 loc) · 4.29 KB
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
<?php
declare(strict_types=1);
/*
* This file is part of the package t3g/intercept.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/
namespace App\Tests\Functional\Service;
use App\Entity\DocumentationJar;
use App\Service\SlackService;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Psr7\Response;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
class SlackServiceTest extends KernelTestCase
{
public function testSendRepositoryDiscoveryMessageContainsExpectedPayload(): void
{
$capturedPayload = null;
$mockClient = $this->createMock(ClientInterface::class);
$mockClient
->expects(self::once())
->method('request')
->with(
'POST',
'http://localhost/slack',
self::callback(static function (array $options) use (&$capturedPayload): bool {
$capturedPayload = $options['json'] ?? null;
return true;
})
)
->willReturn(new Response());
self::getContainer()->set('guzzle.client.slack', $mockClient);
$jar = new DocumentationJar();
$jar->setVendor('acme');
$jar->setName('my-extension');
$jar->setPackageName('acme/my-extension');
$jar->setRepositoryUrl('https://github.com/acme/my-extension.git');
$jar->setTypeShort('p');
$jar->setTargetBranchDirectory('main');
$service = self::getContainer()->get(SlackService::class);
$service->sendRepositoryDiscoveryMessage($jar);
self::assertNotNull($capturedPayload, 'Slack message payload was not captured');
// Verify channel and identity
self::assertSame('#typo3-documentation', $capturedPayload['channel']);
self::assertSame('Intercept', $capturedPayload['username']);
$attachment = $capturedPayload['attachments'][0];
// Title should indicate awaiting approval
self::assertStringContainsString('awaiting approval', strtolower($attachment['title']));
// Title link must point to deployments admin
self::assertSame('https://localhost/admin/docs/deployments', $attachment['title_link']);
// Fallback must mention package and deployments URL
self::assertStringContainsString('acme/my-extension', $attachment['fallback']);
self::assertStringContainsString('localhost/admin/docs/deployments', $attachment['fallback']);
// Text must mention the package
self::assertStringContainsString('acme/my-extension', $attachment['text']);
// Text must explain what happens next
self::assertStringContainsString('maintainer', strtolower($attachment['text']));
self::assertStringContainsString('volunteer', strtolower($attachment['text']));
// Must contain the specific docs link for this package
self::assertStringContainsString('localhost/docs/p/acme/my-extension/main/en-us', $attachment['text']);
// Must link to deployments admin for maintainers
self::assertStringContainsString('localhost/admin/docs/deployments', $attachment['text']);
// Must link to webhook docs for extension authors
self::assertStringContainsString('localhost/docs/permalink/h2document:webhook', $attachment['text']);
// Footer must contain source link to SlackService.php on GitHub
self::assertStringContainsString('github.com/TYPO3GmbH/site-intercept', $attachment['footer']);
self::assertStringContainsString('SlackService.php', $attachment['footer']);
// Every link target must be a bare URL, `<{https://…}|label>` is rendered verbatim by Slack
foreach (['text', 'footer'] as $field) {
preg_match_all('/<([^|>]*)\|[^>]*>/', $attachment[$field], $links);
self::assertNotEmpty($links[1], sprintf('No Slack link found in attachment field "%s"', $field));
foreach ($links[1] as $target) {
self::assertMatchesRegularExpression('#^https?://\S+$#', $target);
}
}
// Must opt into mrkdwn rendering for the text field
self::assertContains('text', $attachment['mrkdwn_in']);
self::assertStringContainsString('<https://github.com/acme/my-extension.git|:git:', $attachment['text']);
}
}