Skip to content

Commit

Permalink
Add Language Switcher Install Command Test
Browse files Browse the repository at this point in the history
  • Loading branch information
askdkc committed Jul 7, 2023
1 parent 7b6fcb2 commit 179af98
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 0 deletions.
68 changes: 68 additions & 0 deletions tests/Kernel.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace App\Http;

use Illuminate\Foundation\Http\Kernel as HttpKernel;

class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array<int, class-string|string>
*/
protected $middleware = [
// \App\Http\Middleware\TrustHosts::class,
\App\Http\Middleware\TrustProxies::class,
\Illuminate\Http\Middleware\HandleCors::class,
\App\Http\Middleware\PreventRequestsDuringMaintenance::class,
\Illuminate\Foundation\Http\Middleware\ValidatePostSize::class,
\App\Http\Middleware\TrimStrings::class,
\Illuminate\Foundation\Http\Middleware\ConvertEmptyStringsToNull::class,
];

/**
* The application's route middleware groups.
*
* @var array<string, array<int, class-string|string>>
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],

'api' => [
// \Laravel\Sanctum\Http\Middleware\EnsureFrontendRequestsAreStateful::class,
\Illuminate\Routing\Middleware\ThrottleRequests::class.':api',
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
];

/**
* The application's middleware aliases.
*
* Aliases may be used instead of class names to conveniently assign middleware to routes and groups.
*
* @var array<string, class-string|string>
*/
protected $middlewareAliases = [
'auth' => \App\Http\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'auth.session' => \Illuminate\Session\Middleware\AuthenticateSession::class,
'cache.headers' => \Illuminate\Http\Middleware\SetCacheHeaders::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'password.confirm' => \Illuminate\Auth\Middleware\RequirePassword::class,
'precognitive' => \Illuminate\Foundation\Http\Middleware\HandlePrecognitiveRequests::class,
'signed' => \App\Http\Middleware\ValidateSignature::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'verified' => \Illuminate\Auth\Middleware\EnsureEmailIsVerified::class,
];
}
14 changes: 14 additions & 0 deletions tests/LanguageSwitcherInstallTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

test('breezejp language switcher command successfully', closure: function () {
$this->artisan('breezejp --langswitch')
->expectsOutput('言語切替用のRoute language/{locale} を準備します')
->expectsOutput('言語切替用の Middleware を準備します')
->expectsOutput('Kernel に Middleware を登録します')
->expectsOutput('Language Switherのインストールが完了しました!')
->assertExitCode(0);

$this->assertFileExists(base_path('app/Http/Middleware/Localization.php'));
$webfile = file_get_contents(base_path('routes/web.php'));
$this->assertStringContainsString("// Language Switcher Route 言語切替用ルートだよ", $webfile);
});
5 changes: 5 additions & 0 deletions tests/NoDebugCommandLeftTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php

test('globals')
->expect(['dd', 'dump','ray'])
->not->toBeUsed();
24 changes: 24 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,26 @@ protected function setUp(): void
{
parent::setUp();

// テスト用のファイルが残ってたら消す(web.php)
if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) {
unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php');
}

// テスト用のファイル作成(web.php)
if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php')) {
copy(__DIR__.'/web.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/routes/web.php');
}

// テスト用のファイルが残ってたら消す(Kernel.php)
if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) {
unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php');
}

// テスト用のファイル作成(Kernel.php)
if (!is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php')) {
copy(__DIR__.'/Kernel.php.stub', __DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Kernel.php');
}

Factory::guessFactoryNamesUsing(
fn (string $modelName) => 'Askdkc\\Breezejp\\Database\\Factories\\'.class_basename($modelName).'Factory'
);
Expand All @@ -26,6 +46,10 @@ protected function setUp(): void
rmdir(__DIR__.'/../vendor/orchestra/testbench-core/laravel/lang/ja');
}

if (is_file(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Middleware/Localization.php')) {
unlink(__DIR__.'/../vendor/orchestra/testbench-core/laravel/app/Http/Middleware/Localization.php');
}

// config/app.phpのlocaleをenに戻す
$configfile = file_get_contents(__DIR__.'/../vendor/orchestra/testbench-core/laravel/config/app.php');
$configfile = str_replace("'locale' => 'ja'", "'locale' => 'en'", $configfile);
Expand Down
31 changes: 31 additions & 0 deletions tests/web.php.stub
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/

Route::get('/', function () {
return view('welcome');
});

Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');

Route::middleware('auth')->group(function () {
Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

require __DIR__.'/auth.php';

0 comments on commit 179af98

Please sign in to comment.