1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Cli\Command; |
4
|
|
|
|
5
|
|
|
use Guzzle\Http\Client; |
6
|
|
|
use Magium\Cli\Command\Test\TestSkeleton; |
7
|
|
|
use Magium\InvalidConfigurationException; |
8
|
|
|
use Magium\NotFoundException; |
9
|
|
|
use Magium\Util\Api\ApiConfiguration; |
10
|
|
|
use Symfony\Component\Console\Command\Command; |
11
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
12
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
|
17
|
|
|
class ApiPing extends Command |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected function configure() |
21
|
|
|
{ |
22
|
|
|
$this->setName('api:ping'); |
23
|
|
|
$this->setDescription('Pings the API ping endpoint to ensure that the key works'); |
24
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
28
|
|
|
{ |
29
|
|
|
$test = new TestSkeleton(); |
30
|
|
|
$test->configureDi(); |
31
|
|
|
$api = $test->get('Magium\Util\Api\ApiConfiguration'); |
32
|
|
|
/* @var $api ApiConfiguration */ |
33
|
|
|
$api->setEnabled(true); // Gotta force this for this test |
34
|
|
|
|
35
|
|
|
$output->writeln('Sending un-authenticated payload...'); |
36
|
|
|
$client = new Client('http://' . $api->getApiHostname(). '/api/ping'); |
37
|
|
|
$request = $client->get(); |
38
|
|
|
$response = $request->send(); |
39
|
|
|
$output->writeln( |
40
|
|
|
'Checking for 200 status message... ' |
41
|
|
|
. ($response->getStatusCode() == '200'?'OK':'Failed') |
42
|
|
|
); |
43
|
|
|
$output->writeln( |
44
|
|
|
'Checking for application/json content type... ' |
45
|
|
|
. (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed') |
46
|
|
|
); |
47
|
|
|
$content = json_decode($response->getBody(), true); |
48
|
|
|
$output->writeln( |
49
|
|
|
'Checking for successful response... ' |
50
|
|
|
. (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed') |
51
|
|
|
); |
52
|
|
|
|
53
|
|
|
$output->writeln(''); |
54
|
|
|
$output->writeln('Attempting authenticated ping...'); |
55
|
|
|
|
56
|
|
|
$request = $test->get('Magium\Util\Api\Request'); |
57
|
|
|
/* @var $request \Magium\Util\Api\Request */ |
58
|
|
|
$response = $request->fetch('/api/ping-authed'); |
59
|
|
|
|
60
|
|
|
$output->writeln( |
61
|
|
|
'Checking for 200 status message... ' |
62
|
|
|
. ($response->getStatusCode() == '200'?'OK':'Failed') |
63
|
|
|
); |
64
|
|
|
$output->writeln( |
65
|
|
|
'Checking for application/json content type... ' |
66
|
|
|
. (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed') |
67
|
|
|
); |
68
|
|
|
$content = $request->getPayload($response); |
|
|
|
|
69
|
|
|
$output->writeln( |
70
|
|
|
'Checking for successful response... ' |
71
|
|
|
. (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed') |
72
|
|
|
); |
73
|
|
|
|
74
|
|
|
|
75
|
|
|
$output->writeln(''); |
76
|
|
|
$output->writeln('Attempting authenticated echo...'); |
77
|
|
|
$request = $test->get('Magium\Util\Api\Request'); |
78
|
|
|
/* @var $request \Magium\Util\Api\Request */ |
79
|
|
|
$response = $request->push('/api/echo-authed', ['message' => 'hello world']); |
80
|
|
|
$output->writeln( |
81
|
|
|
'Checking for 200 status message... ' |
82
|
|
|
. ($response->getStatusCode() == '200'?'OK':'Failed') |
83
|
|
|
); |
84
|
|
|
$output->writeln( |
85
|
|
|
'Checking for application/json content type... ' |
86
|
|
|
. (stripos($response->getContentType(), 'application/json') !== false?'OK':'Failed') |
87
|
|
|
); |
88
|
|
|
$content = $request->getPayload($response); |
|
|
|
|
89
|
|
|
$output->writeln( |
90
|
|
|
'Checking for successful response... ' |
91
|
|
|
. (is_array($content) && isset($content['status']) && $content['status'] === 'success'?'OK':'Failed') |
92
|
|
|
); |
93
|
|
|
$output->writeln( |
94
|
|
|
'Checking for matching echo message... ' |
95
|
|
|
. (is_array($content) && isset($content['message']) && $content['message'] === 'hello world'?'OK':'Failed') |
96
|
|
|
); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
} |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.