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 | 'health', |
||
18 | 'broadcasting', |
||
19 | 'cache', |
||
20 | 'database', |
||
21 | 'docusign', |
||
22 | 'filesystem', |
||
23 | 'framework', |
||
24 | 'http', |
||
25 | 'https', |
||
26 | 'laravelservices', |
||
27 | 'localstorage', |
||
28 | 'mail', |
||
29 | 'mysql', |
||
30 | 'newrelicdeamon', |
||
31 | 'nginxserver', |
||
32 | 'php', |
||
33 | 'postgresqlserver', |
||
34 | 'queue', |
||
35 | 'queueworkers', |
||
36 | 'rebootrequired', |
||
37 | 'redis', |
||
38 | 'redisserver', |
||
39 | 's3', |
||
40 | 'serverload', |
||
41 | 'serveruptime', |
||
42 | 'sshd', |
||
43 | 'supervisor', |
||
44 | ]; |
||
45 | |||
46 | const RESOURCES_FAILING = [ |
||
47 | 'Health', |
||
48 | 'Broadcasting', |
||
49 | 'Database', |
||
50 | 'DocuSign', |
||
51 | 'Http', |
||
52 | 'Https', |
||
53 | 'NewrelicDeamon', |
||
54 | 'Redis', |
||
55 | 'S3', |
||
56 | 'NginxServer', |
||
57 | 'Php', |
||
58 | 'PostgreSqlServer', |
||
59 | 'ServerLoad', |
||
60 | ]; |
||
61 | |||
62 | /** |
||
63 | * @var \PragmaRX\Health\Service |
||
64 | */ |
||
65 | private $service; |
||
66 | |||
67 | /** |
||
68 | * @var \Illuminate\Support\Collection |
||
69 | */ |
||
70 | private $resources; |
||
71 | |||
72 | /** |
||
73 | * @param bool $force |
||
74 | * @return \Illuminate\Support\Collection |
||
75 | */ |
||
76 | private function getResources($force = false) |
||
77 | { |
||
78 | if ($force || !$this->resources) { |
||
79 | $this->resources = $this->service->checkResources($force); |
||
80 | } |
||
81 | |||
82 | return $this->resources; |
||
83 | } |
||
84 | |||
85 | /** |
||
86 | * Define environment setup. |
||
87 | * |
||
88 | * @param \Illuminate\Foundation\Application $app |
||
89 | * @return void |
||
90 | */ |
||
91 | protected function getEnvironmentSetUp($app) |
||
92 | { |
||
93 | $this->app = $app; |
||
94 | |||
95 | $this->app['config']->set( |
||
96 | 'health.resources_location.path', |
||
97 | package_resources_dir() |
||
98 | ); |
||
99 | } |
||
100 | |||
101 | public function setUp() |
||
102 | { |
||
103 | parent::setUp(); |
||
104 | |||
105 | $this->service = app('pragmarx.health'); |
||
106 | } |
||
107 | |||
108 | public function testResourcesWhereChecked() |
||
109 | { |
||
110 | $this->assertCheckedResources($this->getResources()); |
||
111 | } |
||
112 | |||
113 | public function testCacheFlush() |
||
114 | { |
||
115 | $this->assertCheckedResources($this->getResources(true)); |
||
116 | } |
||
117 | |||
118 | public function testLoadArray() |
||
119 | { |
||
120 | $this->app['config']->set( |
||
121 | 'health.resources_location.type', |
||
122 | \PragmaRX\Health\Support\Constants::RESOURCES_TYPE_ARRAY |
||
123 | ); |
||
124 | |||
125 | $this->assertCheckedResources($this->getResources(true)); |
||
126 | } |
||
127 | |||
128 | public function testLoadFiles() |
||
129 | { |
||
130 | $this->app['config']->set( |
||
131 | 'health.resources_location.type', |
||
132 | \PragmaRX\Health\Support\Constants::RESOURCES_TYPE_FILES |
||
133 | ); |
||
134 | |||
135 | $this->assertCheckedResources($this->getResources(true)); |
||
136 | } |
||
137 | |||
138 | public function testUnsorted() |
||
139 | { |
||
140 | $this->app['config']->set('health.sort_by', null); |
||
141 | |||
142 | $this->assertCheckedResources($this->getResources(true)); |
||
143 | } |
||
144 | |||
145 | public function testInvalidEnabledResources() |
||
146 | { |
||
147 | $this->expectException(\DomainException::class); |
||
148 | |||
149 | $this->app['config']->set('health.resources.enabled', 'invalid'); |
||
150 | |||
151 | (new ResourceLoader(new Yaml()))->load(); |
||
152 | |||
153 | $this->getResources(true); |
||
154 | } |
||
155 | |||
156 | public function testInvalidLoadOneResource() |
||
157 | { |
||
158 | $this->app['config']->set('health.resources.enabled', ['Database']); |
||
159 | |||
160 | $resource = (new ResourceLoader(new Yaml()))->load(); |
||
161 | |||
162 | $this->assertTrue($resource->first()['name'] == 'Database'); |
||
163 | } |
||
164 | |||
165 | public function assertCheckedResources($resources) |
||
166 | { |
||
167 | $healthCount = $resources->reduce(function ($carry, $item) { |
||
168 | return $carry + (isset($item['health']['healthy']) ? 1 : 0); |
||
169 | }, 0); |
||
170 | |||
171 | $this->assertEquals(count(static::ALL_RESOURCES), $healthCount); |
||
172 | |||
173 | $failing = $resources->filter(function ($item) { |
||
174 | return $item['health']['healthy']; |
||
175 | }); |
||
176 | |||
177 | $this->assertGreaterThanOrEqual( |
||
178 | static::RESOURCES_HEALTHY_EVERYWHERE, |
||
179 | $failing->count() |
||
180 | ); |
||
181 | } |
||
182 | |||
183 | public function testInstantiation() |
||
184 | { |
||
185 | $this->assertInstanceOf(Collection::class, $this->getResources()); |
||
186 | } |
||
187 | |||
188 | public function testConfigWasLoadedProperly() |
||
189 | { |
||
190 | $resources = $this->getResources(); |
||
191 | |||
192 | $this->assertEquals( |
||
193 | $resources['Health']['error_message'], |
||
194 | 'At least one resource failed the health check.' |
||
195 | ); |
||
196 | } |
||
197 | |||
198 | public function testResourcesHasTheCorrectCount() |
||
199 | { |
||
200 | $this->assertCount( |
||
201 | count(static::ALL_RESOURCES), |
||
202 | $this->getResources()->toArray() |
||
203 | ); |
||
204 | } |
||
205 | |||
206 | public function testResourcesItemsMatchConfig() |
||
207 | { |
||
208 | $this->assertEquals( |
||
209 | collect(static::ALL_RESOURCES) |
||
210 | ->sort() |
||
211 | ->values() |
||
212 | ->toArray(), |
||
213 | $this->getResources() |
||
214 | ->keys() |
||
215 | ->map(function ($value) { |
||
216 | return strtolower($value); |
||
217 | }) |
||
218 | ->sort() |
||
219 | ->values() |
||
220 | ->toArray() |
||
221 | ); |
||
222 | } |
||
223 | |||
224 | public function testArtisanCommands() |
||
225 | { |
||
226 | $commands = ['panel', 'check', 'export', 'publish']; |
||
227 | |||
228 | foreach ($commands as $command) { |
||
229 | (new Commands($this->service))->$command(); |
||
230 | } |
||
231 | |||
232 | $this->assertFalse(!true); |
||
233 | } |
||
234 | |||
235 | public function testController() |
||
236 | { |
||
237 | $controller = new HealthController($this->service); |
||
238 | |||
239 | $this->assertCheckedResources( |
||
240 | collect(json_decode($controller->check()->getContent(), true)) |
||
241 | ); |
||
242 | |||
243 | foreach ($this->getResources() as $key => $resource) { |
||
244 | $this->assertEquals($controller->resource($key)['name'], $key); |
||
0 ignored issues
–
show
|
|||
245 | } |
||
246 | |||
247 | $string = $controller->string()->getContent(); |
||
248 | |||
249 | $this->assertTrue( |
||
250 | strpos($string, config('health.string.ok') . '-') !== false |
||
251 | ); |
||
252 | |||
253 | $this->assertTrue( |
||
254 | strpos($string, config('health.string.fail') . '-') !== false |
||
255 | ); |
||
256 | |||
257 | $this->assertTrue( |
||
258 | strpos( |
||
259 | $controller->panel()->getContent(), |
||
260 | '<title>' . config('health.title') . '</title>' |
||
261 | ) !== false |
||
262 | ); |
||
263 | } |
||
264 | } |
||
265 |
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.