|
| 1 | +<?php |
| 2 | + |
| 3 | +use Opcodes\LogViewer\Enums\SortingMethod; |
| 4 | +use Opcodes\LogViewer\Enums\SortingOrder; |
| 5 | + |
| 6 | +use function Pest\Laravel\getJson; |
| 7 | + |
| 8 | +beforeEach(function () { |
| 9 | + config(['log-viewer.include_files' => ['*.log']]); |
| 10 | +}); |
| 11 | + |
| 12 | +describe('FilesController with invalid sorting values', function () { |
| 13 | + it('returns files with default desc order when invalid direction is provided', function () { |
| 14 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 15 | + $names = ['one.log', 'two.log', 'three.log']; |
| 16 | + generateLogFiles($names, randomContent: true); |
| 17 | + |
| 18 | + array_map(function (string $name) { |
| 19 | + $this->travelTo(now()->addSecond()); |
| 20 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 21 | + }, $names); |
| 22 | + |
| 23 | + $response = getJson(route('log-viewer.files', ['direction' => 'invalid'])); |
| 24 | + |
| 25 | + expect(array_column($response->json(), 'name'))->toBe([ |
| 26 | + 'three.log', |
| 27 | + 'two.log', |
| 28 | + 'one.log', |
| 29 | + ]); |
| 30 | + }); |
| 31 | + |
| 32 | + it('returns files with default desc order when empty direction is provided', function () { |
| 33 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 34 | + $names = ['one.log', 'two.log', 'three.log']; |
| 35 | + generateLogFiles($names, randomContent: true); |
| 36 | + |
| 37 | + array_map(function (string $name) { |
| 38 | + $this->travelTo(now()->addSecond()); |
| 39 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 40 | + }, $names); |
| 41 | + |
| 42 | + $response = getJson(route('log-viewer.files', ['direction' => ''])); |
| 43 | + |
| 44 | + expect(array_column($response->json(), 'name'))->toBe([ |
| 45 | + 'three.log', |
| 46 | + 'two.log', |
| 47 | + 'one.log', |
| 48 | + ]); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns files with default desc order when direction is null', function () { |
| 52 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 53 | + $names = ['one.log', 'two.log', 'three.log']; |
| 54 | + generateLogFiles($names, randomContent: true); |
| 55 | + |
| 56 | + array_map(function (string $name) { |
| 57 | + $this->travelTo(now()->addSecond()); |
| 58 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 59 | + }, $names); |
| 60 | + |
| 61 | + $response = getJson(route('log-viewer.files')); |
| 62 | + |
| 63 | + expect(array_column($response->json(), 'name'))->toBe([ |
| 64 | + 'three.log', |
| 65 | + 'two.log', |
| 66 | + 'one.log', |
| 67 | + ]); |
| 68 | + }); |
| 69 | + |
| 70 | + it('returns files with alphabetical sorting and default desc when invalid direction is provided', function () { |
| 71 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]); |
| 72 | + generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true); |
| 73 | + |
| 74 | + $response = getJson(route('log-viewer.files', ['direction' => 'invalid'])); |
| 75 | + |
| 76 | + expect(array_column($response->json(), 'name'))->toBe([ |
| 77 | + 'two.log', |
| 78 | + 'three.log', |
| 79 | + 'one.log', |
| 80 | + 'four.log', |
| 81 | + ]); |
| 82 | + }); |
| 83 | + |
| 84 | + it('returns files in correct order with valid asc direction and alphabetical sorting', function () { |
| 85 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]); |
| 86 | + generateLogFiles(['one.log', 'two.log', 'three.log', 'four.log'], randomContent: true); |
| 87 | + |
| 88 | + $response = getJson(route('log-viewer.files', ['direction' => SortingOrder::Ascending])); |
| 89 | + |
| 90 | + expect(array_column($response->json(), 'name'))->toBe([ |
| 91 | + 'four.log', |
| 92 | + 'one.log', |
| 93 | + 'three.log', |
| 94 | + 'two.log', |
| 95 | + ]); |
| 96 | + }); |
| 97 | +}); |
| 98 | + |
| 99 | +describe('FoldersController with invalid sorting values', function () { |
| 100 | + beforeEach(function () { |
| 101 | + config(['log-viewer.include_files' => ['*/**.log']]); |
| 102 | + }); |
| 103 | + |
| 104 | + it('returns folders with files in default desc order when invalid direction is provided', function () { |
| 105 | + config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]); |
| 106 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 107 | + $names = ['sub/one.log', 'sub/two.log', 'sub/three.log']; |
| 108 | + generateLogFiles($names, randomContent: true); |
| 109 | + |
| 110 | + array_map(function (string $name) { |
| 111 | + $this->travelTo(now()->addSecond()); |
| 112 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 113 | + }, $names); |
| 114 | + |
| 115 | + $response = getJson(route('log-viewer.folders', ['direction' => 'invalid'])); |
| 116 | + |
| 117 | + expect(array_column($response->json()[0]['files'], 'name'))->toBe([ |
| 118 | + 'three.log', |
| 119 | + 'two.log', |
| 120 | + 'one.log', |
| 121 | + ]); |
| 122 | + }); |
| 123 | + |
| 124 | + it('returns folders with files in default desc order when empty direction is provided', function () { |
| 125 | + config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]); |
| 126 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 127 | + $names = ['sub/one.log', 'sub/two.log', 'sub/three.log']; |
| 128 | + generateLogFiles($names, randomContent: true); |
| 129 | + |
| 130 | + array_map(function (string $name) { |
| 131 | + $this->travelTo(now()->addSecond()); |
| 132 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 133 | + }, $names); |
| 134 | + |
| 135 | + $response = getJson(route('log-viewer.folders', ['direction' => ''])); |
| 136 | + |
| 137 | + expect(array_column($response->json()[0]['files'], 'name'))->toBe([ |
| 138 | + 'three.log', |
| 139 | + 'two.log', |
| 140 | + 'one.log', |
| 141 | + ]); |
| 142 | + }); |
| 143 | + |
| 144 | + it('returns folders with files in default desc order when direction is null', function () { |
| 145 | + config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]); |
| 146 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::ModifiedTime]); |
| 147 | + $names = ['sub/one.log', 'sub/two.log', 'sub/three.log']; |
| 148 | + generateLogFiles($names, randomContent: true); |
| 149 | + |
| 150 | + array_map(function (string $name) { |
| 151 | + $this->travelTo(now()->addSecond()); |
| 152 | + touch(storage_path('logs/'.$name), now()->timestamp); |
| 153 | + }, $names); |
| 154 | + |
| 155 | + $response = getJson(route('log-viewer.folders')); |
| 156 | + |
| 157 | + expect(array_column($response->json()[0]['files'], 'name'))->toBe([ |
| 158 | + 'three.log', |
| 159 | + 'two.log', |
| 160 | + 'one.log', |
| 161 | + ]); |
| 162 | + }); |
| 163 | + |
| 164 | + it('returns folders with files in correct order with valid asc direction and alphabetical sorting', function () { |
| 165 | + config(['log-viewer.defaults.folder_sorting_method' => SortingMethod::Alphabetical]); |
| 166 | + config(['log-viewer.defaults.file_sorting_method' => SortingMethod::Alphabetical]); |
| 167 | + generateLogFiles(['sub/one.log', 'sub/two.log', 'sub/three.log', 'sub/four.log'], randomContent: true); |
| 168 | + |
| 169 | + $response = getJson(route('log-viewer.folders', ['direction' => SortingOrder::Ascending])); |
| 170 | + |
| 171 | + expect(array_column($response->json()[0]['files'], 'name'))->toBe([ |
| 172 | + 'four.log', |
| 173 | + 'one.log', |
| 174 | + 'three.log', |
| 175 | + 'two.log', |
| 176 | + ]); |
| 177 | + }); |
| 178 | +}); |
0 commit comments