|
1 | 1 | <?php |
2 | 2 |
|
| 3 | +use Opcodes\LogViewer\Enums\SortingMethod; |
| 4 | +use Opcodes\LogViewer\Enums\SortingOrder; |
3 | 5 | use Opcodes\LogViewer\Facades\LogViewer; |
4 | 6 | use Opcodes\LogViewer\LogFile; |
5 | 7 | use Opcodes\LogViewer\LogFileCollection; |
|
151 | 153 | ->and($collection[2])->toBe($bFolder) |
152 | 154 | ->and($collection[3])->toBe($aFolder); |
153 | 155 | }); |
| 156 | + |
| 157 | +test('LogFolderCollection can sort by ModifiedTime ascending using sortUsing method', function () { |
| 158 | + $firstFolder = Mockery::mock(new LogFolder('folder', [])) |
| 159 | + ->allows(['earliestTimestamp' => now()->subDay()->timestamp]); |
| 160 | + $secondFolder = Mockery::mock(new LogFolder('folder2', [])) |
| 161 | + ->allows(['earliestTimestamp' => now()->subDays(2)->timestamp]); |
| 162 | + $collection = new LogFolderCollection([$firstFolder, $secondFolder]); |
| 163 | + |
| 164 | + $collection->sortUsing(SortingMethod::ModifiedTime, SortingOrder::Ascending); |
| 165 | + |
| 166 | + expect($collection[0])->toBe($secondFolder) |
| 167 | + ->and($collection[1])->toBe($firstFolder); |
| 168 | +}); |
| 169 | + |
| 170 | +test('LogFolderCollection can sort by ModifiedTime descending using sortUsing method', function () { |
| 171 | + $firstFolder = Mockery::mock(new LogFolder('folder', [])) |
| 172 | + ->allows(['latestTimestamp' => now()->subDays(2)->timestamp]); |
| 173 | + $secondFolder = Mockery::mock(new LogFolder('folder2', [])) |
| 174 | + ->allows(['latestTimestamp' => now()->subDay()->timestamp]); |
| 175 | + $collection = new LogFolderCollection([$firstFolder, $secondFolder]); |
| 176 | + |
| 177 | + $collection->sortUsing(SortingMethod::ModifiedTime, SortingOrder::Descending); |
| 178 | + |
| 179 | + expect($collection[0])->toBe($secondFolder) |
| 180 | + ->and($collection[1])->toBe($firstFolder); |
| 181 | +}); |
| 182 | + |
| 183 | +test('LogFolderCollection can sort alphabetically ascending using sortUsing method, with root always on top', function () { |
| 184 | + $rootFolder = Mockery::mock(new LogFolder('', []))->allows(['isRoot' => true, 'cleanPath' => LogFolder::rootPrefix()]); |
| 185 | + $bFolder = Mockery::mock(new LogFolder('b', []))->allows(['isRoot' => false, 'cleanPath' => 'b']); |
| 186 | + $aFolder = Mockery::mock(new LogFolder('a', []))->allows(['isRoot' => false, 'cleanPath' => 'a']); |
| 187 | + $zFolder = Mockery::mock(new LogFolder('z', []))->allows(['isRoot' => false, 'cleanPath' => 'z']); |
| 188 | + $collection = new LogFolderCollection([$zFolder, $rootFolder, $bFolder, $aFolder]); |
| 189 | + |
| 190 | + $collection->sortUsing(SortingMethod::Alphabetical, SortingOrder::Ascending); |
| 191 | + |
| 192 | + expect($collection[0])->toBe($rootFolder) |
| 193 | + ->and($collection[1])->toBe($aFolder) |
| 194 | + ->and($collection[2])->toBe($bFolder) |
| 195 | + ->and($collection[3])->toBe($zFolder); |
| 196 | +}); |
| 197 | + |
| 198 | +test('LogFolderCollection can sort alphabetically descending using sortUsing method, with root always on top', function () { |
| 199 | + $rootFolder = Mockery::mock(new LogFolder('', []))->allows(['isRoot' => true, 'cleanPath' => LogFolder::rootPrefix()]); |
| 200 | + $bFolder = Mockery::mock(new LogFolder('b', []))->allows(['isRoot' => false, 'cleanPath' => 'b']); |
| 201 | + $aFolder = Mockery::mock(new LogFolder('a', []))->allows(['isRoot' => false, 'cleanPath' => 'a']); |
| 202 | + $zFolder = Mockery::mock(new LogFolder('z', []))->allows(['isRoot' => false, 'cleanPath' => 'z']); |
| 203 | + $collection = new LogFolderCollection([$aFolder, $zFolder, $rootFolder, $bFolder]); |
| 204 | + |
| 205 | + $collection->sortUsing(SortingMethod::Alphabetical, SortingOrder::Descending); |
| 206 | + |
| 207 | + expect($collection[0])->toBe($rootFolder) |
| 208 | + ->and($collection[1])->toBe($zFolder) |
| 209 | + ->and($collection[2])->toBe($bFolder) |
| 210 | + ->and($collection[3])->toBe($aFolder); |
| 211 | +}); |
0 commit comments