• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-helpers / 17169773811

23 Aug 2025 01:57AM UTC coverage: 77.967% (-0.02%) from 77.985%
17169773811

Pull #217

github

web-flow
Merge 5b934cda8 into bc8bea176
Pull Request #217: feat: add TEST_TOKEN

0 of 4 new or added lines in 1 file covered. (0.0%)

3 existing lines in 1 file now uncovered.

1143 of 1466 relevant lines covered (77.97%)

12.78 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/HelpersServiceProvider.php
1
<?php
2

3
namespace RonasIT\Support;
4

5
use Illuminate\Routing\Route;
6
use Illuminate\Routing\Router;
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\Auth;
9
use Illuminate\Support\Facades\DB;
10
use Illuminate\Support\Facades\URL;
11
use Illuminate\Support\Facades\ParallelTesting;
12
use Illuminate\Support\Facades\Route as RouteFacade;
13
use Illuminate\Support\Facades\Validator;
14
use Illuminate\Support\ServiceProvider;
15
use Illuminate\Testing\Concerns\TestDatabases;
16
use Maatwebsite\Excel\ExcelServiceProvider;
17
use RonasIT\Support\Contracts\VersionEnumContract as Version;
18
use RonasIT\Support\Exceptions\BindingVersionEnumException;
19
use RonasIT\Support\Exceptions\InvalidValidationRuleUsageException;
20
use RonasIT\Support\Http\Middleware\SecurityMiddleware;
21

