Skip to content
This repository was archived by the owner on Jan 31, 2020. It is now read-only.

Commit 333196a

Browse files
committed
Simplify SecurityAdvisory, do not expect or use passed-in instance of checker as it accomplishes nothing, fix CS.
1 parent 4d80b70 commit 333196a

File tree

3 files changed

+41
-31
lines changed

3 files changed

+41
-31
lines changed

src/ZendDiagnostics/Check/SecurityAdvisory.php

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use ZendDiagnostics\Result\Warning;
1313

1414
/**
15-
* Checks installed dependencies against the SensioLabs Security Advisory database.
15+
* Checks installed composer dependencies against the SensioLabs Security Advisory database.
1616
*/
1717
class SecurityAdvisory extends AbstractCheck
1818
{
@@ -27,26 +27,21 @@ class SecurityAdvisory extends AbstractCheck
2727
protected $securityChecker;
2828

2929
/**
30-
* @param SecurityChecker|null $securityChecker An instance of SecurityChecker
31-
* @param string $lockFilePath Path to composer.lock
32-
* @throws \InvalidArgumentException
30+
* @param string $lockFilePath Path to composer.lock
31+
* @throws InvalidArgumentException
3332
*/
34-
public function __construct(SecurityChecker $securityChecker = null, $lockFilePath = null)
33+
public function __construct($lockFilePath = null)
3534
{
36-
if(!$securityChecker) {
37-
if(!class_exists('SensioLabs\Security\SecurityChecker')) {
38-
throw new InvalidArgumentException(sprintf(
39-
'Unable to find "%s" class. Please install "%s" library to use this Check.',
40-
'SensioLabs\Security\SecurityChecker',
41-
'sensiolabs/security-checker'
42-
));
43-
}
44-
45-
$securityChecker = new SecurityChecker();
35+
if (!class_exists('SensioLabs\Security\SecurityChecker')) {
36+
throw new InvalidArgumentException(sprintf(
37+
'Unable to find "%s" class. Please install "%s" library to use this Check.',
38+
'SensioLabs\Security\SecurityChecker',
39+
'sensiolabs/security-checker'
40+
));
4641
}
4742

48-
if(!$lockFilePath) {
49-
if(!file_exists('composer.lock')) {
43+
if (!$lockFilePath) {
44+
if (!file_exists('composer.lock')) {
5045
throw new InvalidArgumentException(
5146
'You have not provided lock file path and there is no "composer.lock" file in current directory.'
5247
);
@@ -60,8 +55,8 @@ public function __construct(SecurityChecker $securityChecker = null, $lockFilePa
6055
));
6156
}
6257

63-
$this->lockFilePath = $lockFilePath;
64-
$this->securityChecker = $securityChecker;
58+
$this->lockFilePath = $lockFilePath;
59+
$this->securityChecker = new SecurityChecker();
6560
}
6661

6762
public function check()

tests/ZendDiagnosticsTest/ChecksTest.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
use ZendDiagnostics\Check\ExtensionLoaded;
1212
use ZendDiagnostics\Check\PhpVersion;
1313
use ZendDiagnostics\Check\ProcessRunning;
14-
use ZendDiagnostics\Check\SecurityAdvisory;
1514
use ZendDiagnostics\Check\StreamWrapperExists;
1615
use ZendDiagnostics\Result\Success;
16+
use ZendDiagnosticsTest\TestAsset\Check\SecurityAdvisory;
1717
use ZendDiagnosticsTest\TestAsset\Check\AlwaysSuccess;
1818

1919
class ChecksTest extends \PHPUnit_Framework_TestCase
@@ -416,33 +416,28 @@ public function testSecurityAdvisory()
416416
}
417417

418418
$secureComposerLock = __DIR__ . '/TestAsset/secure-composer.lock';
419-
$checker = new SecurityChecker();
420-
$check = new SecurityAdvisory($checker, $secureComposerLock);
419+
$check = new SecurityAdvisory($secureComposerLock);
421420
$result = $check->check();
422421
$this->assertNotInstanceOf('ZendDiagnostics\Result\Failure', $result);
423422

424423
// check against non-existent lock file
425-
$checker = new SecurityChecker();
426-
$check = new SecurityAdvisory($checker, __DIR__ . '/improbable-lock-file-99999999999.lock');
424+
$check = new SecurityAdvisory(__DIR__ . '/improbable-lock-file-99999999999.lock');
427425
$result = $check->check();
428426
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result);
429427

430428
// check against unreadable lock file
431429
$tmpDir = sys_get_temp_dir();
432430
if (!is_dir($tmpDir) || !is_writable($tmpDir)) {
433431
$this->markTestSkipped('Cannot access writable system temp dir to perform the test... ');
434-
435432
return;
436433
}
437434
$unreadableFile = $tmpDir . '/composer.' . uniqid('', true) . '.lock';
438435
if (!file_put_contents($unreadableFile, 'foo') || !chmod($unreadableFile, 0000)) {
439436
$this->markTestSkipped('Cannot create temporary file in system temp dir to perform the test... ');
440-
441437
return;
442438
}
443439

444-
$checker = new SecurityChecker();
445-
$check = new SecurityAdvisory($checker, $unreadableFile);
440+
$check = new SecurityAdvisory($unreadableFile);
446441
$result = $check->check();
447442
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result);
448443

@@ -463,7 +458,8 @@ public function testSecurityAdvisoryFailure()
463458
->with($this->equalTo($secureComposerLock))
464459
->will($this->returnValue('[{"a":1},{"b":2},{"c":3}]'));
465460

466-
$check = new SecurityAdvisory($checker, $secureComposerLock);
461+
$check = new SecurityAdvisory($secureComposerLock);
462+
$check->setSecurityChecker($checker);
467463
$result = $check->check();
468464
$this->assertInstanceOf('ZendDiagnostics\Result\Failure', $result);
469465
}
@@ -479,7 +475,8 @@ public function testSecurityAdvisoryInvalidServerResponse()
479475
->method('check')
480476
->with($this->equalTo($secureComposerLock))
481477
->will($this->returnValue('404 error'));
482-
$check = new SecurityAdvisory($checker, $secureComposerLock);
478+
$check = new SecurityAdvisory($secureComposerLock);
479+
$check->setSecurityChecker($checker);
483480
$result = $check->check();
484481
$this->assertInstanceOf('ZendDiagnostics\Result\Warning', $result);
485482

@@ -495,7 +492,8 @@ public function testSecurityAdvisoryCheckerException()
495492
->method('check')
496493
->with($this->equalTo($secureComposerLock))
497494
->will($this->throwException(new Exception));
498-
$check = new SecurityAdvisory($checker, $secureComposerLock);
495+
$check = new SecurityAdvisory($secureComposerLock);
496+
$check->setSecurityChecker($checker);
499497
$result = $check->check();
500498
$this->assertInstanceOf('ZendDiagnostics\Result\Warning', $result);
501499
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
namespace ZendDiagnosticsTest\TestAsset\Check;
4+
5+
use SensioLabs\Security\SecurityChecker;
6+
use ZendDiagnostics\Check\SecurityAdvisory as BaseCheck;
7+
8+
class SecurityAdvisory extends BaseCheck
9+
{
10+
/**
11+
* @param SecurityChecker $securityChecker
12+
*/
13+
public function setSecurityChecker(SecurityChecker $securityChecker)
14+
{
15+
$this->securityChecker = $securityChecker;
16+
}
17+
}

0 commit comments

Comments
 (0)