-
-
Save tuanpht/f453d03bfdbc0181b636b81efcd44d97 to your computer and use it in GitHub Desktop.
Rector result for repo https://github.com/tuanpht/laravel-test-example/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/app/Http/Controllers/Web/HomeController.php b/app/Http/Controllers/Web/HomeController.php | |
index 4f5dca9..f7d42c0 100644 | |
--- a/app/Http/Controllers/Web/HomeController.php | |
+++ b/app/Http/Controllers/Web/HomeController.php | |
@@ -6,8 +6,16 @@ use App\Http\Controllers\Controller; | |
class HomeController extends Controller | |
{ | |
+ private $viewFactory; | |
+ | |
+ public function __construct( | |
+ \Illuminate\Contracts\View\Factory $viewFactory | |
+ ) { | |
+ $this->viewFactory = $viewFactory; | |
+ } | |
+ | |
public function index() | |
{ | |
- return view('home'); | |
+ return $this->viewFactory->make('home'); | |
} | |
} | |
diff --git a/app/Http/Controllers/Web/RegisterController.php b/app/Http/Controllers/Web/RegisterController.php | |
index 4c58e02..296b0b0 100644 | |
--- a/app/Http/Controllers/Web/RegisterController.php | |
+++ b/app/Http/Controllers/Web/RegisterController.php | |
@@ -4,8 +4,6 @@ namespace App\Http\Controllers\Web; | |
use App\Http\Controllers\Controller; | |
use App\Http\Requests\Web\RegisterRequest; | |
-use App\Services\Web\UserService; | |
-use Illuminate\Support\Facades\Mail; | |
use App\Mail\UserRegistered; | |
use Illuminate\Http\Request; | |
@@ -13,9 +11,22 @@ class RegisterController extends Controller | |
{ | |
protected $userService; | |
- public function __construct(UserService $userService) | |
- { | |
+ private $viewFactory; | |
+ | |
+ private $mailer; | |
+ | |
+ private $redirector; | |
+ | |
+ public function __construct( | |
+ \App\Services\Web\UserService $userService, | |
+ \Illuminate\Contracts\View\Factory $viewFactory, | |
+ \Illuminate\Contracts\Mail\Mailer $mailer, | |
+ \Illuminate\Routing\Redirector $redirector | |
+ ) { | |
$this->userService = $userService; | |
+ $this->viewFactory = $viewFactory; | |
+ $this->mailer = $mailer; | |
+ $this->redirector = $redirector; | |
} | |
/** | |
@@ -25,12 +36,12 @@ class RegisterController extends Controller | |
*/ | |
public function showFormRegister() | |
{ | |
- return view('auth.register'); | |
+ return $this->viewFactory->make('auth.register'); | |
} | |
public function showRegisterSuccess() | |
{ | |
- return view('auth.register_success'); | |
+ return $this->viewFactory->make('auth.register_success'); | |
} | |
public function register(RegisterRequest $request) | |
@@ -39,9 +50,9 @@ class RegisterController extends Controller | |
$user = $this->userService->create($inputs); | |
- Mail::to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
+ $this->mailer->to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
- return redirect()->action([static::class, 'showRegisterSuccess']); | |
+ return $this->redirector->action([static::class, 'showRegisterSuccess']); | |
} | |
public function verify(Request $request) | |
@@ -55,12 +66,12 @@ class RegisterController extends Controller | |
$this->userService->verifyUser($user); | |
} | |
- return view('auth.verify.message'); | |
+ return $this->viewFactory->make('auth.verify.message'); | |
} | |
public function showFormVerification() | |
{ | |
- return view('auth.verify.resend'); | |
+ return $this->viewFactory->make('auth.verify.resend'); | |
} | |
public function resendVerificationLink(Request $request) | |
@@ -68,9 +79,9 @@ class RegisterController extends Controller | |
$user = $this->userService->findByEmail($request->input('email')); | |
if ($user && !$user->hasVerifiedEmail()) { | |
- Mail::to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
+ $this->mailer->to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
} | |
- return redirect()->action([static::class, 'showFormVerification'])->with('resent', true); | |
+ return $this->redirector->action([static::class, 'showFormVerification'])->with('resent', true); | |
} | |
} | |
diff --git a/app/Http/Middleware/Authenticate.php b/app/Http/Middleware/Authenticate.php | |
index a4be5c5..568774d 100644 | |
--- a/app/Http/Middleware/Authenticate.php | |
+++ b/app/Http/Middleware/Authenticate.php | |
@@ -6,6 +6,17 @@ use Illuminate\Auth\Middleware\Authenticate as Middleware; | |
class Authenticate extends Middleware | |
{ | |
+ private $urlGenerator; | |
+ | |
+ public function __construct( | |
+ \Illuminate\Contracts\Auth\Factory $authFactory, | |
+ \Illuminate\Routing\UrlGenerator $urlGenerator | |
+ ) { | |
+ $this->urlGenerator = $urlGenerator; | |
+ | |
+ parent::__construct($authFactory); | |
+ } | |
+ | |
/** | |
* Get the path the user should be redirected to when they are not authenticated. | |
* | |
@@ -15,7 +26,7 @@ class Authenticate extends Middleware | |
protected function redirectTo($request) | |
{ | |
if (! $request->expectsJson()) { | |
- return route('login'); | |
+ return $this->urlGenerator->route('login'); | |
} | |
} | |
} | |
diff --git a/app/Http/Middleware/RedirectIfAuthenticated.php b/app/Http/Middleware/RedirectIfAuthenticated.php | |
index e4cec9c..376a3bb 100644 | |
--- a/app/Http/Middleware/RedirectIfAuthenticated.php | |
+++ b/app/Http/Middleware/RedirectIfAuthenticated.php | |
@@ -3,10 +3,21 @@ | |
namespace App\Http\Middleware; | |
use Closure; | |
-use Illuminate\Support\Facades\Auth; | |
class RedirectIfAuthenticated | |
{ | |
+ private $authFactory; | |
+ | |
+ private $redirector; | |
+ | |
+ public function __construct( | |
+ \Illuminate\Contracts\Auth\Factory $authFactory, | |
+ \Illuminate\Routing\Redirector $redirector | |
+ ) { | |
+ $this->authFactory = $authFactory; | |
+ $this->redirector = $redirector; | |
+ } | |
+ | |
/** | |
* Handle an incoming request. | |
* | |
@@ -17,8 +28,8 @@ class RedirectIfAuthenticated | |
*/ | |
public function handle($request, Closure $next, $guard = null) | |
{ | |
- if (Auth::guard($guard)->check()) { | |
- return redirect('/home'); | |
+ if ($this->authFactory->guard($guard)->check()) { | |
+ return $this->redirector->back('/home'); | |
} | |
return $next($request); | |
diff --git a/app/Mail/UserRegistered.php b/app/Mail/UserRegistered.php | |
index 9c553cb..d5d3e9b 100644 | |
--- a/app/Mail/UserRegistered.php | |
+++ b/app/Mail/UserRegistered.php | |
@@ -6,7 +6,7 @@ use Illuminate\Bus\Queueable; | |
use Illuminate\Mail\Mailable; | |
use Illuminate\Queue\SerializesModels; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
-use Illuminate\Support\Facades\URL; | |
+use Illuminate\Routing\UrlGenerator; | |
class UserRegistered extends Mailable implements ShouldQueue | |
{ | |
@@ -27,19 +27,19 @@ class UserRegistered extends Mailable implements ShouldQueue | |
* | |
* @return $this | |
*/ | |
- public function build() | |
+ public function build(UrlGenerator $urlGenerator) | |
{ | |
return $this->markdown('emails.user.registered') | |
->subject('Verify your account') | |
->with([ | |
- 'url' => $this->verificationUrl(), | |
+ 'url' => $this->verificationUrl($urlGenerator), | |
'userName' => $this->userName, | |
]); | |
} | |
- private function verificationUrl() | |
+ private function verificationUrl(UrlGenerator $urlGenerator) | |
{ | |
- return URL::temporarySignedRoute( | |
+ return $urlGenerator->temporarySignedRoute( | |
'web.register.verify', | |
now()->addMinutes(60), | |
['id' => $this->userId] | |
diff --git a/tests/Unit/Http/Controllers/Web/HomeControllerTest.php b/tests/Unit/Http/Controllers/Web/HomeControllerTest.php | |
index 9c92505..8981761 100644 | |
--- a/tests/Unit/Http/Controllers/Web/HomeControllerTest.php | |
+++ b/tests/Unit/Http/Controllers/Web/HomeControllerTest.php | |
@@ -9,7 +9,7 @@ class HomeControllerTest extends TestCase | |
{ | |
public function testIndexCorrectView() | |
{ | |
- $controller = new HomeController; | |
+ $controller = $this->app->make(HomeController::class); | |
$view = $controller->index(); | |
diff --git a/tests/Unit/Http/Controllers/Web/RegisterControllerTest.php b/tests/Unit/Http/Controllers/Web/RegisterControllerTest.php | |
index c8f655b..5c74642 100644 | |
--- a/tests/Unit/Http/Controllers/Web/RegisterControllerTest.php | |
+++ b/tests/Unit/Http/Controllers/Web/RegisterControllerTest.php | |
@@ -11,6 +11,9 @@ use Illuminate\Http\RedirectResponse; | |
use Illuminate\Support\Facades\Mail; | |
use App\Mail\UserRegistered; | |
use App\Models\User; | |
+use Illuminate\Routing\Redirector; | |
+use Illuminate\Contracts\View\Factory; | |
+use Illuminate\Support\Testing\Fakes\MailFake; | |
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | |
class RegisterControllerTest extends TestCase | |
@@ -21,13 +24,19 @@ class RegisterControllerTest extends TestCase | |
/** @var UserService|\Mockery\MockInterface */ | |
private $userService; | |
+ /** @var MailFake */ | |
+ private $mailer; | |
+ | |
public function setUp(): void | |
{ | |
parent::setUp(); | |
$this->userService = Mockery::mock(UserService::class); | |
+ $this->mailer = new MailFake; | |
+ $viewFactory = $this->app->make(Factory::class); | |
+ $redirector = $this->app->make(Redirector::class); | |
- $this->registerController = new RegisterController($this->userService); | |
+ $this->registerController = new RegisterController($this->userService, $viewFactory, $this->mailer, $redirector); | |
} | |
public function testShowFormRegister() | |
@@ -60,11 +69,10 @@ class RegisterControllerTest extends TestCase | |
->shouldReceive('create') | |
->with($filteredInputs) | |
->andReturn(new User($filteredInputs)); | |
- Mail::fake(); | |
$response = $this->registerController->register($request); | |
- Mail::assertQueued(UserRegistered::class); | |
+ $this->mailer->assertQueued(UserRegistered::class); | |
$this->assertInstanceOf(RedirectResponse::class, $response); | |
$this->assertEquals( | |
action([RegisterController::class, 'showRegisterSuccess']), | |
@@ -121,11 +129,10 @@ class RegisterControllerTest extends TestCase | |
->shouldReceive('findByEmail') | |
->with($inputs['email']) | |
->andReturn($fakeUser); | |
- Mail::fake(); | |
$response = $this->registerController->resendVerificationLink($request); | |
- Mail::assertQueued(UserRegistered::class); | |
+ $this->mailer->assertQueued(UserRegistered::class); | |
$this->assertInstanceOf(RedirectResponse::class, $response); | |
$this->assertEquals( | |
action([RegisterController::class, 'showFormVerification']), | |
diff --git a/tests/Unit/Mail/UserRegisteredTest.php b/tests/Unit/Mail/UserRegisteredTest.php | |
index 9ad2fe4..ceb8297 100644 | |
--- a/tests/Unit/Mail/UserRegisteredTest.php | |
+++ b/tests/Unit/Mail/UserRegisteredTest.php | |
@@ -5,6 +5,7 @@ namespace Tests\Unit\Mail; | |
use Tests\TestCase; | |
use App\Mail\UserRegistered; | |
use Illuminate\Contracts\Queue\ShouldQueue; | |
+use Illuminate\Routing\UrlGenerator; | |
class UserRegisteredTest extends TestCase | |
{ | |
@@ -19,7 +20,7 @@ class UserRegisteredTest extends TestCase | |
{ | |
$mailable = new UserRegistered(1, 'Test user name'); | |
- $mailable->build(); | |
+ $mailable->build($this->app->make(UrlGenerator::class)); | |
$this->assertArrayHasKey('url', $mailable->viewData); | |
$this->assertEquals('Test user name', $mailable->viewData['userName']); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Rector 0.8.x-dev@e02410b | |
8 files with changes | |
==================== | |
1) app/Http/Controllers/Web/HomeController.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -6,8 +6,16 @@ | |
class HomeController extends Controller | |
{ | |
+ /** | |
+ * @var \Illuminate\Contracts\View\Factory | |
+ */ | |
+ private $viewFactory; | |
+ public function __construct(\Illuminate\Contracts\View\Factory $viewFactory) | |
+ { | |
+ $this->viewFactory = $viewFactory; | |
+ } | |
public function index() | |
{ | |
- return view('home'); | |
+ return $this->viewFactory->make('home'); | |
} | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\FuncCall\HelperFunctionToConstructorInjectionRector | |
2) app/Http/Controllers/Web/RegisterController.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -12,10 +12,25 @@ | |
class RegisterController extends Controller | |
{ | |
protected $userService; | |
+ /** | |
+ * @var \Illuminate\Contracts\View\Factory | |
+ */ | |
+ private $viewFactory; | |
+ /** | |
+ * @var \Illuminate\Mail\Mailer | |
+ */ | |
+ private $mailer; | |
+ /** | |
+ * @var \Illuminate\Routing\Redirector | |
+ */ | |
+ private $redirector; | |
- public function __construct(UserService $userService) | |
+ public function __construct(UserService $userService, \Illuminate\Contracts\View\Factory $viewFactory, \Illuminate\Mail\Mailer $mailer, \Illuminate\Routing\Redirector $redirector) | |
{ | |
$this->userService = $userService; | |
+ $this->viewFactory = $viewFactory; | |
+ $this->mailer = $mailer; | |
+ $this->redirector = $redirector; | |
} | |
/** | |
@@ -25,12 +40,12 @@ | |
*/ | |
public function showFormRegister() | |
{ | |
- return view('auth.register'); | |
+ return $this->viewFactory->make('auth.register'); | |
} | |
public function showRegisterSuccess() | |
{ | |
- return view('auth.register_success'); | |
+ return $this->viewFactory->make('auth.register_success'); | |
} | |
public function register(RegisterRequest $request) | |
@@ -39,9 +54,9 @@ | |
$user = $this->userService->create($inputs); | |
- Mail::to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
+ $this->mailer->to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
- return redirect()->action([static::class, 'showRegisterSuccess']); | |
+ return $this->redirector->action([static::class, 'showRegisterSuccess']); | |
} | |
public function verify(Request $request) | |
@@ -55,12 +70,12 @@ | |
$this->userService->verifyUser($user); | |
} | |
- return view('auth.verify.message'); | |
+ return $this->viewFactory->make('auth.verify.message'); | |
} | |
public function showFormVerification() | |
{ | |
- return view('auth.verify.resend'); | |
+ return $this->viewFactory->make('auth.verify.resend'); | |
} | |
public function resendVerificationLink(Request $request) | |
@@ -68,9 +83,9 @@ | |
$user = $this->userService->findByEmail($request->input('email')); | |
if ($user && !$user->hasVerifiedEmail()) { | |
- Mail::to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
+ $this->mailer->to($user)->send(new UserRegistered($user->getKey(), $user->name)); | |
} | |
- return redirect()->action([static::class, 'showFormVerification'])->with('resent', true); | |
+ return $this->redirector->action([static::class, 'showFormVerification'])->with('resent', true); | |
} | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\FuncCall\HelperFunctionToConstructorInjectionRector | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
3) app/Http/Middleware/Authenticate.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -7,6 +7,15 @@ | |
class Authenticate extends Middleware | |
{ | |
/** | |
+ * @var \Illuminate\Routing\UrlGenerator | |
+ */ | |
+ private $urlGenerator; | |
+ public function __construct(\Illuminate\Routing\UrlGenerator $urlGenerator) | |
+ { | |
+ $this->urlGenerator = $urlGenerator; | |
+ parent::__construct(); | |
+ } | |
+ /** | |
* Get the path the user should be redirected to when they are not authenticated. | |
* | |
* @param \Illuminate\Http\Request $request | |
@@ -15,7 +24,7 @@ | |
protected function redirectTo($request) | |
{ | |
if (! $request->expectsJson()) { | |
- return route('login'); | |
+ return $this->urlGenerator->route('login'); | |
} | |
} | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\FuncCall\HelperFunctionToConstructorInjectionRector | |
4) app/Http/Middleware/RedirectIfAuthenticated.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -8,6 +8,19 @@ | |
class RedirectIfAuthenticated | |
{ | |
/** | |
+ * @var \Illuminate\Auth\AuthManager | |
+ */ | |
+ private $authManager; | |
+ /** | |
+ * @var \Illuminate\Routing\Redirector | |
+ */ | |
+ private $redirector; | |
+ public function __construct(\Illuminate\Auth\AuthManager $authManager, \Illuminate\Routing\Redirector $redirector) | |
+ { | |
+ $this->authManager = $authManager; | |
+ $this->redirector = $redirector; | |
+ } | |
+ /** | |
* Handle an incoming request. | |
* | |
* @param \Illuminate\Http\Request $request | |
@@ -17,8 +30,8 @@ | |
*/ | |
public function handle($request, Closure $next, $guard = null) | |
{ | |
- if (Auth::guard($guard)->check()) { | |
- return redirect('/home'); | |
+ if ($this->authManager->guard($guard)->check()) { | |
+ return $this->redirector->back('/home'); | |
} | |
return $next($request); | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\FuncCall\HelperFunctionToConstructorInjectionRector | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
5) app/Mail/UserRegistered.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -15,11 +15,16 @@ | |
protected $userId; | |
protected $userName; | |
+ /** | |
+ * @var \Illuminate\Routing\UrlGenerator | |
+ */ | |
+ private $urlGenerator; | |
- public function __construct($userId, $userName) | |
+ public function __construct($userId, $userName, \Illuminate\Routing\UrlGenerator $urlGenerator) | |
{ | |
$this->userId = $userId; | |
$this->userName = $userName; | |
+ $this->urlGenerator = $urlGenerator; | |
} | |
/** | |
@@ -39,10 +44,6 @@ | |
private function verificationUrl() | |
{ | |
- return URL::temporarySignedRoute( | |
- 'web.register.verify', | |
- now()->addMinutes(60), | |
- ['id' => $this->userId] | |
- ); | |
+ return $this->urlGenerator->temporarySignedRoute('web.register.verify', now()->addMinutes(60), ['id' => $this->userId]); | |
} | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
6) app/Models/User.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -39,11 +39,20 @@ | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
]; | |
+ /** | |
+ * @var \Illuminate\Contracts\Hashing\Hasher | |
+ */ | |
+ private $hasher; | |
+ public function __construct(\Illuminate\Contracts\Hashing\Hasher $hasher) | |
+ { | |
+ $this->hasher = $hasher; | |
+ parent::__construct(); | |
+ } | |
public function setPasswordAttribute($value) | |
{ | |
if ($value) { | |
- $this->attributes['password'] = Hash::make($value); | |
+ $this->attributes['password'] = $this->hasher->make($value); | |
} | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
7) app/Providers/BroadcastServiceProvider.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -8,6 +8,15 @@ | |
class BroadcastServiceProvider extends ServiceProvider | |
{ | |
/** | |
+ * @var \Illuminate\Contracts\Broadcasting\Factory | |
+ */ | |
+ private $factory; | |
+ public function __construct(\Illuminate\Contracts\Broadcasting\Factory $factory) | |
+ { | |
+ $this->factory = $factory; | |
+ parent::__construct(); | |
+ } | |
+ /** | |
* Bootstrap any application services. | |
* | |
* @return void | |
@@ -14,7 +23,7 @@ | |
*/ | |
public function boot() | |
{ | |
- Broadcast::routes(); | |
+ $this->factory->routes(); | |
require base_path('routes/channels.php'); | |
} | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
8) app/Providers/RouteServiceProvider.php | |
---------- begin diff ---------- | |
--- Original | |
+++ New | |
@@ -15,6 +15,15 @@ | |
* @var string | |
*/ | |
protected $namespace = 'App\Http\Controllers'; | |
+ /** | |
+ * @var \Illuminate\Routing\Router | |
+ */ | |
+ private $router; | |
+ public function __construct(\Illuminate\Routing\Router $router) | |
+ { | |
+ $this->router = $router; | |
+ parent::__construct(); | |
+ } | |
/** | |
* Define your route model bindings, pattern filters, etc. | |
@@ -51,7 +60,7 @@ | |
*/ | |
protected function mapWebRoutes() | |
{ | |
- Route::middleware('web') | |
+ $this->router->middleware('web') | |
->namespace($this->namespace) | |
->group(base_path('routes/web.php')); | |
} | |
@@ -65,7 +74,7 @@ | |
*/ | |
protected function mapApiRoutes() | |
{ | |
- Route::prefix('api') | |
+ $this->router->prefix('api') | |
->middleware('api') | |
->namespace($this->namespace) | |
->group(base_path('routes/api.php')); | |
----------- end diff ----------- | |
Applied rules: | |
* Rector\Laravel\Rector\StaticCall\FacadeStaticCallToConstructorInjectionRector | |
[OK] Rector is done! 8 files would have changed (dry-run). | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment