Test Failed
Push — master ( 892598...558941 )
by Antonio Carlos
11:17
created

tests/PhpUnit/Service/ServiceTest.php (2 issues)

Upgrade to new PHP Analysis Engine

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
        'Http',
31
        'Https',
32
        'LaravelServices',
33
        'Latency',
34
        'LocalStorage',
35
        'Mail',
36
        'MailgunConnectable',
37
        'MemcachedConnectable',
38
        'MigrationsUpToDate',
39
        'MySql',
40
        'MySqlConnectable',
41
        'NewrelicDeamon',
42
        'NginxServer',
43
        'PackagesUpToDate',
44
        'Php',
45
        'PostgreSqlConnectable',
46
        'PostgreSqlServer',
47
        'Queue',
48
        'QueueWorkers',
49
        'RebootRequired',
50
        'Redis',
51
        'RedisConnectable',
52
        'RedisServer',
53
        'RoutesCached',
54
        'S3',
55
        'SecurityChecker',
56
        'ServerLoad',
57
        'ServerUptime',
58
        'Sshd',
59
        'Supervisor',
60
    ];
61
62
    const RESOURCES_HEALTHY = [
63
        'Cache',
64
        'Debug Mode',
65
        'Directory Permissions',
66
        'Disk Space',
67
        'Filesystem',
68
        'Framework',
69
        'LaravelServices',
70
        'LocalStorage',
71
        'Mail',
72
        'Mailgun Connectable',
73
        'Memcached Connectable',
74
        'MySql',
75
        'MySQL Connectable',
76
        'Packages up to date',
77
        'Php',
78
        'PostgreSQL Connectable',
79
        'Queue',
80
        'QueueWorkers',
81
        'RebootRequired',
82
        'Redis',
83
        'Redis Connectable',
84
        'RedisServer',
85
        'ServerUptime',
86
        'Supervisor',
87
    ];
88
89
    const RESOURCES_FAILING = [
90
        'AppKey',
91
        'Broadcasting',
92
        'Cache',
93
        'ConfigurationCached',
94
        'Database',
95
        'DebugMode',
96
        'DirectoryPermissions',
97
        'DiskSpace',
98
        'DocuSign',
99
        'ElasticsearchConnectable',
100
        'EnvExists',
101
        'Filesystem',
102
        'Framework',
103
        'Http',
104
        'Https',
105
        'LaravelServices',
106
        'Latency',
107
        'LocalStorage',
108
        'Mail',
109
        'MailgunConnectable',
110
        'MemcachedConnectable',
111
        'MigrationsUpToDate',
112
        'MySql',
113
        'MySqlConnectable',
114
        'NewrelicDeamon',
115
        'NginxServer',
116
        'PackagesUpToDate',
117
        'Php',
118
        'PostgreSqlConnectable',
119
        'PostgreSqlServer',
120
        'Queue',
121
        'QueueWorkers',
122
        'RebootRequired',
123
        'Redis',
124
        'RedisConnectable',
125
        'RedisServer',
126
        'RoutesCached',
127
        'S3',
128
        'SecurityChecker',
129
        'ServerLoad',
130
        'ServerUptime',
131
        'Sshd',
132
        'Supervisor',
133
    ];
134
135
    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';
136
137
    /**
138
     * @var \PragmaRX\Health\Service
139
     */
140
    private $service;
141
142
    /**
143
     * @var \Illuminate\Support\Collection
144
     */
145
    private $resources;
146
147
    /**
148
     * @param bool $force
149
     * @return \Illuminate\Support\Collection
150
     */
151
    private function getResources($force = false)
152
    {
153
        if ($force || !$this->resources) {
154
            $this->resources = $this->service->checkResources($force);
155
        }
156
157
        return $this->resources;
158
    }
159
160
    /**
161
     * Define environment setup.
162
     *
163
     * @param  \Illuminate\Foundation\Application  $app
164
     * @return void
165
     */
166
    protected function getEnvironmentSetUp($app)
167
    {
168
        $this->app = $app;
169
170
        $this->app['config']->set(
171
            'health.resources_location.path',
172
            package_resources_dir()
173
        );
174
    }
175
176
    public function setUp()
177
    {
178
        parent::setUp();
179
180
        $this->service = app('pragmarx.health');
181
    }
182
183
    private function sortChars($string)
184
    {
185
        $stringParts = str_split($string);
186
187
        sort($stringParts);
188
189
        return implode('', $stringParts);
190
    }
191
192
    public function testResourcesWhereChecked()
193
    {
194
        $this->assertCheckedResources($this->getResources());
195
    }
196
197
    public function testCacheFlush()
198
    {
199
        $this->assertCheckedResources($this->getResources(true));
200
    }
201
202
    public function testUnsorted()
203
    {
204
        $this->app['config']->set('health.sort_by', null);
205
206
        $this->assertCheckedResources($this->getResources(true));
207
    }
208
209
    public function testInvalidEnabledResources()
210
    {
211
        $this->expectException(\DomainException::class);
212
213
        $this->app['config']->set('health.resources.enabled', 'invalid');
214
215
        (new ResourceLoader(new Yaml()))->load();
216
217
        $this->getResources(true);
218
    }
219
220
    public function testInvalidLoadOneResource()
221
    {
222
        $this->app['config']->set('health.resources.enabled', ['Database']);
223
224
        $resource = (new ResourceLoader(new Yaml()))->load();
225
226
        $this->assertTrue($resource->first()['name'] == 'Database');
227
    }
228
229
    public function assertCheckedResources($resources)
230
    {
231
        $healthCount = $resources->reduce(function ($carry, $resource) {
232
            return $carry + ($resource->isHealthy() ? 1 : 0);
233
        }, 0);
234
235
        $this->assertEquals(count(static::RESOURCES_HEALTHY), $healthCount);
236
237
        $failing = $resources->filter(function ($resource) {
238
            return $resource->isHealthy();
239
        });
240
241
        $this->assertGreaterThanOrEqual(
242
            static::RESOURCES_HEALTHY_EVERYWHERE,
243
            $failing->count()
244
        );
245
    }
246
247
    public function testInstantiation()
248
    {
249
        $this->assertInstanceOf(Collection::class, $this->getResources());
250
    }
251
252
    public function testResourcesHasTheCorrectCount()
253
    {
254
        $this->assertCount(
255
            count(static::ALL_RESOURCES),
256
            $this->getResources()->toArray()
0 ignored issues
show
$this->getResources()->toArray() is of type array, but the function expects a object<Countable>|object...nit\Framework\iterable>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
257
        );
258
    }
259
260
    public function testResourcesItemsMatchConfig()
261
    {
262
        $this->assertEquals(
263
            collect(static::ALL_RESOURCES)->map(function ($value) {
264
                return strtolower($value);
265
            })
266
                ->sort()
267
                ->values()
268
                ->toArray(),
269
            $this->getResources()
270
                ->keys()
271
                ->map(function ($value) {
272
                    return strtolower($value);
273
                })
274
                ->sort()
275
                ->values()
276
                ->toArray()
277
        );
278
    }
279
280
    public function testArtisanCommands()
281
    {
282
        $commands = ['panel', 'check'];
283
284
        foreach ($commands as $command) {
285
            (new Commands($this->service))->$command();
286
        }
287
288
        $this->assertFalse(!true);
289
    }
290
291
    public function testController()
292
    {
293
        $controller = new HealthController($this->service);
294
295
        $this->assertEquals(
296
            collect(json_decode($controller->check()->getContent(), true))->count(),
0 ignored issues
show
The method getContent does only exist in Illuminate\Http\Response, but not in Illuminate\Contracts\Routing\ResponseFactory.

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:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
297
            count(static::ALL_RESOURCES)
298
        );
299
300
        $this->assertTrue(starts_with($controller->panel()->getContent(), '<!DOCTYPE html>'));
301
302
        $this->assertEquals($this->sortChars($controller->string()->getContent()), $this->sortChars(static::RESOURCES_STRING));
303
304
        $this->assertTrue(count($controller->config()) > 10);
305
306
        $this->assertTrue($controller->getResource('app-key')->name == 'App Key');
307
308
        $this->assertTrue($controller->allResources()->count() == count(static::ALL_RESOURCES));
309
    }
310
}
311
312