These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | |||
3 | namespace PragmaRX\Health\Tests\PhpUnit\Service; |
||
4 | |||
5 | use PragmaRX\Health\Commands; |
||
6 | use PragmaRX\Yaml\Package\Yaml; |
||
7 | use Illuminate\Support\Collection; |
||
8 | use PragmaRX\Health\Support\ResourceLoader; |
||
9 | use PragmaRX\Health\Tests\PhpUnit\TestCase; |
||
10 | use PragmaRX\Health\Http\Controllers\Health as HealthController; |
||
11 | |||
12 | class ServiceTest extends TestCase |
||
13 | { |
||
14 | const RESOURCES_HEALTHY_EVERYWHERE = 8; |
||
15 | |||
16 | const ALL_RESOURCES = [ |
||
17 | 'AppKey', |
||
18 | 'Broadcasting', |
||
19 | 'Cache', |
||
20 | 'ConfigurationCached', |
||
21 | 'Database', |
||
22 | 'DebugMode', |
||
23 | 'DirectoryPermissions', |
||
24 | 'DiskSpace', |
||
25 | 'DocuSign', |
||
26 | 'ElasticsearchConnectable', |
||
27 | 'EnvExists', |
||
28 | 'Filesystem', |
||
29 | 'Framework', |
||
30 | 'Horizon', |
||
31 | 'Http', |
||
32 | 'Https', |
||
33 | 'LaravelServices', |
||
34 | 'Latency', |
||
35 | 'LocalStorage', |
||
36 | 'Mail', |
||
37 | 'MailgunConnectable', |
||
38 | 'MemcachedConnectable', |
||
39 | 'MigrationsUpToDate', |
||
40 | 'MySql', |
||
41 | 'MySqlConnectable', |
||
42 | 'NewrelicDeamon', |
||
43 | 'NginxServer', |
||
44 | 'PackagesUpToDate', |
||
45 | 'Php', |
||
46 | 'PostgreSqlConnectable', |
||
47 | 'PostgreSqlServer', |
||
48 | 'Queue', |
||
49 | 'QueueWorkers', |
||
50 | 'RebootRequired', |
||
51 | 'Redis', |
||
52 | 'RedisConnectable', |
||
53 | 'RedisServer', |
||
54 | 'RoutesCached', |
||
55 | 'S3', |
||
56 | 'SecurityChecker', |
||
57 | 'ServerLoad', |
||
58 | 'ServerUptime', |
||
59 | 'Sshd', |
||
60 | 'Supervisor', |
||
61 | ]; |
||
62 | |||
63 | const RESOURCES_HEALTHY = [ |
||
64 | 'Cache', |
||
65 | 'Debug Mode', |
||
66 | 'Directory Permissions', |
||
67 | 'Disk Space', |
||
68 | 'Filesystem', |
||
69 | 'Framework', |
||
70 | 'LaravelServices', |
||
71 | 'LocalStorage', |
||
72 | 'Mail', |
||
73 | 'Mailgun Connectable', |
||
74 | 'Memcached Connectable', |
||
75 | 'MySql', |
||
76 | 'MySQL Connectable', |
||
77 | 'Packages up to date', |
||
78 | 'PostgreSqlConnectable', |
||
79 | 'PostgreSqlServer', |
||
80 | 'Php', |
||
81 | 'Queue', |
||
82 | 'QueueWorkers', |
||
83 | 'RebootRequired', |
||
84 | 'Redis', |
||
85 | 'Redis Connectable', |
||
86 | 'RedisServer', |
||
87 | 'ServerUptime', |
||
88 | 'Supervisor', |
||
89 | ]; |
||
90 | |||
91 | const RESOURCES_FAILING = [ |
||
92 | 'AppKey', |
||
93 | 'Broadcasting', |
||
94 | 'Cache', |
||
95 | 'ConfigurationCached', |
||
96 | 'Database', |
||
97 | 'DebugMode', |
||
98 | 'DirectoryPermissions', |
||
99 | 'DiskSpace', |
||
100 | 'DocuSign', |
||
101 | 'ElasticsearchConnectable', |
||
102 | 'EnvExists', |
||
103 | 'Filesystem', |
||
104 | 'Framework', |
||
105 | 'Horizon', |
||
106 | 'Http', |
||
107 | 'Https', |
||
108 | 'LaravelServices', |
||
109 | 'Latency', |
||
110 | 'LocalStorage', |
||
111 | 'Mail', |
||
112 | 'MailgunConnectable', |
||
113 | 'MemcachedConnectable', |
||
114 | 'MigrationsUpToDate', |
||
115 | 'MySql', |
||
116 | 'MySqlConnectable', |
||
117 | 'NewrelicDeamon', |
||
118 | 'NginxServer', |
||
119 | 'PackagesUpToDate', |
||
120 | 'Php', |
||
121 | 'PostgreSqlConnectable', |
||
122 | 'PostgreSqlServer', |
||
123 | 'Queue', |
||
124 | 'QueueWorkers', |
||
125 | 'RebootRequired', |
||
126 | 'Redis', |
||
127 | 'RedisConnectable', |
||
128 | 'RedisServer', |
||
129 | 'RoutesCached', |
||
130 | 'S3', |
||
131 | 'SecurityChecker', |
||
132 | 'ServerLoad', |
||
133 | 'ServerUptime', |
||
134 | 'Sshd', |
||
135 | 'Supervisor', |
||
136 | ]; |
||
137 | |||
138 | const RESOURCES_STRING = 'appkeyFAIL-brdcFAIL-cshOK-cfgcchFAIL-dbFAIL-debugOK-dirpermOK-dskspcOK-dcsgnFAIL-redisconnFAIL-envexistsFAIL-flstmOK-frmwrkOK-httpFAIL-httpsFAIL-lvsOK-latencyFAIL-lclstrgOK-mlOK-redisconnOK-redisconnOK-debugFAIL-msqlOK-mysqlgrsqlsrvrconnOK-nwrlcdmnFAIL-ngnxsrvrFAIL-debugFAIL-pkgupdtdOK-phpOK-pstgrsqlsrvrconnOK-pstgrsqlsrvrFAIL-queueOK-qwrkrsOK-rbtrqrdOK-rdsOK-redisconnOK-rdssrvrOK-rtcchFAIL-s3FAIL-loadFAIL-uptmOK-sshdFAIL-sprvsrOK'; |
||
139 | |||
140 | private $service; |
||
141 | |||
142 | private $resources; |
||
143 | |||
144 | private function getResources($force = false) |
||
145 | { |
||
146 | if ($force || !$this->resources) { |
||
147 | $this->resources = $this->service->checkResources($force); |
||
148 | } |
||
149 | |||
150 | return $this->resources; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Define environment setup. |
||
155 | * |
||
156 | * @param \Illuminate\Foundation\Application $app |
||
157 | * @return void |
||
158 | */ |
||
159 | protected function getEnvironmentSetUp($app) |
||
160 | { |
||
161 | $this->app = $app; |
||
162 | |||
163 | $this->app['config']->set( |
||
164 | 'health.resources_location.path', |
||
165 | package_resources_dir() |
||
166 | ); |
||
167 | } |
||
168 | |||
169 | public function setUp() |
||
170 | { |
||
171 | parent::setUp(); |
||
172 | |||
173 | $this->service = app('pragmarx.health'); |
||
174 | } |
||
175 | |||
176 | private function sortChars($string) |
||
177 | { |
||
178 | $stringParts = str_split($string); |
||
179 | |||
180 | sort($stringParts); |
||
181 | |||
182 | return implode('', $stringParts); |
||
183 | } |
||
184 | |||
185 | public function testResourcesWhereChecked() |
||
186 | { |
||
187 | $this->assertCheckedResources($this->getResources()); |
||
188 | } |
||
189 | |||
190 | public function testCacheFlush() |
||
191 | { |
||
192 | $this->assertCheckedResources($this->getResources(true)); |
||
193 | } |
||
194 | |||
195 | public function testUnsorted() |
||
196 | { |
||
197 | $this->app['config']->set('health.sort_by', null); |
||
198 | |||
199 | $this->assertCheckedResources($this->getResources(true)); |
||
200 | } |
||
201 | |||
202 | public function testInvalidEnabledResources() |
||
203 | { |
||
204 | $this->expectException(\DomainException::class); |
||
205 | |||
206 | $this->app['config']->set('health.resources.enabled', 'invalid'); |
||
207 | |||
208 | (new ResourceLoader(new Yaml()))->load(); |
||
209 | |||
210 | $this->getResources(true); |
||
211 | } |
||
212 | |||
213 | public function testInvalidLoadOneResource() |
||
214 | { |
||
215 | $this->app['config']->set('health.resources.enabled', ['Database']); |
||
216 | |||
217 | $resource = (new ResourceLoader(new Yaml()))->load(); |
||
218 | |||
219 | $this->assertTrue($resource->first()['name'] == 'Database'); |
||
220 | } |
||
221 | |||
222 | public function assertCheckedResources($resources) |
||
223 | { |
||
224 | $healthCount = $resources->reduce(function ($carry, $resource) { |
||
225 | return $carry + ($resource->isHealthy() ? 1 : 0); |
||
226 | }, 0); |
||
227 | |||
228 | $this->assertGreaterThanOrEqual( |
||
229 | count(static::RESOURCES_HEALTHY), |
||
230 | $healthCount |
||
231 | ); |
||
232 | |||
233 | $failing = $resources->filter(function ($resource) { |
||
234 | return $resource->isHealthy(); |
||
235 | }); |
||
236 | |||
237 | $this->assertGreaterThanOrEqual( |
||
238 | static::RESOURCES_HEALTHY_EVERYWHERE, |
||
239 | $failing->count() |
||
240 | ); |
||
241 | } |
||
242 | |||
243 | public function testInstantiation() |
||
244 | { |
||
245 | $this->assertInstanceOf(Collection::class, $this->getResources()); |
||
246 | } |
||
247 | |||
248 | public function testResourcesHasTheCorrectCount() |
||
249 | { |
||
250 | $this->assertCount( |
||
251 | count(static::ALL_RESOURCES), |
||
252 | $this->getResources()->toArray() |
||
253 | ); |
||
254 | } |
||
255 | |||
256 | public function testResourcesItemsMatchConfig() |
||
257 | { |
||
258 | $this->assertEquals( |
||
259 | collect(static::ALL_RESOURCES) |
||
260 | ->map(function ($value) { |
||
261 | return strtolower($value); |
||
262 | }) |
||
263 | ->sort() |
||
264 | ->values() |
||
265 | ->toArray(), |
||
266 | $this->getResources() |
||
267 | ->keys() |
||
268 | ->map(function ($value) { |
||
269 | return strtolower($value); |
||
270 | }) |
||
271 | ->sort() |
||
272 | ->values() |
||
273 | ->toArray() |
||
274 | ); |
||
275 | } |
||
276 | |||
277 | public function testArtisanCommands() |
||
278 | { |
||
279 | $commands = ['panel', 'check']; |
||
280 | |||
281 | foreach ($commands as $command) { |
||
282 | (new Commands($this->service))->$command(); |
||
283 | } |
||
284 | |||
285 | $this->assertFalse(!true); |
||
286 | } |
||
287 | |||
288 | public function testController() |
||
289 | { |
||
290 | $controller = new HealthController($this->service); |
||
291 | |||
292 | $this->assertEquals( |
||
293 | collect( |
||
294 | json_decode($controller->check()->getContent(), true) |
||
0 ignored issues
–
show
|
|||
295 | )->count(), |
||
296 | count(static::ALL_RESOURCES) |
||
297 | ); |
||
298 | |||
299 | $this->assertTrue( |
||
300 | starts_with($controller->panel()->getContent(), '<!DOCTYPE html>') |
||
301 | ); |
||
302 | |||
303 | $this->assertTrue(count($controller->config()) > 10); |
||
304 | |||
305 | $this->assertTrue( |
||
306 | $controller->getResource('app-key')->name == 'App Key' |
||
307 | ); |
||
308 | |||
309 | $this->assertTrue( |
||
310 | $controller->allResources()->count() == count(static::ALL_RESOURCES) |
||
311 | ); |
||
312 | } |
||
313 | } |
||
314 |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: