This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include
, or for example
via PHP's auto-loading mechanism.
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 KamranAhmed\Smasher; |
||
4 | |||
5 | use RecursiveDirectoryIterator; |
||
6 | use RecursiveIteratorIterator; |
||
7 | use ReflectionClass; |
||
8 | |||
9 | /** |
||
10 | * Class ScannerTest |
||
11 | * |
||
12 | * Tests the Scanner class |
||
13 | * |
||
14 | * @package KamranAhmed\Smasher |
||
15 | */ |
||
16 | class ScannerTest extends \PHPUnit_Framework_TestCase |
||
17 | { |
||
18 | private $sampleDirPath; |
||
19 | private $invalidDirPath; |
||
20 | private $outputJsonPath; |
||
21 | private $basePathToPopulate; |
||
22 | |||
23 | private $populatedDir; |
||
24 | private $populatedFile; |
||
25 | |||
26 | private $invalidScanSample; |
||
27 | private $emptyScanSample; |
||
28 | private $sampleJson; |
||
29 | |||
30 | /** |
||
31 | * Setup the class for testing |
||
32 | */ |
||
33 | public function setUp() |
||
34 | { |
||
35 | $currentDir = __DIR__; |
||
36 | |||
37 | $this->sampleDirPath = $currentDir . '/data/sample-path'; |
||
38 | $this->invalidDirPath = $currentDir . '/invalid/path/that/does/not/exist'; |
||
39 | $this->outputJsonPath = $currentDir . '/data/output/sample-path.json'; |
||
40 | $this->basePathToPopulate = $currentDir . '/data/output/'; |
||
41 | |||
42 | $this->populatedDir = $currentDir . '/data/output/sample-path'; |
||
43 | $this->populatedFile = $currentDir . '/data/output/sample-path/child-item/grand-child/child-file.md'; |
||
44 | |||
45 | $this->invalidScanSample = $currentDir . '/data/scanned-samples/invalid-scan.md'; |
||
46 | $this->emptyScanSample = $currentDir . '/data/scanned-samples/empty-scan.json'; |
||
47 | $this->sampleJson = $currentDir . '/data/scanned-samples/scanned-json.json'; |
||
48 | } |
||
49 | |||
50 | public function testCanScanPathAndGetResult() |
||
51 | { |
||
52 | $scanner = new Scanner(new JsonResponse()); |
||
53 | $scanResult = $scanner->scan($this->sampleDirPath); |
||
54 | |||
55 | $this->assertTrue($this->isValidJson($scanResult)); |
||
56 | } |
||
57 | |||
58 | private function isValidJson($json) |
||
59 | { |
||
60 | json_decode($json); |
||
61 | |||
62 | return json_last_error() === JSON_ERROR_NONE; |
||
63 | } |
||
64 | |||
65 | public function testCanScanPathAndCreateValidResponseFile() |
||
66 | { |
||
67 | $scanner = new Scanner(new JsonResponse()); |
||
68 | $scanner->scan($this->sampleDirPath, $this->outputJsonPath); |
||
69 | |||
70 | $this->assertTrue(file_exists($this->outputJsonPath)); |
||
71 | |||
72 | $result = file_get_contents($this->outputJsonPath); |
||
73 | $this->assertTrue($this->isValidJson($result)); |
||
74 | } |
||
75 | |||
76 | public function testCanPopulatePathUsingInputFile() |
||
77 | { |
||
78 | $scanner = new Scanner(new JsonResponse()); |
||
79 | $scanner->populate($this->basePathToPopulate, $this->sampleJson); |
||
80 | |||
81 | $this->assertFileExists($this->populatedFile); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @expectedException \KamranAhmed\Smasher\Exceptions\UnreadablePathException |
||
86 | */ |
||
87 | public function testThrowsExceptionTryingToScanInvalidPath() |
||
88 | { |
||
89 | $scanner = new Scanner(new JsonResponse()); |
||
90 | $scanner->scan($this->invalidDirPath); |
||
91 | } |
||
92 | |||
93 | View Code Duplication | public function testCanProbePathAndGenerateArrayOfContent() |
|
0 ignored issues
–
show
|
|||
94 | { |
||
95 | $scanner = new Scanner(new JsonResponse()); |
||
96 | $output = []; |
||
97 | |||
98 | $this->callProtectedMethod($scanner, 'probe', [ |
||
99 | $this->sampleDirPath, |
||
100 | &$output, |
||
101 | ]); |
||
102 | |||
103 | // Verifying that a valid array is returned by checking |
||
104 | // Orchestrate a better way to verify this array. |
||
105 | $this->assertTrue(isset($output['sample-path']['child-item']['grand-child']['child-file.md'])); |
||
106 | } |
||
107 | |||
108 | public static function callProtectedMethod($object, $method, array $args = []) |
||
109 | { |
||
110 | $class = new ReflectionClass(get_class($object)); |
||
111 | $method = $class->getMethod($method); |
||
112 | $method->setAccessible(true); |
||
113 | |||
114 | return $method->invokeArgs($object, $args); |
||
115 | } |
||
116 | |||
117 | /** |
||
118 | * @expectedException \KamranAhmed\Smasher\Exceptions\InvalidContentException |
||
119 | */ |
||
120 | public function testGettingContentThrowsExceptionForInvalidFile() |
||
121 | { |
||
122 | $scanner = new Scanner(new JsonResponse()); |
||
123 | $this->callProtectedMethod($scanner, 'getScannedContent', [ |
||
124 | $this->invalidScanSample, |
||
125 | ]); |
||
126 | } |
||
127 | |||
128 | /** |
||
129 | * @expectedException \KamranAhmed\Smasher\Exceptions\NoContentException |
||
130 | */ |
||
131 | public function testGettingContentThrowsExceptionForEmptyFile() |
||
132 | { |
||
133 | $scanner = new Scanner(new JsonResponse()); |
||
134 | $this->callProtectedMethod($scanner, 'getScannedContent', [ |
||
135 | $this->emptyScanSample, |
||
136 | ]); |
||
137 | } |
||
138 | |||
139 | View Code Duplication | public function testCanGetScannedContentFromJsonFile() |
|
0 ignored issues
–
show
This method seems to be duplicated in your project.
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation. You can also find more detailed suggestions in the “Code” section of your repository. ![]() |
|||
140 | { |
||
141 | $scanner = new Scanner(new JsonResponse()); |
||
142 | $scannedArray = $this->callProtectedMethod($scanner, 'getScannedContent', [ |
||
143 | $this->sampleJson, |
||
144 | ]); |
||
145 | |||
146 | // Verifying that a valid array is returned by checking |
||
147 | // Orchestrate a better way to verify this array. |
||
148 | $this->assertTrue(isset($scannedArray['sample-path']['child-item']['grand-child']['child-file.md'])); |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Remove unnecessary files/directories |
||
153 | */ |
||
154 | protected function tearDown() |
||
155 | { |
||
156 | if (file_exists($this->populatedDir)) { |
||
157 | $this->removeDirectory($this->populatedDir); |
||
158 | } |
||
159 | |||
160 | if (file_exists($this->outputJsonPath)) { |
||
161 | unlink($this->outputJsonPath); |
||
162 | } |
||
163 | } |
||
164 | |||
165 | private function removeDirectory($directory) |
||
166 | { |
||
167 | $iterator = new RecursiveDirectoryIterator($directory, RecursiveDirectoryIterator::SKIP_DOTS); |
||
168 | $files = new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST); |
||
169 | |||
170 | foreach ($files as $file) { |
||
171 | if ($file->isDir()) { |
||
172 | rmdir($file->getRealPath()); |
||
173 | } else { |
||
174 | unlink($file->getRealPath()); |
||
175 | } |
||
176 | } |
||
177 | |||
178 | rmdir($directory); |
||
179 | } |
||
180 | } |
||
181 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.