Completed
Push — master ( 87a00d...80388d )
by Maxence
02:18
created

CirclesTest::testRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 3
nc 4
nop 4
1
<?php declare(strict_types=1);
2
3
4
/**
5
 * Circles - Bring cloud-users closer together.
6
 *
7
 * This file is licensed under the Affero General Public License version 3 or
8
 * later. See the COPYING file.
9
 *
10
 * @author Maxence Lange <[email protected]>
11
 * @copyright 2017
12
 * @license GNU AGPL version 3 or any later version
13
 *
14
 * This program is free software: you can redistribute it and/or modify
15
 * it under the terms of the GNU Affero General Public License as
16
 * published by the Free Software Foundation, either version 3 of the
17
 * License, or (at your option) any later version.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
26
 *
27
 */
28
29
30
namespace OCA\Circles\Command;
31
32
use daita\MySmallPhpTools\Exceptions\RequestNetworkException;
33
use daita\MySmallPhpTools\Model\Nextcloud\NC19Request;
34
use daita\MySmallPhpTools\Model\Request;
35
use daita\MySmallPhpTools\Traits\Nextcloud\TNC19Request;
36
use daita\MySmallPhpTools\Traits\TArrayTools;
37
use Exception;
38
use OC\Core\Command\Base;
39
use OCA\Circles\Model\GlobalScale\GSEvent;
40
use OCA\Circles\Service\ConfigService;
41
use OCA\Circles\Service\GlobalScaleService;
42
use OCA\Circles\Service\GSUpstreamService;
43
use OCP\IL10N;
44
use OCP\IURLGenerator;
45
use Symfony\Component\Console\Input\InputInterface;
46
use Symfony\Component\Console\Input\InputOption;
47
use Symfony\Component\Console\Output\OutputInterface;
48
49
50
/**
51
 * Class CirclesList
52
 *
53
 * @package OCA\Circles\Command
54
 */
55
class CirclesTest extends Base {
56
57
58
	use TArrayTools;
59
	use TNC19Request;
60
61
62
	/** @var IL10N */
63
	private $l10n;
64
65
	/** @var IURLGenerator */
66
	private $urlGenerator;
67
68
	/** @var GlobalScaleService */
69
	private $globalScaleService;
70
71
	/** @var GSUpstreamService */
72
	private $gsUpstreamService;
73
74
	/** @var ConfigService */
75
	private $configService;
76
77
78
	/** @var int */
79
	private $delay = 5;
80
81
82
	/**
83
	 * CirclesList constructor.
84
	 *
85
	 * @param IL10N $l10n
86
	 * @param IURLGenerator $urlGenerator
87
	 * @param GlobalScaleService $globalScaleService
88
	 * @param GSUpstreamService $gsUpstreamService
89
	 * @param ConfigService $configService
90
	 */
91
	public function __construct(
92
		IL10N $l10n, IURLGenerator $urlGenerator, GlobalScaleService $globalScaleService,
93
		GSUpstreamService $gsUpstreamService, ConfigService $configService
94
	) {
95
		parent::__construct();
96
97
		$this->l10n = $l10n;
98
		$this->urlGenerator = $urlGenerator;
99
		$this->gsUpstreamService = $gsUpstreamService;
100
		$this->globalScaleService = $globalScaleService;
101
		$this->configService = $configService;
102
	}
103
104
105
	protected function configure() {
106
		parent::configure();
107
		$this->setName('circles:test')
108
			 ->setDescription('testing some features')
109
			 ->addOption('delay', 'd', InputOption::VALUE_REQUIRED, 'delay before checking result')
110
			 ->addOption('url', '', InputOption::VALUE_REQUIRED, 'specify a source url', '');
111
	}
112
113
114
	/**
115
	 * @param InputInterface $input
116
	 * @param OutputInterface $output
117
	 *
118
	 * @return int
119
	 * @throws Exception
120
	 */
121
	protected function execute(InputInterface $input, OutputInterface $output): int {
122
		if ($input->getOption('delay')) {
123
			$this->delay = (int)$input->getOption('delay');
124
		}
125
126
		$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');
127
		$this->configService->setAppValue(ConfigService::TEST_NC_BASE, $input->getOption('url'));
128
129
		if (!$this->testRequest($output, 'GET', 'core.CSRFToken.index')) {
130
			return 0;
131
		}
132
133
		if (!$this->testRequest(
134
			$output, 'POST', 'circles.GlobalScale.asyncBroadcast',
135
			['token' => 'test-dummy-token']
136
		)) {
137
			return 0;
138
		}
139
140
		$test = new GSEvent(GSEvent::TEST, true, true);
141
		$test->setAsync(true);
142
		$token = $this->gsUpstreamService->newEvent($test);
143
144
		$output->writeln('- Async request is sent, now waiting ' . $this->delay . ' seconds');
145
		sleep($this->delay);
146
		$output->writeln('- Pause is over, checking results for ' . $token);
147
148
		$wrappers = $this->gsUpstreamService->getEventsByToken($token);
149
150
		$result = [];
151
		$instances = array_merge($this->globalScaleService->getInstances(true));
152
		foreach ($wrappers as $wrapper) {
153
			$result[$wrapper->getInstance()] = $wrapper->getEvent();
154
		}
155
156
		foreach ($instances as $instance) {
157
			$output->write($instance . ' ');
158
			if (array_key_exists($instance, $result)
159
				&& $result[$instance]->getResult()
160
									 ->gInt('status') === 1) {
161
				$output->writeln('<info>ok</info>');
162
			} else {
163
				$output->writeln('<error>fail</error>');
164
			}
165
		}
166
167
		$this->configService->setAppValue(ConfigService::TEST_NC_BASE, '');
168
169
		return 0;
170
	}
171
172
173
	/**
174
	 * @param OutputInterface $o
175
	 * @param string $type
176
	 * @param string $route
177
	 * @param array $args
178
	 *
179
	 * @return bool
180
	 * @throws RequestNetworkException
181
	 */
182
	private function testRequest(OutputInterface $o, string $type, string $route, array $args = []): bool {
183
		$request = new NC19Request('', Request::type($type));
184
		$this->configService->configureRequest($request, $route, $args);
185
186
		$o->write('- ' . $type . ' request on ' . $request->getCompleteUrl() . ': ');
187
		$this->doRequest($request);
188
189
		$color = 'error';
190
		if ($request->getResultCode() === 200) {
191
			$color = 'info';
192
		}
193
		$o->writeln('<' . $color . '>' . $request->getResultCode() . '</' . $color . '>');
194
195
		if ($request->getResultCode() === 200) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return $request->getResultCode() === 200;.
Loading history...
196
			return true;
197
		}
198
199
		return false;
200
	}
201
202
}
203
204