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

Commit eacc2cd

Browse files
committed
Refactor unit tests
1 parent 1cdedfb commit eacc2cd

1 file changed

Lines changed: 86 additions & 19 deletions

File tree

tests/bitExpert/Phing/SecurityChecker/SecurityCheckerTaskUnitTest.php

Lines changed: 86 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace bitExpert\Phing\SecurityChecker;
1313

14-
use bitExpert\Phing\SecurityChecker\SecurityCheckerTask;
1514
use PHPUnit\Framework\TestCase;
1615
use SensioLabs\Security\Crawler\CrawlerInterface;
1716
use SensioLabs\Security\SecurityChecker;
@@ -43,23 +42,7 @@ protected function setUp()
4342
{
4443
parent::setUp();
4544

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();
6346
}
6447

6548
/**
@@ -107,17 +90,101 @@ public function endPointParameterShouldBePassedToSecurityCheckerWhenGiven()
10790
$this->checkerTask->main();
10891
}
10992

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+
110147
/**
111148
* @test
112149
* @expectedException \BuildException
113150
*/
114151
public function throwsBuildExceptionWhenVulnerabilitiesFound()
115152
{
116-
$this->checker->expects($this->any())
153+
$this->checker->expects($this->once())
117154
->method('getLastVulnerabilityCount')
118155
->will($this->returnValue(1));
119156

120157
$this->checkerTask->setLockfile(__FILE__);
121158
$this->checkerTask->main();
122159
}
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+
}
123190
}

0 commit comments

Comments
 (0)