|
11 | 11 |
|
12 | 12 | namespace bitExpert\Phing\SecurityChecker; |
13 | 13 |
|
14 | | -use bitExpert\Phing\SecurityChecker\SecurityCheckerTask; |
15 | 14 | use PHPUnit\Framework\TestCase; |
16 | 15 | use SensioLabs\Security\Crawler\CrawlerInterface; |
17 | 16 | use SensioLabs\Security\SecurityChecker; |
@@ -43,23 +42,7 @@ protected function setUp() |
43 | 42 | { |
44 | 43 | parent::setUp(); |
45 | 44 |
|
46 | | - $this->crawler = $this->createMock(CrawlerInterface::class); |
47 | | - $this->checker = $this->createMock(SecurityChecker::class); |
48 | | - $this->checker->expects($this->any()) |
49 | | - ->method('check') |
50 | | - ->will($this->returnValue([])); |
51 | | - $this->checker->expects($this->any()) |
52 | | - ->method('getCrawler') |
53 | | - ->will($this->returnValue($this->crawler)); |
54 | | - |
55 | | - $this->checkerTask = $this->createPartialMock( |
56 | | - SecurityCheckerTask::class, |
57 | | - ['getSecurityChecker'] |
58 | | - ); |
59 | | - $this->checkerTask->expects($this->any()) |
60 | | - ->method('getSecurityChecker') |
61 | | - ->will($this->returnValue($this->checker)); |
62 | | - $this->checkerTask->setProject(new \Project()); |
| 45 | + $this->createMockObjects(); |
63 | 46 | } |
64 | 47 |
|
65 | 48 | /** |
@@ -107,17 +90,101 @@ public function endPointParameterShouldBePassedToSecurityCheckerWhenGiven() |
107 | 90 | $this->checkerTask->main(); |
108 | 91 | } |
109 | 92 |
|
| 93 | + /** |
| 94 | + * @test |
| 95 | + */ |
| 96 | + public function advisoriesIncludingLinkWillCallLogMethodFiveTimes() |
| 97 | + { |
| 98 | + $vulnerabilities = [ |
| 99 | + 'my/dependency' => [ |
| 100 | + 'version' => '1.0.0', |
| 101 | + 'advisories' => [ |
| 102 | + 0 => [ |
| 103 | + 'title' => 'Advisories title', |
| 104 | + 'cve' => 'CVE-2017-0001', |
| 105 | + 'link' => 'http://localhost' |
| 106 | + ] |
| 107 | + ] |
| 108 | + ] |
| 109 | + ]; |
| 110 | + $this->createMockObjects($vulnerabilities); |
| 111 | + |
| 112 | + $this->checkerTask->expects($this->exactly(5)) |
| 113 | + ->method('log'); |
| 114 | + |
| 115 | + $this->checkerTask->setLockfile(__FILE__); |
| 116 | + $this->checkerTask->setEndPoint('http://localhost'); |
| 117 | + $this->checkerTask->main(); |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @test |
| 122 | + */ |
| 123 | + public function advisoriesWithEmptyLinkWillCallLogMethodFourTimes() |
| 124 | + { |
| 125 | + $vulnerabilities = [ |
| 126 | + 'my/dependency' => [ |
| 127 | + 'version' => '1.0.0', |
| 128 | + 'advisories' => [ |
| 129 | + 0 => [ |
| 130 | + 'title' => 'Some title', |
| 131 | + 'cve' => 'CVE-2017-0001', |
| 132 | + 'link' => '' |
| 133 | + ] |
| 134 | + ] |
| 135 | + ] |
| 136 | + ]; |
| 137 | + $this->createMockObjects($vulnerabilities); |
| 138 | + |
| 139 | + $this->checkerTask->expects($this->exactly(4)) |
| 140 | + ->method('log'); |
| 141 | + |
| 142 | + $this->checkerTask->setLockfile(__FILE__); |
| 143 | + $this->checkerTask->setEndPoint('http://localhost'); |
| 144 | + $this->checkerTask->main(); |
| 145 | + } |
| 146 | + |
110 | 147 | /** |
111 | 148 | * @test |
112 | 149 | * @expectedException \BuildException |
113 | 150 | */ |
114 | 151 | public function throwsBuildExceptionWhenVulnerabilitiesFound() |
115 | 152 | { |
116 | | - $this->checker->expects($this->any()) |
| 153 | + $this->checker->expects($this->once()) |
117 | 154 | ->method('getLastVulnerabilityCount') |
118 | 155 | ->will($this->returnValue(1)); |
119 | 156 |
|
120 | 157 | $this->checkerTask->setLockfile(__FILE__); |
121 | 158 | $this->checkerTask->main(); |
122 | 159 | } |
| 160 | + |
| 161 | + /** |
| 162 | + * Helper method to create all required mock objects and configure the {@link \SensioLabs\Security\SecurityChecker} |
| 163 | + * instance to return the given $vulnerabilities. |
| 164 | + * |
| 165 | + * @param array $vulnerabilities |
| 166 | + */ |
| 167 | + protected function createMockObjects(array $vulnerabilities = []) |
| 168 | + { |
| 169 | + $this->crawler = $this->createMock(CrawlerInterface::class); |
| 170 | + $this->checker = $this->createMock(SecurityChecker::class); |
| 171 | + $this->checker->expects($this->any()) |
| 172 | + ->method('check') |
| 173 | + ->will($this->returnValue($vulnerabilities)); |
| 174 | + $this->checker->expects($this->any()) |
| 175 | + ->method('getCrawler') |
| 176 | + ->will($this->returnValue($this->crawler)); |
| 177 | + |
| 178 | + $this->checkerTask = $this->createPartialMock( |
| 179 | + SecurityCheckerTask::class, |
| 180 | + [ |
| 181 | + 'getSecurityChecker', |
| 182 | + 'log' |
| 183 | + ] |
| 184 | + ); |
| 185 | + $this->checkerTask->expects($this->any()) |
| 186 | + ->method('getSecurityChecker') |
| 187 | + ->will($this->returnValue($this->checker)); |
| 188 | + $this->checkerTask->setProject(new \Project()); |
| 189 | + } |
123 | 190 | } |
0 commit comments