22
class HelpersServiceProvider extends ServiceProvider
23
{
24
    use TestDatabases;
25

26
    public function boot(): void
27
    {
28
        URL::forceHttps();
279✔
29

30
        $router = $this->app['router'];
279✔
31

32
        $router->prependMiddlewareToGroup('web', SecurityMiddleware::class);
279✔
33
        $router->prependMiddlewareToGroup('api', SecurityMiddleware::class);
279✔
34

35
        $this->extendValidator();
279✔
36

37
        app(ExcelServiceProvider::class, ['app' => app()])->boot();
279✔
38

39
        $this->extendRouter();
279✔
40

41
        if ($this->app->runningUnitTests()) {
279✔
42
            $this->whenNotUsingInMemoryDatabase(function ($database) {
279✔
43
                [$testDatabase, $created] = $this->ensureTestDatabaseExists($database);
×
44

UNCOV
45
                $this->switchToDatabase($testDatabase);
×
46

UNCOV
47
                if ($created) {
×
UNCOV
48
                    ParallelTesting::callSetUpTestDatabaseCallbacks($testDatabase);
×
49
                }
50
            });
279✔
51
        }
52
    }
53

54
    public function register(): void
55
    {
56
        app(ExcelServiceProvider::class, ['app' => app()])->register();
279✔
57
    }
58

59
    protected function extendValidator(): void
60
    {
61
        Validator::extend('unique_except_of_authorized_user', function ($attribute, $value, $parameters = []) {
279✔
62
            $table = Arr::get($parameters, 0, 'users');
4✔
63
            $keyField = Arr::get($parameters, 1, 'id');
4✔
64
            $userId = Auth::id();
4✔
65

66
            $result = DB::table($table)
4✔
67
                ->where($keyField, '<>', $userId)
4✔
68
                ->whereIn($attribute, Arr::flatten((array) $value))
4✔
69
                ->exists();
4✔
70

71
            return !$result;
4✔
72
        });
279✔
73

74
        Validator::extend('list_exists', function ($attribute, $value, $parameters, $validator) {
279✔
75
            if (!is_array($value)) {
7✔
76
                $validator->addReplacer('list_exists', fn ($message, $attribute) => "The {$attribute} field must be an array.");
1✔
77

78
                return false;
1✔
79
            }
80

81
            if (count($parameters) < 1) {
6✔
82
                throw new InvalidValidationRuleUsageException("list_exists: At least 1 parameter must be added when checking the {$attribute} field in the request.");
1✔
83
            }
84

85
            $hasFieldNameParam = !empty(Arr::get($parameters, 2));
5✔
86

87
            if (is_multidimensional($value) && !$hasFieldNameParam) {
5✔
88
                throw new InvalidValidationRuleUsageException("list_exists: The third parameter should be filled when checking the {$attribute} field if we are using a collection in request.");
1✔
89
            }
90

91
            if ($hasFieldNameParam) {
4✔
92
                $value = Arr::pluck($value, Arr::get($parameters, 2));
1✔
93
            }
94

95
            $value = array_unique($value);
4✔
96

97
            $table = Arr::get($parameters, 0);
4✔
98
            $keyField = Arr::get($parameters, 1, 'id');
4✔
99

100
            $existingValueCount = DB::table($table)
4✔
101
                ->whereIn($keyField, $value)
4✔
102
                ->distinct()
4✔
103
                ->count($keyField);
4✔
104

105
            $validator->addReplacer('list_exists', fn ($message, $attribute) => "Some of the passed {$attribute} are not exists.");
4✔
106

107
            return $existingValueCount === count($value);
4✔
108
        });
279✔
109
    }
110

111
    protected function extendRouter(): void
112
    {
113
        /**
114
         * Specify that the route version must be in the range of given values inclusive.
115
         *
116
         * @param Version|null $start
117
         * @param Version|null $end
118
         * @param string|null $param (default is 'version')
119
         * @param Route|null $instance
120
         * @return Router|Route
121
         */
122
        $versionRange = function (
279✔
123
            ?Version $start,
279✔
124
            ?Version $end,
279✔
125
            ?string $param,
279✔
126
            Route $instance = null
279✔
127
        ) {
279✔
128
            if (!$param) {
39✔
129
                $param = 'version';
39✔
130
            }
131

132
            if (!$this->app->bound(Version::class)) {
39✔
133
                throw new BindingVersionEnumException();
1✔
134
            }
135

136
            $versionEnum = app(Version::class);
38✔
137
            $disabledVersions = config('app.disabled_api_versions') ?: [];
38✔
138

139
            $versions = array_diff($versionEnum::values(), $disabledVersions);
38✔
140

141
            $versions = array_filter($versions, function ($version) use ($start, $end) {
38✔
142
                $result = true;
38✔
143

144
                if (!empty($start)) {
38✔
145
                    $result &= version_compare($version, $start->value, '>=');
38✔
146
                }
147

148
                if (!empty($end)) {
38✔
149
                    $result &= version_compare($version, $end->value, '<=');
38✔
150
                }
151

152
                return $result;
38✔
153
            });
38✔
154

155
            return (!empty($instance))
38✔
156
                ? $instance->whereIn($param, $versions)
38✔
157
                : RouteFacade::whereIn($param, $versions);
38✔
158
        };
279✔
159

160
        Route::macro(
279✔
161
            'versionRange',
279✔
162
            fn (Version $from, Version $to, $param = null) => $versionRange($from, $to, $param, $this)
279✔
163
        );
279✔
164
        Route::macro('versionFrom', fn (Version $from, $param = null) => $versionRange($from, null, $param, $this));
279✔
165
        Route::macro('versionTo', fn (Version $to, $param = null) => $versionRange(null, $to, $param, $this));
279✔
166

167
        RouteFacade::macro(
279✔
168
            'versionRange',
279✔
169
            fn (Version $from, Version $to, string $param = null) => $versionRange($from, $to, $param)
279✔
170
        );
279✔
171
        RouteFacade::macro('versionFrom', fn (Version $from, $param = null) => $versionRange($from, null, $param));
279✔
172
        RouteFacade::macro('versionTo', fn (Version $to, $param = null) => $versionRange(null, $to, $param));
279✔
173
        RouteFacade::macro('version', fn (Version $version) => RouteFacade::prefix('v' . $version->value));
279✔
174
    }
175
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc