|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the slack-cli package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Cas Leentfaar <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace CL\SlackCli\Command; |
|
13
|
|
|
|
|
14
|
|
|
use CL\Slack\Payload\ApiTestPayload; |
|
15
|
|
|
use CL\Slack\Payload\ApiTestPayloadResponse; |
|
16
|
|
|
use Symfony\Component\Console\Input\InputOption; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @author Cas Leentfaar <[email protected]> |
|
20
|
|
|
*/ |
|
21
|
|
|
class ApiTestCommand extends AbstractApiCommand |
|
22
|
|
|
{ |
|
23
|
|
|
/** |
|
24
|
|
|
* @var bool |
|
25
|
|
|
*/ |
|
26
|
|
|
private $expectError = false; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* {@inheritDoc} |
|
30
|
|
|
*/ |
|
31
|
1 |
|
protected function configure() |
|
32
|
|
|
{ |
|
33
|
1 |
|
parent::configure(); |
|
34
|
|
|
|
|
35
|
1 |
|
$this->setName('api:test'); |
|
36
|
1 |
|
$this->setDescription('Tests connecting with the Slack API using the token.'); |
|
37
|
1 |
|
$this->addOption('arguments', null, InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, 'Arguments to be tested in key:value format, such as "foo:bar"'); |
|
38
|
1 |
|
$this->addOption('error', null, InputOption::VALUE_REQUIRED, 'Optional error message to mock an error response from Slack with'); |
|
39
|
1 |
|
$this->setHelp(<<<EOT |
|
40
|
|
|
The <info>api:test</info> command lets you connect with the Slack API for testing purposes. |
|
41
|
|
|
|
|
42
|
|
|
Testing arguments returned by Slack |
|
43
|
|
|
<info>php bin/slack api.test --args="foo=bar&apple=pie"</info> |
|
44
|
|
|
|
|
45
|
|
|
Testing an error response |
|
46
|
|
|
<info>php bin/slack api.test --error="This is my error"</info> |
|
47
|
|
|
|
|
48
|
|
|
For more information about the related API method, check out the official documentation: |
|
49
|
|
|
<comment>https://api.slack.com/methods/api.test</comment> |
|
50
|
|
|
EOT |
|
51
|
1 |
|
); |
|
52
|
1 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return ApiTestPayload |
|
56
|
|
|
*/ |
|
57
|
1 |
|
protected function createPayload() |
|
58
|
|
|
{ |
|
59
|
1 |
|
$payload = new ApiTestPayload(); |
|
60
|
|
|
|
|
61
|
1 |
|
if ($this->input->getOption('arguments')) { |
|
62
|
1 |
|
$args = []; |
|
63
|
1 |
|
foreach ($this->input->getOption('arguments') as $keyValue) { |
|
64
|
1 |
|
list($key, $value) = explode(':', $keyValue); |
|
65
|
1 |
|
$args[$key] = $value; |
|
66
|
1 |
|
} |
|
67
|
|
|
|
|
68
|
1 |
|
$payload->replaceArguments($args); |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
1 |
|
if ($this->input->getOption('error')) { |
|
72
|
1 |
|
$this->expectError = true; |
|
73
|
1 |
|
$payload->setError($this->input->getOption('error')); |
|
74
|
1 |
|
} |
|
75
|
|
|
|
|
76
|
1 |
|
return $payload; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* {@inheritdoc} |
|
81
|
|
|
* |
|
82
|
|
|
* @param ApiTestPayloadResponse $payloadResponse |
|
83
|
|
|
*/ |
|
84
|
1 |
|
protected function handleResponse($payloadResponse) |
|
85
|
|
|
{ |
|
86
|
1 |
|
if ($this->expectError === true || $payloadResponse->isOk()) { |
|
87
|
1 |
|
$this->writeOk('Slack API seems to have responded correctly (no error expected, no error returned)'); |
|
88
|
1 |
|
$data = []; |
|
89
|
1 |
|
if (null !== $error = $payloadResponse->getError()) { |
|
90
|
1 |
|
$data['error'] = $error; |
|
91
|
1 |
|
} |
|
92
|
1 |
|
foreach ($payloadResponse->getArguments() as $key => $val) { |
|
93
|
1 |
|
if ($key == 'token') { |
|
94
|
|
|
continue; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
1 |
|
$data['args'][$key] = $val; |
|
98
|
1 |
|
} |
|
99
|
1 |
|
$this->renderKeyValueTable($data); |
|
100
|
|
|
|
|
101
|
|
|
// force 0 so any error tested here won't trigger a failure |
|
102
|
1 |
|
return 0; |
|
103
|
|
|
} else { |
|
104
|
1 |
|
$this->writeError(sprintf( |
|
105
|
1 |
|
'Slack API did not respond correctly (no error expected): %s', |
|
106
|
1 |
|
lcfirst($payloadResponse->getErrorExplanation()) |
|
107
|
1 |
|
)); |
|
108
|
|
|
|
|
109
|
1 |
|
return 1; |
|
110
|
|
|
} |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|