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

daycry / auth / 16344800892

17 Jul 2025 12:13PM UTC coverage: 66.494% (+6.6%) from 59.854%
16344800892

push

github

daycry
Add comprehensive unit tests for helpers and services

This commit introduces extensive unit tests for helper functions (auth, checkEndpoint, checkIp, email), libraries (CheckIpInRange, Logger), and service classes (AttemptHandler, ExceptionHandler, RequestLogger). Also fixes ReflectionProperty usage in ExceptionHandler to correctly pass the exception object. These tests improve code coverage and ensure reliability of authentication, endpoint, IP checking, email, logging, and exception handling features.

1 of 1 new or added line in 1 file covered. (100.0%)

136 existing lines in 8 files now uncovered.

2177 of 3274 relevant lines covered (66.49%)

32.78 hits per line

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

0.0
/src/Controllers/RegisterController.php
1
<?php
2

3
declare(strict_types=1);
4

5
/**
6
 * This file is part of Daycry Auth.
7
 *
8
 * (c) Daycry <daycry9@proton.me>
9
 *
10
 * For the full copyright and license information, please view
11
 * the LICENSE file that was distributed with this source code.
12
 */
13

14
namespace Daycry\Auth\Controllers;
15

16
use CodeIgniter\Events\Events;
17
use CodeIgniter\HTTP\RedirectResponse;
18
use CodeIgniter\HTTP\ResponseInterface;
19
use Daycry\Auth\Entities\User;
20
use Daycry\Auth\Exceptions\ValidationException;
21
use Daycry\Auth\Models\UserModel;
22
use Daycry\Auth\Validation\ValidationRules;
23

24
/**
25
 * Class RegisterController
26
 *
27
 * Handles displaying registration form,
28
 * and handling actual registration flow.
29
 */
30
class RegisterController extends BaseAuthController
31
{
32
    /**
33
     * Displays the registration form.
34
     */
35
    public function registerView(): ResponseInterface
36
    {
37
        // Check if already logged in
UNCOV
38
        if ($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) {
×
UNCOV
39
            return $redirect;
×
40
        }
41

42
        // Check if registration is allowed
UNCOV
43
        if (! setting('Auth.allowRegistration')) {
×
UNCOV
44
            return $this->handleError(
×
UNCOV
45
                $this->request->getUri()->getPath(),
×
46
                lang('Auth.registerDisabled'),
×
47
            );
×
48
        }
49

50
        // Check if there's a pending post-auth action
51
        if ($this->hasPostAuthAction()) {
×
52
            return $this->redirectToAuthAction();
×
53
        }
54

UNCOV
55
        $content = $this->view(setting('Auth.views')['register']);
×
56

57
        return $this->response->setBody($content);
×
58
    }
59

60
    /**
61
     * Attempts to register the user.
62
     */
63
    public function registerAction(): RedirectResponse
64
    {
65
        // Check if already logged in
UNCOV
66
        if ($redirect = $this->redirectIfLoggedIn(config('Auth')->registerRedirect())) {
×
UNCOV
67
            return $redirect;
×
68
        }
69

70
        // Check if registration is allowed
UNCOV
71
        if (! setting('Auth.allowRegistration')) {
×
72
            return $this->handleError(
×
73
                config('Auth')->registerRoute(),
×
UNCOV
74
                lang('Auth.registerDisabled'),
×
UNCOV
75
            );
×
76
        }
77

78
        // Validate input
79
        $rules    = $this->getValidationRules();
×
UNCOV
80
        $postData = $this->request->getPost();
×
81

82
        if (! $this->validateRequest($postData, $rules)) {
×
UNCOV
83
            return $this->handleValidationError(config('Auth')->registerRoute());
×
84
        }
85

86
        // Save the user
UNCOV
87
        $users             = $this->getUserProvider();
×
88
        $allowedPostFields = array_keys($rules);
×
89
        $user              = $this->getUserEntity();
×
UNCOV
90
        $user->fill($this->request->getPost($allowedPostFields));
×
91

92
        // Workaround for email only registration/login
93
        if ($user->username === null) {
×
94
            $user->username = null;
×
95
        }
96

97
        try {
98
            $users->save($user);
×
99
        } catch (ValidationException $e) {
×
UNCOV
100
            return $this->handleError(
×
UNCOV
101
                config('Auth')->registerRoute(),
×
UNCOV
102
                'Registration failed',
×
103
                true,
×
104
            )->with('errors', $users->errors());
×
105
        }
106

107
        // Get complete user object with ID
UNCOV
108
        $user = $users->findById($users->getInsertID());
×
109

110
        // Add to default group
UNCOV
111
        $users->addToDefaultGroup($user);
×
112

UNCOV
113
        Events::trigger('register', $user);
×
114

115
        // Start authentication process
UNCOV
116
        $authenticator = $this->getSessionAuthenticator();
×
117
        $authenticator->startLogin($user);
×
118

119
        // Check for post-registration action
UNCOV
120
        $hasAction = $authenticator->startUpAction('register', $user);
×
UNCOV
121
        if ($hasAction) {
×
122
            return $this->redirectToAuthAction();
×
123
        }
124

125
        // Set the user active and complete login
UNCOV
126
        $user->activate();
×
UNCOV
127
        $authenticator->completeLogin($user);
×
128

UNCOV
129
        return $this->handleSuccess(
×
130
            config('Auth')->registerRedirect(),
×
UNCOV
131
            lang('Auth.registerSuccess'),
×
UNCOV
132
        );
×
133
    }
134

135
    /**
136
     * Returns the User provider
137
     */
138
    protected function getUserProvider(): UserModel
139
    {
UNCOV
140
        $provider = model(setting('Auth.userProvider'));
×
141

142
        assert($provider instanceof UserModel, 'Config Auth.userProvider is not a valid UserProvider.');
×
143

144
        return $provider;
×
145
    }
146

147
    /**
148
     * Returns the Entity class that should be used
149
     */
150
    protected function getUserEntity(): User
151
    {
UNCOV
152
        return new User();
×
153
    }
154

155
    /**
156
     * Returns the rules that should be used for validation.
157
     *
158
     * @return         array<string, array<string, list<string>|string>>
159
     * @phpstan-return array<string, array<string, string|list<string>>>
160
     */
161
    protected function getValidationRules(): array
162
    {
UNCOV
163
        $rules = new ValidationRules();
×
164

165
        return $rules->getRegistrationRules();
×
166
    }
167
}
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

© 2026 Coveralls, Inc